@peers-app/peers-sdk 0.26.5 → 0.27.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.
- package/README.md +3 -1
- package/bin/peers-package.js +13 -0
- package/define/package.json +4 -0
- package/dist/contracts/__tests__/action-dispatch.test.js +56 -76
- package/dist/contracts/__tests__/also-implements.test.d.ts +1 -0
- package/dist/contracts/__tests__/also-implements.test.js +110 -0
- package/dist/contracts/__tests__/builder.test.js +45 -0
- package/dist/contracts/__tests__/contract-resolvers.test.js +134 -4
- package/dist/contracts/__tests__/frozen-contracts.test.d.ts +1 -0
- package/dist/contracts/__tests__/frozen-contracts.test.js +191 -0
- package/dist/contracts/__tests__/generic-tool-dispatch.test.js +13 -56
- package/dist/contracts/__tests__/host-contract-client.test.d.ts +1 -0
- package/dist/contracts/__tests__/host-contract-client.test.js +793 -0
- package/dist/contracts/__tests__/host-contract-transport.test.js +15 -0
- package/dist/contracts/__tests__/registry.test.js +68 -0
- package/dist/contracts/__tests__/target-dispatch.d.ts +11 -0
- package/dist/contracts/__tests__/target-dispatch.js +24 -0
- package/dist/contracts/__tests__/tool-catalog-service.test.js +37 -1
- package/dist/contracts/__tests__/tool-catalog.test.js +12 -3
- package/dist/contracts/__tests__/validate.test.js +177 -7
- package/dist/contracts/__tests__/workflow-context.test.js +0 -11
- package/dist/contracts/builder.d.ts +16 -2
- package/dist/contracts/builder.js +44 -3
- package/dist/contracts/contract-definition-schema.d.ts +251 -0
- package/dist/contracts/contract-definition-schema.js +48 -0
- package/dist/contracts/contract-provider-router.d.ts +2 -0
- package/dist/contracts/contract-provider-router.js +5 -2
- package/dist/contracts/contract-proxy.d.ts +52 -18
- package/dist/contracts/contract-proxy.js +144 -203
- package/dist/contracts/contract-resolvers.d.ts +6 -4
- package/dist/contracts/contract-resolvers.js +32 -9
- package/dist/contracts/frozen-contracts.d.ts +59 -0
- package/dist/contracts/frozen-contracts.js +86 -0
- package/dist/contracts/host-contract-client.d.ts +134 -0
- package/dist/contracts/host-contract-client.js +396 -0
- package/dist/contracts/host-contract-transport.d.ts +2 -0
- package/dist/contracts/host-contract-transport.js +8 -0
- package/dist/contracts/index.d.ts +6 -4
- package/dist/contracts/index.js +11 -5
- package/dist/contracts/registry.d.ts +31 -1
- package/dist/contracts/registry.js +100 -6
- package/dist/contracts/system/contract-catalog-contract.d.ts +61 -6
- package/dist/contracts/system/contract-catalog-contract.js +17 -5
- package/dist/contracts/tool-catalog-service.js +26 -6
- package/dist/contracts/tool-catalog.d.ts +1 -1
- package/dist/contracts/tool-catalog.js +4 -1
- package/dist/contracts/types.d.ts +32 -2
- package/dist/contracts/validate.d.ts +16 -1
- package/dist/contracts/validate.js +139 -25
- package/dist/data/change-tracking.d.ts +17 -5
- package/dist/data/change-tracking.js +22 -6
- package/dist/data/change-tracking.test.d.ts +1 -0
- package/dist/data/change-tracking.test.js +62 -0
- package/dist/data/group-members.d.ts +6 -1
- package/dist/data/group-members.js +12 -4
- package/dist/data/groups.d.ts +6 -1
- package/dist/data/groups.js +12 -4
- package/dist/data/orm/sql.data-source.d.ts +9 -0
- package/dist/data/orm/sql.data-source.js +47 -2
- package/dist/data/orm/sql.data-source.test.js +142 -0
- package/dist/data/orm/table-container.js +9 -9
- package/dist/data/orm/table-container.test.js +44 -0
- package/dist/data/orm/types.d.ts +41 -1
- package/dist/data/orm/types.js +159 -28
- package/dist/data/orm/types.test.js +129 -0
- package/dist/data/package-versions.d.ts +5 -1
- package/dist/data/package-versions.js +11 -4
- package/dist/data/packages.d.ts +7 -1
- package/dist/data/packages.js +13 -4
- package/dist/data/peer-types.d.ts +8 -62
- package/dist/data/persistent-vars.d.ts +4 -1
- package/dist/data/persistent-vars.js +7 -2
- package/dist/data/table-signing.test.d.ts +1 -0
- package/dist/data/table-signing.test.js +118 -0
- package/dist/data/tools.d.ts +29 -221
- package/dist/data/users.d.ts +1 -2
- package/dist/data/users.js +8 -5
- package/dist/define/also-implements.test.d.ts +1 -0
- package/dist/define/also-implements.test.js +123 -0
- package/dist/define/context.d.ts +77 -0
- package/dist/define/context.js +2 -0
- package/dist/define/contract-ref.d.ts +177 -0
- package/dist/define/contract-ref.js +67 -0
- package/dist/define/declare-contract.d.ts +167 -0
- package/dist/define/declare-contract.js +220 -0
- package/dist/define/declare-contract.test.d.ts +1 -0
- package/dist/define/declare-contract.test.js +573 -0
- package/dist/define/define-package.d.ts +203 -0
- package/dist/define/define-package.js +289 -0
- package/dist/define/define.test.d.ts +1 -0
- package/dist/define/define.test.js +1010 -0
- package/dist/define/empty-fields.d.ts +15 -0
- package/dist/define/empty-fields.js +134 -0
- package/dist/define/empty-fields.test.d.ts +1 -0
- package/dist/define/empty-fields.test.js +55 -0
- package/dist/define/events-observables.test.d.ts +1 -0
- package/dist/define/events-observables.test.js +1013 -0
- package/dist/define/events.d.ts +81 -0
- package/dist/define/events.js +81 -0
- package/dist/define/generated-field.d.ts +20 -0
- package/dist/define/generated-field.js +25 -0
- package/dist/define/index.d.ts +28 -0
- package/dist/define/index.js +62 -0
- package/dist/define/lazy-members.d.ts +8 -0
- package/dist/define/lazy-members.js +38 -0
- package/dist/define/manifest.d.ts +63 -0
- package/dist/define/manifest.js +653 -0
- package/dist/define/observables.d.ts +146 -0
- package/dist/define/observables.js +187 -0
- package/dist/define/serve-members.d.ts +21 -0
- package/dist/define/serve-members.js +152 -0
- package/dist/define/serve-members.test.d.ts +1 -0
- package/dist/define/serve-members.test.js +630 -0
- package/dist/define/serve.d.ts +22 -0
- package/dist/define/serve.js +58 -0
- package/dist/define/system-contracts.d.ts +41 -0
- package/dist/define/system-contracts.js +21 -0
- package/dist/define/table-handle.d.ts +13 -0
- package/dist/define/table-handle.js +36 -0
- package/dist/define/table.d.ts +128 -0
- package/dist/define/table.js +122 -0
- package/dist/define/tool.d.ts +129 -0
- package/dist/define/tool.js +91 -0
- package/dist/define/vars.d.ts +82 -0
- package/dist/define/vars.js +256 -0
- package/dist/define/vars.test.d.ts +1 -0
- package/dist/define/vars.test.js +39 -0
- package/dist/define/wire-value.d.ts +12 -0
- package/dist/define/wire-value.js +76 -0
- package/dist/define/worker-state.d.ts +15 -0
- package/dist/define/worker-state.js +45 -0
- package/dist/device/tx-encoding.test.js +10 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +5 -1
- package/dist/observable.d.ts +42 -2
- package/dist/observable.js +25 -7
- package/dist/package-build/build-package.d.ts +60 -0
- package/dist/package-build/build-package.js +312 -0
- package/dist/package-build/build-package.test.d.ts +1 -0
- package/dist/package-build/build-package.test.js +204 -0
- package/dist/package-build/cli.d.ts +7 -0
- package/dist/package-build/cli.js +218 -0
- package/dist/package-build/contract-add.d.ts +35 -0
- package/dist/package-build/contract-add.js +97 -0
- package/dist/package-build/contract-add.test.d.ts +1 -0
- package/dist/package-build/contract-add.test.js +467 -0
- package/dist/package-build/contract-source.d.ts +59 -0
- package/dist/package-build/contract-source.js +471 -0
- package/dist/package-build/freeze-contracts.d.ts +56 -0
- package/dist/package-build/freeze-contracts.js +198 -0
- package/dist/package-build/freeze-contracts.test.d.ts +1 -0
- package/dist/package-build/freeze-contracts.test.js +355 -0
- package/dist/package-installer/package-creator.d.ts +4 -0
- package/dist/package-installer/package-creator.js +57 -16
- package/dist/package-installer/package-creator.test.js +86 -0
- package/dist/package-loader/contract-members.d.ts +53 -0
- package/dist/package-loader/contract-members.js +31 -0
- package/dist/package-loader/contract-package-loader.js +5 -1
- package/dist/package-loader/contract-package-loader.test.js +11 -0
- package/dist/package-loader/host-authority-loading.test.js +9 -5
- package/dist/package-loader/index.d.ts +1 -0
- package/dist/package-loader/index.js +1 -0
- package/dist/package-loader/isolated-artifact.d.ts +80 -17
- package/dist/package-loader/isolated-artifact.js +257 -51
- package/dist/package-loader/isolated-artifact.test.js +294 -21
- package/dist/package-loader/isolated-runtime.d.ts +22 -7
- package/dist/package-loader/isolated-table-gateway.js +30 -11
- package/dist/package-loader/isolated-table-gateway.test.js +243 -0
- package/dist/package-loader/package-loader.d.ts +8 -1
- package/dist/package-loader/package-loader.js +39 -20
- package/dist/package-loader/package-loader.test.js +47 -0
- package/dist/peer-id.d.ts +25 -0
- package/dist/peer-id.js +99 -0
- package/dist/serial-json.compat.test.d.ts +1 -0
- package/dist/serial-json.compat.test.js +369 -0
- package/dist/serial-json.d.ts +41 -5
- package/dist/serial-json.js +216 -157
- package/dist/serial-json.oracle.d.ts +21 -0
- package/dist/serial-json.oracle.js +222 -0
- package/dist/serial-json.test.js +315 -0
- package/dist/testing/index.d.ts +4 -0
- package/dist/testing/index.js +8 -0
- package/dist/testing/mock-host-contract.d.ts +123 -0
- package/dist/testing/mock-host-contract.js +434 -0
- package/dist/testing/mock-host-contract.test.d.ts +1 -0
- package/dist/testing/mock-host-contract.test.js +782 -0
- package/dist/types/field-type.d.ts +35 -18
- package/dist/types/field-type.js +4 -1
- package/dist/types/zod-types.d.ts +9 -0
- package/dist/types/zod-types.js +8 -2
- package/dist/utils.d.ts +1 -25
- package/dist/utils.js +10 -90
- package/package.json +9 -2
- package/testing/package.json +4 -0
|
@@ -0,0 +1,793 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const zod_1 = require("zod");
|
|
4
|
+
const userContext = require("../../context/user-context-singleton");
|
|
5
|
+
const contract_ref_1 = require("../../define/contract-ref");
|
|
6
|
+
const define_package_1 = require("../../define/define-package");
|
|
7
|
+
const events_1 = require("../../define/events");
|
|
8
|
+
const observables_1 = require("../../define/observables");
|
|
9
|
+
const table_1 = require("../../define/table");
|
|
10
|
+
const tool_1 = require("../../define/tool");
|
|
11
|
+
const events_2 = require("../../events");
|
|
12
|
+
const observable_1 = require("../../observable");
|
|
13
|
+
const peer_id_1 = require("../../peer-id");
|
|
14
|
+
const zod_types_1 = require("../../types/zod-types");
|
|
15
|
+
const contract_proxy_1 = require("../contract-proxy");
|
|
16
|
+
const contract_transport_1 = require("../contract-transport");
|
|
17
|
+
const host_contract_client_1 = require("../host-contract-client");
|
|
18
|
+
const hostTransport = require("../host-contract-transport");
|
|
19
|
+
const host_contract_transport_1 = require("../host-contract-transport");
|
|
20
|
+
const contractId = "00mvhostclient00000000001";
|
|
21
|
+
const widgetSchema = zod_1.z.object({ widgetId: zod_types_1.zodPeerId, label: zod_1.z.string() });
|
|
22
|
+
const Widgets = (0, table_1.defineTable)({
|
|
23
|
+
name: "Widgets",
|
|
24
|
+
description: "Widgets.",
|
|
25
|
+
primaryKeyName: "widgetId",
|
|
26
|
+
schema: widgetSchema,
|
|
27
|
+
});
|
|
28
|
+
const ping = (0, tool_1.defineTool)({
|
|
29
|
+
name: "ping",
|
|
30
|
+
usageDescription: "Echo.",
|
|
31
|
+
accessLevel: 0,
|
|
32
|
+
inputSchema: zod_1.z.object({ note: zod_1.z.string().optional() }),
|
|
33
|
+
outputSchema: zod_1.z.object({ label: zod_1.z.string(), note: zod_1.z.string().optional() }),
|
|
34
|
+
handler: () => ({ label: "" }),
|
|
35
|
+
});
|
|
36
|
+
const alert = (0, events_1.defineEvent)({
|
|
37
|
+
name: "alert",
|
|
38
|
+
description: "Something happened.",
|
|
39
|
+
payloadSchema: zod_1.z.object({ title: zod_1.z.string() }),
|
|
40
|
+
});
|
|
41
|
+
const count = (0, observables_1.defineObservable)({
|
|
42
|
+
name: "count",
|
|
43
|
+
description: "A writable count.",
|
|
44
|
+
schema: zod_1.z.number(),
|
|
45
|
+
writable: true,
|
|
46
|
+
source: (0, observable_1.observable)(0),
|
|
47
|
+
});
|
|
48
|
+
const status = (0, observables_1.defineObservable)({
|
|
49
|
+
name: "status",
|
|
50
|
+
description: "A read-only status.",
|
|
51
|
+
schema: zod_1.z.string(),
|
|
52
|
+
source: (0, observable_1.observable)("idle"),
|
|
53
|
+
});
|
|
54
|
+
const Main = (0, define_package_1.defineContract)({
|
|
55
|
+
contractId,
|
|
56
|
+
version: 1,
|
|
57
|
+
name: "Main",
|
|
58
|
+
tables: [Widgets],
|
|
59
|
+
tools: [ping],
|
|
60
|
+
events: [alert],
|
|
61
|
+
observables: [count, status],
|
|
62
|
+
});
|
|
63
|
+
const MainRef = (0, contract_ref_1.contractRef)(contractId, 1);
|
|
64
|
+
/** Let fire-and-forget requests and pushes land. */
|
|
65
|
+
const flush = async () => {
|
|
66
|
+
for (let i = 0; i < 5; i++) {
|
|
67
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
/** A provider session over its own transport, registered on `connect()`. */
|
|
71
|
+
function host(label, beforeCall, afterCall) {
|
|
72
|
+
const rows = [{ widgetId: (0, peer_id_1.newid)(), label: "first" }];
|
|
73
|
+
const changes = new events_2.Emitter(`widgets-${(0, peer_id_1.newid)()}`);
|
|
74
|
+
const alerts = new events_2.Emitter(`alerts-${(0, peer_id_1.newid)()}`);
|
|
75
|
+
const live = { count: (0, observable_1.observable)(0), status: (0, observable_1.observable)("idle") };
|
|
76
|
+
const table = {
|
|
77
|
+
dataChanged: changes.event,
|
|
78
|
+
list: async () => rows,
|
|
79
|
+
get: async (id) => rows.find((row) => row.widgetId === id),
|
|
80
|
+
count: async () => rows.length,
|
|
81
|
+
findOne: async () => rows[0],
|
|
82
|
+
save: async (row) => {
|
|
83
|
+
rows.push(row);
|
|
84
|
+
return row;
|
|
85
|
+
},
|
|
86
|
+
delete: async (id) => {
|
|
87
|
+
rows.splice(rows.findIndex((row) => row.widgetId === id), 1);
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
const [consumerSide, providerSide] = (0, contract_transport_1.inProcessTransportPair)();
|
|
91
|
+
const calls = [];
|
|
92
|
+
const recording = {
|
|
93
|
+
emit: async (channel, ...args) => {
|
|
94
|
+
if (channel === contract_transport_1.CONTRACT_REQUEST_CHANNEL) {
|
|
95
|
+
calls.push(args[0]);
|
|
96
|
+
await beforeCall?.(args[0]);
|
|
97
|
+
}
|
|
98
|
+
const result = await consumerSide.emit(channel, ...args);
|
|
99
|
+
return afterCall && channel === contract_transport_1.CONTRACT_REQUEST_CHANNEL
|
|
100
|
+
? afterCall(args[0], result)
|
|
101
|
+
: result;
|
|
102
|
+
},
|
|
103
|
+
on: (channel, handler) => consumerSide.on(channel, handler),
|
|
104
|
+
};
|
|
105
|
+
const session = (0, contract_proxy_1.createContractProviderSession)({
|
|
106
|
+
contractId,
|
|
107
|
+
version: 1,
|
|
108
|
+
tables: { Widgets: table },
|
|
109
|
+
tools: {
|
|
110
|
+
ping: {
|
|
111
|
+
tool: { name: "ping", usageDescription: "Echo.", accessLevel: 0 },
|
|
112
|
+
toolFn: async (input) => ({ label, ...input }),
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
events: { alert: alerts.event },
|
|
116
|
+
observables: live,
|
|
117
|
+
observableWritability: { count: true, status: false },
|
|
118
|
+
}, providerSide);
|
|
119
|
+
let unregister;
|
|
120
|
+
return {
|
|
121
|
+
label,
|
|
122
|
+
rows,
|
|
123
|
+
calls,
|
|
124
|
+
changes,
|
|
125
|
+
alerts,
|
|
126
|
+
...live,
|
|
127
|
+
connect: () => {
|
|
128
|
+
unregister = (0, host_contract_transport_1.registerHostContractTransport)(recording);
|
|
129
|
+
},
|
|
130
|
+
dispose: () => {
|
|
131
|
+
unregister?.();
|
|
132
|
+
session.dispose();
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
const cleanups = [];
|
|
137
|
+
afterEach(() => {
|
|
138
|
+
for (const cleanup of cleanups.splice(0).reverse()) {
|
|
139
|
+
cleanup();
|
|
140
|
+
}
|
|
141
|
+
jest.restoreAllMocks();
|
|
142
|
+
});
|
|
143
|
+
function setup(label = "a") {
|
|
144
|
+
const provider = host(label);
|
|
145
|
+
provider.connect();
|
|
146
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
147
|
+
cleanups.push(() => client.dispose(), () => provider.dispose());
|
|
148
|
+
return { provider, client };
|
|
149
|
+
}
|
|
150
|
+
describe("hostContract", () => {
|
|
151
|
+
test("calls tools and tables over the host transport, in the client's data context", async () => {
|
|
152
|
+
const { provider, client } = setup();
|
|
153
|
+
const pinged = await client.tools.ping({ note: "hi" });
|
|
154
|
+
expect(pinged).toEqual({ label: "a", note: "hi" });
|
|
155
|
+
expect(await client.tools.ping()).toEqual({ label: "a" });
|
|
156
|
+
const rows = await client.tables.Widgets.list({}, { pageSize: 5 });
|
|
157
|
+
expect(rows.map((row) => row.label)).toEqual(["first"]);
|
|
158
|
+
const added = await client.tables.Widgets.save({ widgetId: (0, peer_id_1.newid)(), label: "second" });
|
|
159
|
+
expect(added.label).toBe("second");
|
|
160
|
+
expect(await client.tables.Widgets.count()).toBe(2);
|
|
161
|
+
expect(await client.tables.Widgets.get("missing")).toBeUndefined();
|
|
162
|
+
await client.tables.Widgets.delete(added.widgetId);
|
|
163
|
+
expect(provider.rows).toHaveLength(1);
|
|
164
|
+
expect(provider.calls.map((call) => call.dataContextId)).toEqual(provider.calls.map(() => "group-1"));
|
|
165
|
+
expect(provider.calls.find((call) => call.operation === "list")?.args).toEqual([
|
|
166
|
+
{},
|
|
167
|
+
{ pageSize: 5 },
|
|
168
|
+
]);
|
|
169
|
+
});
|
|
170
|
+
test("a client can reach one provider", async () => {
|
|
171
|
+
const provider = host("a");
|
|
172
|
+
provider.connect();
|
|
173
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, {
|
|
174
|
+
dataContextId: async () => "group-1",
|
|
175
|
+
providerPackageId: "00mvprovider0000000000001",
|
|
176
|
+
});
|
|
177
|
+
cleanups.push(() => client.dispose(), () => provider.dispose());
|
|
178
|
+
await client.tools.ping();
|
|
179
|
+
await client.tables.Widgets.count();
|
|
180
|
+
expect(provider.calls.map((call) => call.providerPackageId)).toEqual([
|
|
181
|
+
"00mvprovider0000000000001",
|
|
182
|
+
"00mvprovider0000000000001",
|
|
183
|
+
]);
|
|
184
|
+
// A client for one provider isn't the shared one.
|
|
185
|
+
expect(client).not.toBe((0, host_contract_client_1.hostContract)(MainRef));
|
|
186
|
+
expect(() => (0, host_contract_client_1.hostContract)(MainRef, { providerPackageId: 3 })).toThrow(/package id/);
|
|
187
|
+
for (const providerPackageId of ["", " "]) {
|
|
188
|
+
expect(() => (0, host_contract_client_1.hostContract)(MainRef, { providerPackageId })).toThrow(/non-empty package id/);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
test("provider choice follows observable reads, writes, events, and reconnects", async () => {
|
|
192
|
+
const providerPackageId = (0, peer_id_1.newid)();
|
|
193
|
+
const first = host("first");
|
|
194
|
+
first.connect();
|
|
195
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, {
|
|
196
|
+
dataContextId: async () => "group-1",
|
|
197
|
+
providerPackageId,
|
|
198
|
+
});
|
|
199
|
+
const second = host("second");
|
|
200
|
+
cleanups.push(() => client.dispose(), () => first.dispose(), () => second.dispose());
|
|
201
|
+
const values = client.observables.count;
|
|
202
|
+
const alerts = [];
|
|
203
|
+
const subscription = client.events.alert.subscribe((alert) => alerts.push(alert.title));
|
|
204
|
+
await subscription.ready;
|
|
205
|
+
await flush();
|
|
206
|
+
expect(values()).toBe(0);
|
|
207
|
+
values(5);
|
|
208
|
+
await flush();
|
|
209
|
+
expect(first.count()).toBe(5);
|
|
210
|
+
await first.alerts.emit({ title: "first" });
|
|
211
|
+
expect(alerts).toEqual(["first"]);
|
|
212
|
+
second.count(8);
|
|
213
|
+
second.connect();
|
|
214
|
+
await flush();
|
|
215
|
+
expect(values()).toBe(8);
|
|
216
|
+
await second.alerts.emit({ title: "second" });
|
|
217
|
+
expect(alerts).toEqual(["first", "second"]);
|
|
218
|
+
subscription.unsubscribe();
|
|
219
|
+
client.dispose();
|
|
220
|
+
await flush();
|
|
221
|
+
for (const provider of [first, second]) {
|
|
222
|
+
expect(provider.calls.map((call) => [call.member, call.operation])).toEqual(expect.arrayContaining([
|
|
223
|
+
["observable", "subscribe"],
|
|
224
|
+
["observable", "get"],
|
|
225
|
+
["observable", "unsubscribe"],
|
|
226
|
+
["event", "subscribe"],
|
|
227
|
+
["event", "unsubscribe"],
|
|
228
|
+
]));
|
|
229
|
+
expect(provider.calls.every((call) => call.providerPackageId === providerPackageId)).toBe(true);
|
|
230
|
+
}
|
|
231
|
+
expect(first.calls).toEqual(expect.arrayContaining([expect.objectContaining({ member: "observable", operation: "set" })]));
|
|
232
|
+
});
|
|
233
|
+
test("an observable can await its initial snapshot, and disposal ends a pending load", async () => {
|
|
234
|
+
let release;
|
|
235
|
+
const pending = new Promise((resolve) => {
|
|
236
|
+
release = resolve;
|
|
237
|
+
});
|
|
238
|
+
const provider = host("delayed", async (call) => {
|
|
239
|
+
if (call.member === "observable" && call.operation === "get")
|
|
240
|
+
await pending;
|
|
241
|
+
});
|
|
242
|
+
provider.connect();
|
|
243
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
244
|
+
cleanups.push(() => client.dispose(), () => provider.dispose(), release);
|
|
245
|
+
const value = client.observables.count;
|
|
246
|
+
const loaded = jest.fn();
|
|
247
|
+
void value.loadingPromise.then(loaded);
|
|
248
|
+
await flush();
|
|
249
|
+
expect(value()).toBeUndefined();
|
|
250
|
+
expect(loaded).not.toHaveBeenCalled();
|
|
251
|
+
release();
|
|
252
|
+
await value.loadingPromise;
|
|
253
|
+
expect(value()).toBe(0);
|
|
254
|
+
expect(loaded).toHaveBeenCalledTimes(1);
|
|
255
|
+
provider.dispose();
|
|
256
|
+
const next = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
257
|
+
cleanups.push(() => next.dispose());
|
|
258
|
+
const status = next.observables.status;
|
|
259
|
+
next.dispose();
|
|
260
|
+
await expect(status.loadingPromise).resolves.toBeUndefined();
|
|
261
|
+
});
|
|
262
|
+
test("a failed initial observable read still settles loading", async () => {
|
|
263
|
+
const error = jest.spyOn(console, "error").mockImplementation(() => { });
|
|
264
|
+
const provider = host("failed", async (call) => {
|
|
265
|
+
if (call.member === "observable" && call.operation === "get")
|
|
266
|
+
throw new Error("offline");
|
|
267
|
+
});
|
|
268
|
+
provider.connect();
|
|
269
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
270
|
+
cleanups.push(() => error.mockRestore(), () => client.dispose(), () => provider.dispose());
|
|
271
|
+
const value = client.observables.status;
|
|
272
|
+
await value.loadingPromise;
|
|
273
|
+
expect(value()).toBeUndefined();
|
|
274
|
+
expect(error).toHaveBeenCalledWith("Contract observable initial get failed", expect.objectContaining({ message: "offline" }));
|
|
275
|
+
});
|
|
276
|
+
test("a call waits for the host connection, then fails after the timeout", async () => {
|
|
277
|
+
const provider = host("late");
|
|
278
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
279
|
+
cleanups.push(() => client.dispose(), () => provider.dispose());
|
|
280
|
+
const waiting = client.tools.ping();
|
|
281
|
+
await flush();
|
|
282
|
+
provider.connect();
|
|
283
|
+
await expect(waiting).resolves.toEqual({ label: "late" });
|
|
284
|
+
const impatient = (0, host_contract_client_1.hostContract)(MainRef, {
|
|
285
|
+
dataContextId: async () => "group-1",
|
|
286
|
+
timeoutMs: 10,
|
|
287
|
+
});
|
|
288
|
+
cleanups.push(() => impatient.dispose());
|
|
289
|
+
provider.dispose();
|
|
290
|
+
await expect(impatient.tools.ping()).rejects.toThrow(/Timed out|timed out|timeout/i);
|
|
291
|
+
});
|
|
292
|
+
test("table and event subscriptions follow a reconnect and report it", async () => {
|
|
293
|
+
const first = host("first");
|
|
294
|
+
first.connect();
|
|
295
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
296
|
+
cleanups.push(() => client.dispose(), () => first.dispose());
|
|
297
|
+
const changes = [];
|
|
298
|
+
const alerts = [];
|
|
299
|
+
const reconnects = [];
|
|
300
|
+
client.tables.Widgets.dataChanged.subscribe((event) => changes.push(`${event.op}:${event.dataObject.label}`), { onReconnect: () => reconnects.push("table") });
|
|
301
|
+
client.events.alert.subscribe((payload) => alerts.push(payload.title), {
|
|
302
|
+
onReconnect: () => reconnects.push("event"),
|
|
303
|
+
});
|
|
304
|
+
await flush();
|
|
305
|
+
await first.changes.emit({
|
|
306
|
+
op: "insert",
|
|
307
|
+
dataObject: { widgetId: (0, peer_id_1.newid)(), label: "one" },
|
|
308
|
+
source: "local",
|
|
309
|
+
});
|
|
310
|
+
await first.alerts.emit({ title: "one" });
|
|
311
|
+
await flush();
|
|
312
|
+
expect(changes).toEqual(["insert:one"]);
|
|
313
|
+
expect(alerts).toEqual(["one"]);
|
|
314
|
+
expect(reconnects).toEqual([]);
|
|
315
|
+
const second = host("second");
|
|
316
|
+
cleanups.push(() => second.dispose());
|
|
317
|
+
second.connect();
|
|
318
|
+
await flush();
|
|
319
|
+
expect(reconnects.sort()).toEqual(["event", "table"]);
|
|
320
|
+
await first.changes.emit({
|
|
321
|
+
op: "insert",
|
|
322
|
+
dataObject: { widgetId: (0, peer_id_1.newid)(), label: "stale" },
|
|
323
|
+
source: "local",
|
|
324
|
+
});
|
|
325
|
+
await second.changes.emit({
|
|
326
|
+
op: "update",
|
|
327
|
+
dataObject: { widgetId: (0, peer_id_1.newid)(), label: "two" },
|
|
328
|
+
source: "remote",
|
|
329
|
+
});
|
|
330
|
+
await second.alerts.emit({ title: "two" });
|
|
331
|
+
await flush();
|
|
332
|
+
expect(changes).toEqual(["insert:one", "update:two"]);
|
|
333
|
+
expect(alerts).toEqual(["one", "two"]);
|
|
334
|
+
expect(await client.tools.ping()).toEqual({ label: "second" });
|
|
335
|
+
});
|
|
336
|
+
test("an unsubscribed subscription stays off across a reconnect", async () => {
|
|
337
|
+
const first = host("first");
|
|
338
|
+
first.connect();
|
|
339
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
340
|
+
cleanups.push(() => client.dispose(), () => first.dispose());
|
|
341
|
+
const alerts = [];
|
|
342
|
+
const subscription = client.events.alert.subscribe((payload) => alerts.push(payload.title));
|
|
343
|
+
await flush();
|
|
344
|
+
expect(subscription.unsubscribe()).toBe(true);
|
|
345
|
+
expect(subscription.unsubscribe()).toBe(false);
|
|
346
|
+
const second = host("second");
|
|
347
|
+
cleanups.push(() => second.dispose());
|
|
348
|
+
second.connect();
|
|
349
|
+
await flush();
|
|
350
|
+
await second.alerts.emit({ title: "missed" });
|
|
351
|
+
await flush();
|
|
352
|
+
expect(alerts).toEqual([]);
|
|
353
|
+
expect(second.calls.filter((call) => call.member === "event")).toEqual([]);
|
|
354
|
+
});
|
|
355
|
+
test("observables mirror the provider, write through, and resync a refused write", async () => {
|
|
356
|
+
const { provider, client } = setup();
|
|
357
|
+
const countValue = client.observables.count;
|
|
358
|
+
await flush();
|
|
359
|
+
expect(countValue()).toBe(0);
|
|
360
|
+
provider.count(3);
|
|
361
|
+
await flush();
|
|
362
|
+
expect(countValue()).toBe(3);
|
|
363
|
+
countValue(7);
|
|
364
|
+
await flush();
|
|
365
|
+
expect(provider.count()).toBe(7);
|
|
366
|
+
const errors = jest.spyOn(console, "error").mockImplementation(() => undefined);
|
|
367
|
+
const statusValue = client.observables.status;
|
|
368
|
+
await flush();
|
|
369
|
+
expect(statusValue()).toBe("idle");
|
|
370
|
+
// @ts-expect-error a read-only declaration cannot be written by a typed caller
|
|
371
|
+
statusValue("hacked");
|
|
372
|
+
expect(statusValue()).toBe("hacked");
|
|
373
|
+
await flush();
|
|
374
|
+
expect(provider.status()).toBe("idle");
|
|
375
|
+
expect(statusValue()).toBe("idle");
|
|
376
|
+
expect(errors).toHaveBeenCalledWith("Contract observable set failed", expect.anything());
|
|
377
|
+
});
|
|
378
|
+
test("one observable's pending write does not block another observable", async () => {
|
|
379
|
+
const errors = jest.spyOn(console, "error").mockImplementation(() => undefined);
|
|
380
|
+
let release;
|
|
381
|
+
const provider = host("queues", async (call) => {
|
|
382
|
+
if (call.name === "count" && call.operation === "set") {
|
|
383
|
+
await new Promise((resolve) => {
|
|
384
|
+
release = resolve;
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
});
|
|
388
|
+
provider.connect();
|
|
389
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
390
|
+
cleanups.push(() => client.dispose(), () => provider.dispose());
|
|
391
|
+
const count = client.observables.count;
|
|
392
|
+
const status = client.observables.status;
|
|
393
|
+
await flush();
|
|
394
|
+
count(7);
|
|
395
|
+
await flush();
|
|
396
|
+
expect(release).toBeDefined();
|
|
397
|
+
// @ts-expect-error exercise provider refusal for an untyped caller
|
|
398
|
+
status("refused");
|
|
399
|
+
await flush();
|
|
400
|
+
expect(errors).toHaveBeenCalledWith("Contract observable set failed", expect.anything());
|
|
401
|
+
expect(status()).toBe("idle");
|
|
402
|
+
release?.();
|
|
403
|
+
await flush();
|
|
404
|
+
expect(provider.count()).toBe(7);
|
|
405
|
+
});
|
|
406
|
+
test("a refused-write reread cannot overwrite a newer provider push", async () => {
|
|
407
|
+
jest.spyOn(console, "error").mockImplementation(() => undefined);
|
|
408
|
+
let delay = false;
|
|
409
|
+
let release;
|
|
410
|
+
const provider = host("stale", undefined, async (call, result) => {
|
|
411
|
+
if (delay && call.operation === "get") {
|
|
412
|
+
await new Promise((resolve) => {
|
|
413
|
+
release = resolve;
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
return result;
|
|
417
|
+
});
|
|
418
|
+
provider.connect();
|
|
419
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
420
|
+
cleanups.push(() => client.dispose(), () => provider.dispose());
|
|
421
|
+
const value = client.observables.status;
|
|
422
|
+
await flush();
|
|
423
|
+
delay = true;
|
|
424
|
+
// @ts-expect-error exercise provider refusal for an untyped caller
|
|
425
|
+
value("refused");
|
|
426
|
+
await flush();
|
|
427
|
+
expect(release).toBeDefined();
|
|
428
|
+
provider.status("newer");
|
|
429
|
+
await flush();
|
|
430
|
+
expect(value()).toBe("newer");
|
|
431
|
+
release?.();
|
|
432
|
+
await flush();
|
|
433
|
+
expect(value()).toBe("newer");
|
|
434
|
+
});
|
|
435
|
+
test("observables read the new host's value after a reconnect", async () => {
|
|
436
|
+
const first = host("first");
|
|
437
|
+
first.connect();
|
|
438
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
439
|
+
cleanups.push(() => client.dispose(), () => first.dispose());
|
|
440
|
+
const seen = [];
|
|
441
|
+
client.observables.count.subscribe((value) => seen.push(value));
|
|
442
|
+
await flush();
|
|
443
|
+
const second = host("second");
|
|
444
|
+
second.count(42);
|
|
445
|
+
cleanups.push(() => second.dispose());
|
|
446
|
+
second.connect();
|
|
447
|
+
await flush();
|
|
448
|
+
expect(client.observables.count()).toBe(42);
|
|
449
|
+
second.count(43);
|
|
450
|
+
await flush();
|
|
451
|
+
expect(seen).toEqual([0, 42, 43]);
|
|
452
|
+
});
|
|
453
|
+
test("a write whose reply is lost with its connection does not hold later writes", async () => {
|
|
454
|
+
const errors = jest.spyOn(console, "error").mockImplementation(() => undefined);
|
|
455
|
+
// Like a dropped socket, the first host never answers the write.
|
|
456
|
+
const first = host("lost", (call) => call.member === "observable" && call.operation === "set"
|
|
457
|
+
? new Promise(() => undefined)
|
|
458
|
+
: Promise.resolve());
|
|
459
|
+
first.connect();
|
|
460
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
461
|
+
cleanups.push(() => client.dispose(), () => first.dispose());
|
|
462
|
+
const value = client.observables.count;
|
|
463
|
+
await flush();
|
|
464
|
+
value(5);
|
|
465
|
+
await flush();
|
|
466
|
+
const second = host("second");
|
|
467
|
+
cleanups.push(() => second.dispose());
|
|
468
|
+
second.connect();
|
|
469
|
+
await flush();
|
|
470
|
+
expect(errors).toHaveBeenCalledWith("Contract observable set failed", expect.objectContaining({ message: expect.stringMatching(/replaced/) }));
|
|
471
|
+
// The lost write's outcome is unknown, so the copy follows the new host.
|
|
472
|
+
expect(value()).toBe(0);
|
|
473
|
+
value(6);
|
|
474
|
+
await flush();
|
|
475
|
+
expect(second.count()).toBe(6);
|
|
476
|
+
expect(value()).toBe(6);
|
|
477
|
+
});
|
|
478
|
+
test("a call whose reply is lost with its connection fails, and the next call uses the new host", async () => {
|
|
479
|
+
// Like a dropped socket, the first host never answers.
|
|
480
|
+
const first = host("lost", (call) => call.member === "tool" || call.member === "table"
|
|
481
|
+
? new Promise(() => undefined)
|
|
482
|
+
: Promise.resolve());
|
|
483
|
+
first.connect();
|
|
484
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
485
|
+
cleanups.push(() => client.dispose(), () => first.dispose());
|
|
486
|
+
const tool = client.tools.ping({ note: "lost" });
|
|
487
|
+
const table = client.tables.Widgets.list();
|
|
488
|
+
const toolFailed = expect(tool).rejects.toThrow(/replaced/);
|
|
489
|
+
const tableFailed = expect(table).rejects.toThrow(/replaced/);
|
|
490
|
+
await flush();
|
|
491
|
+
const second = host("second");
|
|
492
|
+
cleanups.push(() => second.dispose());
|
|
493
|
+
second.connect();
|
|
494
|
+
await toolFailed;
|
|
495
|
+
await tableFailed;
|
|
496
|
+
await expect(client.tools.ping({ note: "again" })).resolves.toEqual({
|
|
497
|
+
label: "second",
|
|
498
|
+
note: "again",
|
|
499
|
+
});
|
|
500
|
+
});
|
|
501
|
+
test("a call waiting for its reply fails when the host disconnects", async () => {
|
|
502
|
+
const first = host("lost", (call) => call.member === "tool" ? new Promise(() => undefined) : Promise.resolve());
|
|
503
|
+
first.connect();
|
|
504
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
505
|
+
cleanups.push(() => client.dispose());
|
|
506
|
+
const tool = client.tools.ping();
|
|
507
|
+
const failed = expect(tool).rejects.toThrow(/replaced/);
|
|
508
|
+
await flush();
|
|
509
|
+
first.dispose();
|
|
510
|
+
await failed;
|
|
511
|
+
});
|
|
512
|
+
test("a reply already on its way when the connection is replaced still lands", async () => {
|
|
513
|
+
let release;
|
|
514
|
+
const gate = new Promise((resolve) => {
|
|
515
|
+
release = resolve;
|
|
516
|
+
});
|
|
517
|
+
const first = host("old", undefined, async (call, result) => {
|
|
518
|
+
if (call.member === "tool" || call.member === "table")
|
|
519
|
+
await gate;
|
|
520
|
+
return result;
|
|
521
|
+
});
|
|
522
|
+
first.connect();
|
|
523
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
524
|
+
const second = host("new");
|
|
525
|
+
cleanups.push(() => client.dispose(), () => first.dispose(), () => second.dispose());
|
|
526
|
+
const tool = client.tools.ping({ note: "ran" });
|
|
527
|
+
const table = client.tables.Widgets.list();
|
|
528
|
+
await flush();
|
|
529
|
+
second.connect();
|
|
530
|
+
// The provider ran the call and its answer arrives in the same turn: it isn't reported
|
|
531
|
+
// as lost, so a caller doesn't retry a call that succeeded.
|
|
532
|
+
release();
|
|
533
|
+
await expect(tool).resolves.toEqual({ label: "old", note: "ran" });
|
|
534
|
+
await expect(table).resolves.toEqual(first.rows);
|
|
535
|
+
});
|
|
536
|
+
test("writes queued on a replaced connection cannot overwrite the new host's value", async () => {
|
|
537
|
+
jest.spyOn(console, "error").mockImplementation(() => undefined);
|
|
538
|
+
const first = host("lost", (call) => call.member === "observable" && call.operation === "set"
|
|
539
|
+
? new Promise(() => undefined)
|
|
540
|
+
: Promise.resolve());
|
|
541
|
+
first.connect();
|
|
542
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
543
|
+
cleanups.push(() => client.dispose(), () => first.dispose());
|
|
544
|
+
const value = client.observables.count;
|
|
545
|
+
await flush();
|
|
546
|
+
value(5);
|
|
547
|
+
await flush();
|
|
548
|
+
value(6);
|
|
549
|
+
const second = host("second");
|
|
550
|
+
second.count(42);
|
|
551
|
+
cleanups.push(() => second.dispose());
|
|
552
|
+
second.connect();
|
|
553
|
+
await flush();
|
|
554
|
+
expect(second.calls.filter((call) => call.operation === "set")).toEqual([]);
|
|
555
|
+
expect(second.count()).toBe(42);
|
|
556
|
+
expect(value()).toBe(42);
|
|
557
|
+
value(7);
|
|
558
|
+
await flush();
|
|
559
|
+
expect(second.count()).toBe(7);
|
|
560
|
+
expect(value()).toBe(7);
|
|
561
|
+
});
|
|
562
|
+
test("after a disconnect, its queued writes are retired and writes made offline reach the next host", async () => {
|
|
563
|
+
jest.spyOn(console, "error").mockImplementation(() => undefined);
|
|
564
|
+
const first = host("dropped", (call) => call.member === "observable" && call.operation === "set"
|
|
565
|
+
? new Promise(() => undefined)
|
|
566
|
+
: Promise.resolve());
|
|
567
|
+
first.connect();
|
|
568
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
569
|
+
cleanups.push(() => client.dispose(), () => first.dispose());
|
|
570
|
+
const value = client.observables.count;
|
|
571
|
+
await flush();
|
|
572
|
+
value(5);
|
|
573
|
+
await flush();
|
|
574
|
+
value(6);
|
|
575
|
+
// As in Electron, the dropped transport is unregistered before the next one registers.
|
|
576
|
+
first.dispose();
|
|
577
|
+
value(7);
|
|
578
|
+
await flush();
|
|
579
|
+
const second = host("second");
|
|
580
|
+
second.count(42);
|
|
581
|
+
cleanups.push(() => second.dispose());
|
|
582
|
+
second.connect();
|
|
583
|
+
await flush();
|
|
584
|
+
const sets = second.calls.filter((call) => call.operation === "set");
|
|
585
|
+
expect(sets.map((call) => call.args?.[0])).toEqual([7]);
|
|
586
|
+
expect(second.count()).toBe(7);
|
|
587
|
+
expect(value()).toBe(7);
|
|
588
|
+
});
|
|
589
|
+
test("a refused-write reread lost with its connection does not hold later writes", async () => {
|
|
590
|
+
jest.spyOn(console, "error").mockImplementation(() => undefined);
|
|
591
|
+
let refuse = false;
|
|
592
|
+
const first = host("lost-reread", (call) => {
|
|
593
|
+
if (refuse && call.operation === "set")
|
|
594
|
+
return Promise.reject(new Error("refused"));
|
|
595
|
+
if (refuse && call.operation === "get")
|
|
596
|
+
return new Promise(() => undefined);
|
|
597
|
+
return Promise.resolve();
|
|
598
|
+
});
|
|
599
|
+
first.connect();
|
|
600
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
601
|
+
cleanups.push(() => client.dispose(), () => first.dispose());
|
|
602
|
+
const value = client.observables.count;
|
|
603
|
+
await flush();
|
|
604
|
+
refuse = true;
|
|
605
|
+
value(5);
|
|
606
|
+
await flush();
|
|
607
|
+
expect(first.calls.filter((call) => call.operation === "get")).toHaveLength(2);
|
|
608
|
+
const second = host("second");
|
|
609
|
+
second.count(42);
|
|
610
|
+
cleanups.push(() => second.dispose());
|
|
611
|
+
second.connect();
|
|
612
|
+
await flush();
|
|
613
|
+
expect(value()).toBe(42);
|
|
614
|
+
value(6);
|
|
615
|
+
await flush();
|
|
616
|
+
expect(second.count()).toBe(6);
|
|
617
|
+
expect(value()).toBe(6);
|
|
618
|
+
});
|
|
619
|
+
test("a write before the connection is sent once it connects", async () => {
|
|
620
|
+
const provider = host("late");
|
|
621
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
622
|
+
cleanups.push(() => client.dispose(), () => provider.dispose());
|
|
623
|
+
client.observables.count(5);
|
|
624
|
+
await flush();
|
|
625
|
+
provider.connect();
|
|
626
|
+
await flush();
|
|
627
|
+
expect(provider.count()).toBe(5);
|
|
628
|
+
expect(client.observables.count()).toBe(5);
|
|
629
|
+
});
|
|
630
|
+
test("writes while disconnected wait for the next mirror, and disposed mirrors refuse writes", async () => {
|
|
631
|
+
const { provider, client } = setup();
|
|
632
|
+
const value = client.observables.count;
|
|
633
|
+
await flush();
|
|
634
|
+
provider.dispose();
|
|
635
|
+
expect(() => value(9)).not.toThrow();
|
|
636
|
+
const second = host("second");
|
|
637
|
+
cleanups.push(() => second.dispose());
|
|
638
|
+
second.connect();
|
|
639
|
+
await flush();
|
|
640
|
+
expect(second.count()).toBe(9);
|
|
641
|
+
expect(value()).toBe(9);
|
|
642
|
+
client.dispose();
|
|
643
|
+
expect(() => value(10)).toThrow(/disposed/);
|
|
644
|
+
expect(value()).toBe(9);
|
|
645
|
+
});
|
|
646
|
+
test("dispose cancels pending calls", async () => {
|
|
647
|
+
jest.useFakeTimers();
|
|
648
|
+
try {
|
|
649
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
650
|
+
const waiting = client.tools.ping();
|
|
651
|
+
const rejected = expect(waiting).rejects.toThrow(/disposed/);
|
|
652
|
+
client.dispose();
|
|
653
|
+
// No need to wait for the ten-second connection timeout.
|
|
654
|
+
await rejected;
|
|
655
|
+
expect(jest.getTimerCount()).toBe(0);
|
|
656
|
+
}
|
|
657
|
+
finally {
|
|
658
|
+
jest.useRealTimers();
|
|
659
|
+
}
|
|
660
|
+
});
|
|
661
|
+
test("unsubscribing offline cancels its transport wait", async () => {
|
|
662
|
+
const wait = jest.spyOn(hostTransport, "waitForHostContractTransport");
|
|
663
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
664
|
+
cleanups.push(() => client.dispose());
|
|
665
|
+
const subscription = client.events.alert.subscribe(() => undefined);
|
|
666
|
+
const signal = wait.mock.calls[0][0]?.signal;
|
|
667
|
+
expect(signal?.aborted).toBe(false);
|
|
668
|
+
subscription.unsubscribe();
|
|
669
|
+
expect(signal?.aborted).toBe(true);
|
|
670
|
+
await expect(subscription.ready).resolves.toBe(false);
|
|
671
|
+
});
|
|
672
|
+
test("the default context uses the active group, then the personal context after reconnect", async () => {
|
|
673
|
+
let group = "group-1";
|
|
674
|
+
jest.spyOn(userContext, "getUserContext").mockResolvedValue({
|
|
675
|
+
userId: "user-1",
|
|
676
|
+
currentlyActiveGroupId: () => group,
|
|
677
|
+
});
|
|
678
|
+
const provider = host("default");
|
|
679
|
+
provider.connect();
|
|
680
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef);
|
|
681
|
+
cleanups.push(() => client.dispose(), () => provider.dispose());
|
|
682
|
+
await client.tools.ping();
|
|
683
|
+
expect(provider.calls.at(-1)?.dataContextId).toBe("group-1");
|
|
684
|
+
group = "";
|
|
685
|
+
// Re-registering even the same transport must resolve the context for the new connection.
|
|
686
|
+
provider.connect();
|
|
687
|
+
await client.tools.ping();
|
|
688
|
+
expect(provider.calls.at(-1)?.dataContextId).toBe("user-1");
|
|
689
|
+
});
|
|
690
|
+
test("a new connection does not wait for the previous connection's context lookup", async () => {
|
|
691
|
+
const first = host("first");
|
|
692
|
+
const second = host("second");
|
|
693
|
+
first.connect();
|
|
694
|
+
const resolveContext = jest
|
|
695
|
+
.fn()
|
|
696
|
+
.mockImplementationOnce(() => new Promise(() => undefined))
|
|
697
|
+
.mockResolvedValue("group-2");
|
|
698
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: resolveContext });
|
|
699
|
+
cleanups.push(() => client.dispose(), () => first.dispose(), () => second.dispose());
|
|
700
|
+
const pending = client.tools.ping();
|
|
701
|
+
second.connect();
|
|
702
|
+
await expect(pending).resolves.toEqual({ label: "second" });
|
|
703
|
+
expect(second.calls.at(-1)?.dataContextId).toBe("group-2");
|
|
704
|
+
});
|
|
705
|
+
test("reconnect is reported only after the remote subscription is live", async () => {
|
|
706
|
+
const { provider, client } = setup();
|
|
707
|
+
const reconnect = jest.fn();
|
|
708
|
+
client.tables.Widgets.dataChanged.subscribe(() => undefined, { onReconnect: reconnect });
|
|
709
|
+
await flush();
|
|
710
|
+
provider.dispose();
|
|
711
|
+
let release;
|
|
712
|
+
const second = host("slow", async (call) => {
|
|
713
|
+
if (call.operation === "subscribe")
|
|
714
|
+
await new Promise((resolve) => {
|
|
715
|
+
release = resolve;
|
|
716
|
+
});
|
|
717
|
+
});
|
|
718
|
+
cleanups.push(() => second.dispose());
|
|
719
|
+
second.connect();
|
|
720
|
+
await flush();
|
|
721
|
+
expect(release).toBeDefined();
|
|
722
|
+
expect(reconnect).not.toHaveBeenCalled();
|
|
723
|
+
release?.();
|
|
724
|
+
await flush();
|
|
725
|
+
expect(reconnect).toHaveBeenCalledTimes(1);
|
|
726
|
+
});
|
|
727
|
+
test("a subscription whose first attachment failed reports the next one as a reconnect", async () => {
|
|
728
|
+
jest.spyOn(console, "error").mockImplementation(() => undefined);
|
|
729
|
+
const first = host("first");
|
|
730
|
+
first.connect();
|
|
731
|
+
let failContext = true;
|
|
732
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, {
|
|
733
|
+
dataContextId: async () => {
|
|
734
|
+
if (failContext)
|
|
735
|
+
throw new Error("no context yet");
|
|
736
|
+
return "group-1";
|
|
737
|
+
},
|
|
738
|
+
});
|
|
739
|
+
cleanups.push(() => client.dispose(), () => first.dispose());
|
|
740
|
+
const alerts = [];
|
|
741
|
+
const reconnect = jest.fn();
|
|
742
|
+
const subscription = client.events.alert.subscribe((payload) => alerts.push(payload.title), {
|
|
743
|
+
onReconnect: reconnect,
|
|
744
|
+
});
|
|
745
|
+
await expect(subscription.ready).resolves.toBe(false);
|
|
746
|
+
failContext = false;
|
|
747
|
+
const second = host("second");
|
|
748
|
+
cleanups.push(() => second.dispose());
|
|
749
|
+
second.connect();
|
|
750
|
+
await flush();
|
|
751
|
+
expect(reconnect).toHaveBeenCalledTimes(1);
|
|
752
|
+
await second.alerts.emit({ title: "live" });
|
|
753
|
+
await flush();
|
|
754
|
+
expect(alerts).toEqual(["live"]);
|
|
755
|
+
});
|
|
756
|
+
test("dispose ends subscriptions and refuses calls", async () => {
|
|
757
|
+
const { provider, client } = setup();
|
|
758
|
+
const alerts = [];
|
|
759
|
+
client.events.alert.subscribe((payload) => alerts.push(payload.title));
|
|
760
|
+
await flush();
|
|
761
|
+
client.dispose();
|
|
762
|
+
client.dispose();
|
|
763
|
+
await provider.alerts.emit({ title: "after" });
|
|
764
|
+
await flush();
|
|
765
|
+
expect(alerts).toEqual([]);
|
|
766
|
+
await expect(client.tools.ping()).rejects.toThrow(/disposed/);
|
|
767
|
+
expect(() => client.events.alert.subscribe(() => undefined)).toThrow(/disposed/);
|
|
768
|
+
});
|
|
769
|
+
test("one shared client per contract, and only references", () => {
|
|
770
|
+
expect((0, host_contract_client_1.hostContract)(MainRef)).toBe((0, host_contract_client_1.hostContract)(MainRef));
|
|
771
|
+
expect((0, host_contract_client_1.hostContract)((0, contract_ref_1.contractRef)(contractId, 1))).toBe((0, host_contract_client_1.hostContract)(MainRef));
|
|
772
|
+
const other = (0, host_contract_client_1.hostContract)(MainRef);
|
|
773
|
+
other.dispose();
|
|
774
|
+
expect((0, host_contract_client_1.hostContract)(MainRef)).not.toBe(other);
|
|
775
|
+
(0, host_contract_client_1.hostContract)(MainRef).dispose();
|
|
776
|
+
expect(() => (0, host_contract_client_1.hostContract)({ contractId, version: 1 })).toThrow(/contractRef/);
|
|
777
|
+
// Awaiting or logging a client's members calls nothing.
|
|
778
|
+
expect(JSON.stringify((0, host_contract_client_1.hostContract)(MainRef, {}).tools)).toBe("{}");
|
|
779
|
+
});
|
|
780
|
+
test("members are typed from the contract", () => {
|
|
781
|
+
const client = (0, host_contract_client_1.hostContract)(MainRef, { dataContextId: async () => "group-1" });
|
|
782
|
+
cleanups.push(() => client.dispose());
|
|
783
|
+
// @ts-expect-error no such tool
|
|
784
|
+
void client.tools.missing;
|
|
785
|
+
// @ts-expect-error no such table
|
|
786
|
+
void client.tables.Gadgets;
|
|
787
|
+
// @ts-expect-error alert carries a title
|
|
788
|
+
client.events.alert.subscribe((payload) => payload);
|
|
789
|
+
// @ts-expect-error count is a number
|
|
790
|
+
const text = client.observables.count;
|
|
791
|
+
void text;
|
|
792
|
+
});
|
|
793
|
+
});
|