@ekodb/ekodb-client 0.19.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,8 +3,7 @@
3
3
  *
4
4
  * These tests cover the pure-data construction helpers and the structural
5
5
  * parameter placeholder. They don't hit a running ekoDB — server-side
6
- * behavior is covered by the Rust integration tests in
7
- * `ekodb/ekodb_server/tests/function_parameters_tests.rs`.
6
+ * behavior is covered by the server-side integration tests.
8
7
  */
9
8
 
10
9
  import { describe, it, expect } from "vitest";
@@ -123,12 +123,6 @@ describe("QueryBuilder string operators", () => {
123
123
 
124
124
  expect(query.filter.content.operator).toBe("EndsWith");
125
125
  });
126
-
127
- it("builds regex filter", () => {
128
- const query = new QueryBuilder().regex("phone", "^\\+1").build();
129
-
130
- expect(query.filter.content.operator).toBe("Regex");
131
- });
132
126
  });
133
127
 
134
128
  // ============================================================================
@@ -464,7 +458,6 @@ describe("QueryBuilder chaining", () => {
464
458
  expect(qb.contains("i", "j")).toBe(qb);
465
459
  expect(qb.startsWith("k", "l")).toBe(qb);
466
460
  expect(qb.endsWith("m", "n")).toBe(qb);
467
- expect(qb.regex("o", "p")).toBe(qb);
468
461
  expect(qb.sortAsc("q")).toBe(qb);
469
462
  expect(qb.sortDesc("r")).toBe(qb);
470
463
  expect(qb.limit(1)).toBe(qb);
@@ -216,20 +216,8 @@ export class QueryBuilder {
216
216
  return this;
217
217
  }
218
218
 
219
- /**
220
- * Add a regex filter
221
- */
222
- regex(field: string, pattern: string): this {
223
- this.filters.push({
224
- type: "Condition",
225
- content: {
226
- field,
227
- operator: "Regex",
228
- value: pattern,
229
- },
230
- });
231
- return this;
232
- }
219
+ // Note: regex filtering is pending server-side support. The server has no
220
+ // Regex filter operator; use contains/startsWith/endsWith instead.
233
221
 
234
222
  // ========================================================================
235
223
  // Logical Operators
package/src/utils.test.ts CHANGED
@@ -50,6 +50,11 @@ describe("getValue", () => {
50
50
  expect(getValue(field)).toBeNull();
51
51
  });
52
52
 
53
+ it("passes through a user object that has a value key but no type (regression #134)", () => {
54
+ const field = { value: 1, currency: "USD" };
55
+ expect(getValue(field)).toEqual(field);
56
+ });
57
+
53
58
  it("returns plain string as-is", () => {
54
59
  expect(getValue("plain string")).toBe("plain string");
55
60
  });
package/src/utils.ts CHANGED
@@ -18,7 +18,15 @@
18
18
  * ```
19
19
  */
20
20
  export function getValue<T = any>(field: any): T {
21
- if (field && typeof field === "object" && "value" in field) {
21
+ // Only unwrap a genuine typed wrapper one carrying BOTH a "type"
22
+ // discriminator and a "value". A user object that merely has a "value" key
23
+ // (e.g. { value: 1, currency: "USD" }) must pass through untouched.
24
+ if (
25
+ field &&
26
+ typeof field === "object" &&
27
+ "type" in field &&
28
+ "value" in field
29
+ ) {
22
30
  return field.value as T;
23
31
  }
24
32
  return field as T;
@@ -122,6 +122,45 @@ describe("WebSocketClient", () => {
122
122
  });
123
123
  });
124
124
 
125
+ // --------------------------------------------------------------------------
126
+ // URL normalization
127
+ // --------------------------------------------------------------------------
128
+
129
+ describe("URL normalization", () => {
130
+ function captureRequestPath(): Promise<string> {
131
+ return new Promise((resolve) => {
132
+ wss.once("connection", (_ws, req) => resolve(req.url ?? ""));
133
+ });
134
+ }
135
+
136
+ it("appends /api/ws without a double slash when the base URL has a trailing slash", async () => {
137
+ const pathPromise = captureRequestPath();
138
+ const client = new WebSocketClient(
139
+ `ws://localhost:${port}/`,
140
+ "test-token",
141
+ );
142
+ // Trigger a connect; we only assert the path the server receives.
143
+ client.findAll("users").catch(() => {});
144
+
145
+ expect(await pathPromise).toBe("/api/ws");
146
+
147
+ client.close();
148
+ });
149
+
150
+ it("does not duplicate /api/ws when the URL already ends with it plus a trailing slash", async () => {
151
+ const pathPromise = captureRequestPath();
152
+ const client = new WebSocketClient(
153
+ `ws://localhost:${port}/api/ws/`,
154
+ "test-token",
155
+ );
156
+ client.findAll("users").catch(() => {});
157
+
158
+ expect(await pathPromise).toBe("/api/ws");
159
+
160
+ client.close();
161
+ });
162
+ });
163
+
125
164
  // --------------------------------------------------------------------------
126
165
  // subscribe
127
166
  // --------------------------------------------------------------------------
@@ -709,4 +748,238 @@ describe("WebSocketClient", () => {
709
748
  client.close();
710
749
  });
711
750
  });
751
+
752
+ // --------------------------------------------------------------------------
753
+ // Per-request timeout
754
+ // --------------------------------------------------------------------------
755
+
756
+ describe("per-request timeout", () => {
757
+ it("rejects a pending request when no response arrives", async () => {
758
+ const client = new WebSocketClient(
759
+ `ws://localhost:${port}/api/ws`,
760
+ "test-token",
761
+ { requestTimeoutMs: 50, autoReconnect: false },
762
+ );
763
+
764
+ const resultPromise = client.findAll("users");
765
+
766
+ await new Promise((r) => wss.once("connection", r));
767
+ const ws = getLastConnection();
768
+ await waitForMessage(ws); // receive FindAll but never respond
769
+
770
+ await expect(resultPromise).rejects.toThrow(/timed out after 50ms/);
771
+
772
+ client.close();
773
+ });
774
+
775
+ it("does not time out when a response arrives in time", async () => {
776
+ const client = new WebSocketClient(
777
+ `ws://localhost:${port}/api/ws`,
778
+ "test-token",
779
+ { requestTimeoutMs: 500, autoReconnect: false },
780
+ );
781
+
782
+ const resultPromise = client.findAll("users");
783
+
784
+ await new Promise((r) => wss.once("connection", r));
785
+ const ws = getLastConnection();
786
+ const msg = await waitForMessage(ws);
787
+ ws.send(
788
+ JSON.stringify({
789
+ type: "Success",
790
+ payload: { message_id: msg.messageId, data: [{ id: "1" }] },
791
+ }),
792
+ );
793
+
794
+ await expect(resultPromise).resolves.toEqual([{ id: "1" }]);
795
+ client.close();
796
+ });
797
+ });
798
+
799
+ // --------------------------------------------------------------------------
800
+ // Reject pending requests on disconnect
801
+ // --------------------------------------------------------------------------
802
+
803
+ describe("disconnect handling", () => {
804
+ it("rejects in-flight requests when the socket drops", async () => {
805
+ const client = new WebSocketClient(
806
+ `ws://localhost:${port}/api/ws`,
807
+ "test-token",
808
+ { autoReconnect: false },
809
+ );
810
+
811
+ const resultPromise = client.findAll("users");
812
+
813
+ await new Promise((r) => wss.once("connection", r));
814
+ const ws = getLastConnection();
815
+ await waitForMessage(ws); // FindAll received, never answered
816
+
817
+ // Server drops the connection.
818
+ ws.close();
819
+
820
+ await expect(resultPromise).rejects.toThrow(/connection closed/i);
821
+
822
+ client.close();
823
+ });
824
+ });
825
+
826
+ // --------------------------------------------------------------------------
827
+ // Backoff helper
828
+ // --------------------------------------------------------------------------
829
+
830
+ describe("computeBackoff", () => {
831
+ it("grows exponentially, stays within jittered bounds, and caps", () => {
832
+ const client = new WebSocketClient(
833
+ `ws://localhost:${port}/api/ws`,
834
+ "test-token",
835
+ { reconnectInitialDelayMs: 200, reconnectMaxDelayMs: 5000 },
836
+ );
837
+
838
+ // attempt 0 -> base 200, +/-25% => [150, 250]
839
+ for (let i = 0; i < 20; i++) {
840
+ const d0 = client.computeBackoff(0);
841
+ expect(d0).toBeGreaterThanOrEqual(150);
842
+ expect(d0).toBeLessThanOrEqual(250);
843
+ }
844
+
845
+ // attempt 3 -> base 1600, +/-25% => [1200, 2000]
846
+ const d3 = client.computeBackoff(3);
847
+ expect(d3).toBeGreaterThanOrEqual(1200);
848
+ expect(d3).toBeLessThanOrEqual(2000);
849
+
850
+ // attempt 20 -> capped at 5000, +/-25% => [3750, 6250]
851
+ const dBig = client.computeBackoff(20);
852
+ expect(dBig).toBeGreaterThanOrEqual(3750);
853
+ expect(dBig).toBeLessThanOrEqual(6250);
854
+
855
+ client.close();
856
+ });
857
+ });
858
+
859
+ // --------------------------------------------------------------------------
860
+ // Auto-reconnect + re-subscribe + fresh token
861
+ // --------------------------------------------------------------------------
862
+
863
+ describe("auto-reconnect", () => {
864
+ it("re-subscribes after a socket drop and re-evaluates the token", async () => {
865
+ // Token provider returns a new token each call so we can assert the
866
+ // reconnect used a freshly-obtained token (not a stale snapshot).
867
+ let tokenCalls = 0;
868
+ const tokenProvider = () => `token-${++tokenCalls}`;
869
+
870
+ const client = new WebSocketClient(
871
+ `ws://localhost:${port}/api/ws`,
872
+ tokenProvider,
873
+ {
874
+ autoReconnect: true,
875
+ reconnectInitialDelayMs: 10,
876
+ reconnectMaxDelayMs: 30,
877
+ },
878
+ );
879
+
880
+ // Establish the subscription on the first connection.
881
+ const streamPromise = client.subscribe("orders");
882
+ await new Promise((r) => wss.once("connection", r));
883
+ const ws1 = getLastConnection();
884
+ const sub1 = await waitForMessage(ws1);
885
+ expect(sub1.type).toBe("Subscribe");
886
+ expect(sub1.payload.collection).toBe("orders");
887
+ ws1.send(
888
+ JSON.stringify({
889
+ type: "Success",
890
+ payload: { message_id: sub1.messageId, status: "subscribed" },
891
+ }),
892
+ );
893
+ const stream = await streamPromise;
894
+ expect(tokenCalls).toBe(1);
895
+
896
+ // Arm the next-connection handler BEFORE dropping so we don't miss it.
897
+ const nextConn = new Promise<WS>((resolve) => {
898
+ wss.once("connection", (ws: WS) => resolve(ws));
899
+ });
900
+
901
+ // Simulate a transient drop: server closes the socket.
902
+ ws1.close();
903
+
904
+ // The client should auto-reconnect (new connection) and re-send Subscribe.
905
+ const ws2 = await nextConn;
906
+ const sub2 = await waitForMessage(ws2);
907
+ expect(sub2.type).toBe("Subscribe");
908
+ expect(sub2.payload.collection).toBe("orders");
909
+
910
+ // Reconnect re-evaluated the token provider.
911
+ expect(tokenCalls).toBeGreaterThanOrEqual(2);
912
+
913
+ // Ack the re-subscribe.
914
+ ws2.send(
915
+ JSON.stringify({
916
+ type: "Success",
917
+ payload: { message_id: sub2.messageId, status: "subscribed" },
918
+ }),
919
+ );
920
+
921
+ // The SAME stream still delivers mutations over the new socket.
922
+ const mutationPromise = new Promise<any>((resolve) => {
923
+ stream.on("mutation", resolve);
924
+ });
925
+ ws2.send(
926
+ JSON.stringify({
927
+ type: "MutationNotification",
928
+ payload: {
929
+ collection: "orders",
930
+ event: "insert",
931
+ record_ids: ["order-9"],
932
+ timestamp: "2026-06-02T00:00:00Z",
933
+ },
934
+ }),
935
+ );
936
+ const mutation = await mutationPromise;
937
+ expect(mutation.recordIds).toEqual(["order-9"]);
938
+ expect(stream.closed).toBe(false);
939
+
940
+ client.close();
941
+ });
942
+
943
+ it("does not reconnect after an intentional close()", async () => {
944
+ const client = new WebSocketClient(
945
+ `ws://localhost:${port}/api/ws`,
946
+ "test-token",
947
+ { autoReconnect: true, reconnectInitialDelayMs: 10 },
948
+ );
949
+
950
+ const streamPromise = client.subscribe("widgets");
951
+ await new Promise((r) => wss.once("connection", r));
952
+ const ws = getLastConnection();
953
+ const sub = await waitForMessage(ws);
954
+ ws.send(
955
+ JSON.stringify({
956
+ type: "Success",
957
+ payload: { message_id: sub.messageId, status: "subscribed" },
958
+ }),
959
+ );
960
+ await streamPromise;
961
+
962
+ const connectionsBefore = serverConnections.length;
963
+
964
+ // Intentional close: must NOT reconnect.
965
+ client.close();
966
+
967
+ // Give any erroneous reconnect time to land.
968
+ await new Promise((r) => setTimeout(r, 80));
969
+ expect(serverConnections.length).toBe(connectionsBefore);
970
+ });
971
+ });
972
+
973
+ describe("auth token validation", () => {
974
+ it("rejects connect when the token provider returns null (no Bearer null)", async () => {
975
+ const client = new WebSocketClient(
976
+ `ws://localhost:${port}/api/ws`,
977
+ () => null,
978
+ );
979
+ await expect(client.findAll("users")).rejects.toThrow(
980
+ /token is unavailable/i,
981
+ );
982
+ client.close();
983
+ });
984
+ });
712
985
  });