@peers-app/peers-sdk 0.19.13 → 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.
- package/dist/contracts/__tests__/builder.test.js +24 -0
- package/dist/contracts/__tests__/contract-events.test.d.ts +1 -0
- package/dist/contracts/__tests__/contract-events.test.js +199 -0
- package/dist/contracts/__tests__/contract-observables.test.d.ts +1 -0
- package/dist/contracts/__tests__/contract-observables.test.js +290 -0
- package/dist/contracts/__tests__/contract-provider-router.test.d.ts +1 -0
- package/dist/contracts/__tests__/contract-provider-router.test.js +193 -0
- package/dist/contracts/__tests__/contract-proxy.test.d.ts +1 -0
- package/dist/contracts/__tests__/contract-proxy.test.js +424 -0
- package/dist/contracts/__tests__/contract-resolvers.test.d.ts +1 -0
- package/dist/contracts/__tests__/contract-resolvers.test.js +148 -0
- package/dist/contracts/__tests__/contract-subscription-lifecycle.test.d.ts +1 -0
- package/dist/contracts/__tests__/contract-subscription-lifecycle.test.js +353 -0
- package/dist/contracts/__tests__/contract-table-events.test.d.ts +1 -0
- package/dist/contracts/__tests__/contract-table-events.test.js +209 -0
- package/dist/contracts/__tests__/contract-transport.test.d.ts +1 -0
- package/dist/contracts/__tests__/contract-transport.test.js +163 -0
- package/dist/contracts/__tests__/persistent-registry.test.d.ts +1 -0
- package/dist/contracts/__tests__/persistent-registry.test.js +109 -0
- package/dist/contracts/__tests__/playground.test.d.ts +1 -0
- package/dist/contracts/__tests__/playground.test.js +213 -0
- package/dist/contracts/__tests__/validate.test.js +122 -0
- package/dist/contracts/builder.d.ts +9 -1
- package/dist/contracts/builder.js +13 -0
- package/dist/contracts/contract-provider-router.d.ts +72 -0
- package/dist/contracts/contract-provider-router.js +164 -0
- package/dist/contracts/contract-proxy.d.ts +333 -0
- package/dist/contracts/contract-proxy.js +663 -0
- package/dist/contracts/contract-resolvers.d.ts +66 -0
- package/dist/contracts/contract-resolvers.js +138 -0
- package/dist/contracts/contract-transport.d.ts +94 -0
- package/dist/contracts/contract-transport.js +159 -0
- package/dist/contracts/contracts.table.d.ts +1 -1
- package/dist/contracts/contracts.table.js +2 -2
- package/dist/contracts/index.d.ts +6 -1
- package/dist/contracts/index.js +25 -1
- package/dist/contracts/persistent-registry.js +3 -1
- package/dist/contracts/system/logs-contract.d.ts +24 -0
- package/dist/contracts/system/logs-contract.js +60 -0
- package/dist/contracts/types.d.ts +27 -4
- package/dist/contracts/validate.d.ts +2 -2
- package/dist/contracts/validate.js +44 -2
- package/dist/data/orm/decorators.d.ts +6 -0
- package/dist/data/orm/decorators.js +21 -0
- package/dist/data/user-trust-levels.d.ts +2 -0
- package/dist/data/user-trust-levels.js +2 -1
- package/dist/data/user-trust-levels.test.d.ts +1 -0
- package/dist/data/user-trust-levels.test.js +37 -0
- package/dist/device/binary-peer-connection-v2.d.ts +2 -0
- package/dist/device/binary-peer-connection-v2.js +12 -2
- package/dist/device/binary-peer-connection-v2.test.js +23 -0
- package/dist/device/get-trust-level-fn.d.ts +6 -0
- package/dist/device/get-trust-level-fn.js +12 -7
- package/dist/device/get-trust-level-fn.test.js +16 -2
- package/dist/logging/console-logs.table.d.ts +3 -0
- package/dist/logging/console-logs.table.js +2 -1
- package/dist/system-ids.d.ts +11 -0
- package/dist/system-ids.js +12 -1
- package/package.json +1 -1
|
@@ -208,6 +208,30 @@ describe("definePackage", () => {
|
|
|
208
208
|
expect(result.contracts[0].observables).toHaveLength(1);
|
|
209
209
|
expect(result.contracts[0].observables[0].name).toBe("taskCount");
|
|
210
210
|
});
|
|
211
|
+
it("emits contract events without sharing or mutating the caller's array", () => {
|
|
212
|
+
const eventContractId = (0, utils_1.newid)();
|
|
213
|
+
const events = [
|
|
214
|
+
{
|
|
215
|
+
name: "taskChanged",
|
|
216
|
+
description: "A task changed",
|
|
217
|
+
payloadFields: [{ name: "taskId", type: field_type_1.FieldType.id }],
|
|
218
|
+
},
|
|
219
|
+
];
|
|
220
|
+
const result = (0, builder_1.definePackage)((pkg) => {
|
|
221
|
+
pkg.packageId = TEST_PKG_ID;
|
|
222
|
+
const contract = pkg.contract(eventContractId, 1, "Event Contract");
|
|
223
|
+
contract.events = events;
|
|
224
|
+
});
|
|
225
|
+
const emittedEvents = result.contracts[0].events;
|
|
226
|
+
expect(emittedEvents).toEqual(events);
|
|
227
|
+
expect(emittedEvents).not.toBe(events);
|
|
228
|
+
emittedEvents?.push({
|
|
229
|
+
name: "otherEvent",
|
|
230
|
+
description: "Another event",
|
|
231
|
+
payloadFields: [],
|
|
232
|
+
});
|
|
233
|
+
expect(events).toHaveLength(1);
|
|
234
|
+
});
|
|
211
235
|
it("uses provided name and description on the contract definition", () => {
|
|
212
236
|
const cid = (0, utils_1.newid)();
|
|
213
237
|
const result = (0, builder_1.definePackage)((pkg) => {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const events_1 = require("../../events");
|
|
4
|
+
const field_type_1 = require("../../types/field-type");
|
|
5
|
+
const utils_1 = require("../../utils");
|
|
6
|
+
const contract_proxy_1 = require("../contract-proxy");
|
|
7
|
+
const contract_transport_1 = require("../contract-transport");
|
|
8
|
+
/** Contract-member name of the single event under test (distinct from the live bus name). */
|
|
9
|
+
const EVENT_NAME = "widgetChanged";
|
|
10
|
+
/** Flush pending micro/macrotasks so fire-and-forget subscribe requests land. */
|
|
11
|
+
const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
12
|
+
/** A minimal contract that declares exactly one event. */
|
|
13
|
+
function makeContract(contractId) {
|
|
14
|
+
return {
|
|
15
|
+
contractId,
|
|
16
|
+
version: 1,
|
|
17
|
+
devTag: "dev",
|
|
18
|
+
name: "Events Test Contract",
|
|
19
|
+
description: "For contract event proxy tests",
|
|
20
|
+
tables: [],
|
|
21
|
+
tools: [],
|
|
22
|
+
observables: [],
|
|
23
|
+
events: [
|
|
24
|
+
{
|
|
25
|
+
name: EVENT_NAME,
|
|
26
|
+
description: "Fires when a widget changes",
|
|
27
|
+
payloadFields: [{ name: "widgetId", type: field_type_1.FieldType.id, description: "ID" }],
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Wire a consumer to a provider session over an in-process transport pair. The live
|
|
34
|
+
* event uses a unique global bus name (via `newid()`) so subscriptions never collide
|
|
35
|
+
* across tests, while the contract-member name stays stable as {@link EVENT_NAME}.
|
|
36
|
+
*/
|
|
37
|
+
function wire(opts = {}) {
|
|
38
|
+
const contractId = (0, utils_1.newid)();
|
|
39
|
+
const emitter = new events_1.Emitter((0, utils_1.newid)());
|
|
40
|
+
const resolution = {
|
|
41
|
+
contractId,
|
|
42
|
+
version: 1,
|
|
43
|
+
events: { [EVENT_NAME]: emitter.event },
|
|
44
|
+
};
|
|
45
|
+
const [consumerTransport, providerTransport] = (0, contract_transport_1.inProcessTransportPair)();
|
|
46
|
+
const session = (0, contract_proxy_1.createContractProviderSession)(resolution, providerTransport, {
|
|
47
|
+
permissionCheck: opts.permissionCheck,
|
|
48
|
+
context: opts.context,
|
|
49
|
+
});
|
|
50
|
+
const consumer = (0, contract_proxy_1.createContractConsumer)(makeContract(contractId), consumerTransport);
|
|
51
|
+
return { consumer, session, emitter, contractId, consumerTransport, providerTransport };
|
|
52
|
+
}
|
|
53
|
+
describe("contract events — end-to-end", () => {
|
|
54
|
+
it("delivers a payload to a subscribed consumer", async () => {
|
|
55
|
+
const { consumer, session, emitter } = wire();
|
|
56
|
+
const received = [];
|
|
57
|
+
consumer.events[EVENT_NAME].subscribe((v) => received.push(v));
|
|
58
|
+
await flush();
|
|
59
|
+
await emitter.emit({ widgetId: "w1" });
|
|
60
|
+
expect(received).toEqual([{ widgetId: "w1" }]);
|
|
61
|
+
session.dispose();
|
|
62
|
+
consumer.dispose();
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
describe("contract events — subscription gating", () => {
|
|
66
|
+
it("pushes nothing on the notify channel when nobody is subscribed", async () => {
|
|
67
|
+
const { emitter, providerTransport, session, consumer } = wire();
|
|
68
|
+
const notifySpy = jest.spyOn(providerTransport, "emit");
|
|
69
|
+
await emitter.emit({ widgetId: "w0" });
|
|
70
|
+
expect(notifySpy).not.toHaveBeenCalled();
|
|
71
|
+
session.dispose();
|
|
72
|
+
consumer.dispose();
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
describe("contract events — unsubscribe + refcount", () => {
|
|
76
|
+
it("stops delivering after unsubscribe and detaches the live subscription", async () => {
|
|
77
|
+
const { consumer, session, emitter, providerTransport } = wire();
|
|
78
|
+
const originalSubscribe = emitter.event.subscribe;
|
|
79
|
+
const liveUnsubscribe = jest.fn();
|
|
80
|
+
jest.spyOn(emitter.event, "subscribe").mockImplementation((handler) => {
|
|
81
|
+
const subscription = originalSubscribe(handler);
|
|
82
|
+
return {
|
|
83
|
+
unsubscribe: () => {
|
|
84
|
+
liveUnsubscribe();
|
|
85
|
+
return subscription.unsubscribe();
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
const received = [];
|
|
90
|
+
const sub = consumer.events[EVENT_NAME].subscribe((v) => received.push(v));
|
|
91
|
+
await flush();
|
|
92
|
+
await emitter.emit({ widgetId: "a" });
|
|
93
|
+
expect(received).toEqual([{ widgetId: "a" }]);
|
|
94
|
+
const notifySpy = jest.spyOn(providerTransport, "emit");
|
|
95
|
+
sub.unsubscribe();
|
|
96
|
+
await flush();
|
|
97
|
+
// Refcount reached 0, so the provider detached its live handler: no further pushes.
|
|
98
|
+
expect(liveUnsubscribe).toHaveBeenCalledTimes(1);
|
|
99
|
+
await emitter.emit({ widgetId: "b" });
|
|
100
|
+
expect(notifySpy).not.toHaveBeenCalled();
|
|
101
|
+
expect(received).toEqual([{ widgetId: "a" }]);
|
|
102
|
+
session.dispose();
|
|
103
|
+
consumer.dispose();
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
describe("contract events — fan-out", () => {
|
|
107
|
+
it("delivers to multiple subscribers through a single live subscription", async () => {
|
|
108
|
+
const { consumer, session, emitter, providerTransport } = wire();
|
|
109
|
+
const liveSubscribeSpy = jest.spyOn(emitter.event, "subscribe");
|
|
110
|
+
const a = [];
|
|
111
|
+
const b = [];
|
|
112
|
+
consumer.events[EVENT_NAME].subscribe((v) => a.push(v));
|
|
113
|
+
consumer.events[EVENT_NAME].subscribe((v) => b.push(v));
|
|
114
|
+
await flush();
|
|
115
|
+
// One live subscription is shared across both consumer subscriptions (refcount).
|
|
116
|
+
expect(liveSubscribeSpy).toHaveBeenCalledTimes(1);
|
|
117
|
+
const notifySpy = jest.spyOn(providerTransport, "emit");
|
|
118
|
+
await emitter.emit({ widgetId: "x" });
|
|
119
|
+
expect(a).toEqual([{ widgetId: "x" }]);
|
|
120
|
+
expect(b).toEqual([{ widgetId: "x" }]);
|
|
121
|
+
// One push per subscriptionId fanned out from the single emit.
|
|
122
|
+
expect(notifySpy).toHaveBeenCalledTimes(2);
|
|
123
|
+
session.dispose();
|
|
124
|
+
consumer.dispose();
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
describe("contract events — missing provider member", () => {
|
|
128
|
+
it("reports a consumer subscribe failure when the resolution omits a declared event", async () => {
|
|
129
|
+
const consoleError = jest.spyOn(console, "error").mockImplementation();
|
|
130
|
+
const contractId = (0, utils_1.newid)();
|
|
131
|
+
const [consumerTransport, providerTransport] = (0, contract_transport_1.inProcessTransportPair)();
|
|
132
|
+
const session = (0, contract_proxy_1.createContractProviderSession)({ contractId, version: 1, events: {} }, providerTransport);
|
|
133
|
+
const consumer = (0, contract_proxy_1.createContractConsumer)(makeContract(contractId), consumerTransport);
|
|
134
|
+
const subscription = consumer.events[EVENT_NAME].subscribe(() => { });
|
|
135
|
+
await flush();
|
|
136
|
+
expect(subscription.unsubscribe()).toBe(false);
|
|
137
|
+
expect(consoleError).toHaveBeenCalledWith("Contract subscribe failed", expect.objectContaining({
|
|
138
|
+
message: expect.stringMatching(/does not provide event 'widgetChanged'/),
|
|
139
|
+
}));
|
|
140
|
+
consumer.dispose();
|
|
141
|
+
session.dispose();
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
describe("contract events — permission gating", () => {
|
|
145
|
+
it("rejects a denied subscribe and attaches no live subscription", async () => {
|
|
146
|
+
const localUserId = (0, utils_1.newid)();
|
|
147
|
+
const { consumerTransport, providerTransport, emitter, contractId, session, consumer } = wire({
|
|
148
|
+
permissionCheck: contract_proxy_1.sameUserContractPermissionCheck,
|
|
149
|
+
context: { localUserId, callerUserId: (0, utils_1.newid)() },
|
|
150
|
+
});
|
|
151
|
+
const call = {
|
|
152
|
+
contractId,
|
|
153
|
+
version: 1,
|
|
154
|
+
member: "event",
|
|
155
|
+
name: EVENT_NAME,
|
|
156
|
+
operation: "subscribe",
|
|
157
|
+
subscriptionId: (0, utils_1.newid)(),
|
|
158
|
+
args: [],
|
|
159
|
+
};
|
|
160
|
+
await expect(consumerTransport.emit(contract_transport_1.CONTRACT_REQUEST_CHANNEL, call)).rejects.toThrow(/Permission denied/);
|
|
161
|
+
// No live subscription attached, so emitting the event pushes nothing.
|
|
162
|
+
const notifySpy = jest.spyOn(providerTransport, "emit");
|
|
163
|
+
await emitter.emit({ widgetId: "z" });
|
|
164
|
+
expect(notifySpy).not.toHaveBeenCalled();
|
|
165
|
+
session.dispose();
|
|
166
|
+
consumer.dispose();
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
describe("contract events — teardown", () => {
|
|
170
|
+
it("session.dispose() detaches live subscriptions and stops pushes", async () => {
|
|
171
|
+
const { consumer, session, emitter, providerTransport } = wire();
|
|
172
|
+
const received = [];
|
|
173
|
+
consumer.events[EVENT_NAME].subscribe((v) => received.push(v));
|
|
174
|
+
await flush();
|
|
175
|
+
await emitter.emit({ widgetId: "1" });
|
|
176
|
+
expect(received).toEqual([{ widgetId: "1" }]);
|
|
177
|
+
const notifySpy = jest.spyOn(providerTransport, "emit");
|
|
178
|
+
session.dispose();
|
|
179
|
+
await emitter.emit({ widgetId: "2" });
|
|
180
|
+
expect(notifySpy).not.toHaveBeenCalled();
|
|
181
|
+
expect(received).toEqual([{ widgetId: "1" }]);
|
|
182
|
+
consumer.dispose();
|
|
183
|
+
});
|
|
184
|
+
it("consumer.dispose() unsubscribes the provider and drops later pushes", async () => {
|
|
185
|
+
const { consumer, session, emitter, providerTransport } = wire();
|
|
186
|
+
const received = [];
|
|
187
|
+
consumer.events[EVENT_NAME].subscribe((v) => received.push(v));
|
|
188
|
+
await flush();
|
|
189
|
+
await emitter.emit({ widgetId: "1" });
|
|
190
|
+
expect(received).toEqual([{ widgetId: "1" }]);
|
|
191
|
+
const notifySpy = jest.spyOn(providerTransport, "emit");
|
|
192
|
+
consumer.dispose();
|
|
193
|
+
await flush();
|
|
194
|
+
await emitter.emit({ widgetId: "2" });
|
|
195
|
+
expect(notifySpy).not.toHaveBeenCalled();
|
|
196
|
+
expect(received).toEqual([{ widgetId: "1" }]);
|
|
197
|
+
session.dispose();
|
|
198
|
+
});
|
|
199
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const observable_1 = require("../../observable");
|
|
4
|
+
const field_type_1 = require("../../types/field-type");
|
|
5
|
+
const utils_1 = require("../../utils");
|
|
6
|
+
const contract_proxy_1 = require("../contract-proxy");
|
|
7
|
+
const contract_transport_1 = require("../contract-transport");
|
|
8
|
+
/** Default contract-member name of the observable under test. */
|
|
9
|
+
const OBS_NAME = "widgetCount";
|
|
10
|
+
/** Flush pending micro/macrotasks so fire-and-forget subscribe/set requests land. */
|
|
11
|
+
const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
12
|
+
function deferred() {
|
|
13
|
+
let resolve;
|
|
14
|
+
const promise = new Promise((resolvePromise) => {
|
|
15
|
+
resolve = resolvePromise;
|
|
16
|
+
});
|
|
17
|
+
return { promise, resolve };
|
|
18
|
+
}
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
jest.restoreAllMocks();
|
|
21
|
+
});
|
|
22
|
+
/** A minimal contract declaring one observable per supplied name (no tables/tools/events). */
|
|
23
|
+
function makeContract(contractId, names, writability) {
|
|
24
|
+
return {
|
|
25
|
+
contractId,
|
|
26
|
+
version: 1,
|
|
27
|
+
devTag: "dev",
|
|
28
|
+
name: "Observables Test Contract",
|
|
29
|
+
description: "For contract observable proxy tests",
|
|
30
|
+
tables: [],
|
|
31
|
+
tools: [],
|
|
32
|
+
observables: names.map((name) => ({
|
|
33
|
+
name,
|
|
34
|
+
description: `Observable ${name}`,
|
|
35
|
+
valueType: field_type_1.FieldType.number,
|
|
36
|
+
writable: writability[name],
|
|
37
|
+
})),
|
|
38
|
+
events: [],
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Wire a consumer to a provider session over an in-process transport pair. Each entry in
|
|
43
|
+
* `values` becomes a live provider {@link Observable} seeded with that initial value; the
|
|
44
|
+
* returned `live` map exposes them so a test can drive provider-side changes and assert
|
|
45
|
+
* durability. The consumer subscribes to every observable implicitly at construction.
|
|
46
|
+
*/
|
|
47
|
+
function wire(opts = {}) {
|
|
48
|
+
const values = opts.values ?? { [OBS_NAME]: 0 };
|
|
49
|
+
const contractId = (0, utils_1.newid)();
|
|
50
|
+
const writability = Object.fromEntries(Object.keys(values).map((name) => [name, opts.writability?.[name] ?? true]));
|
|
51
|
+
const live = {};
|
|
52
|
+
for (const [name, value] of Object.entries(values)) {
|
|
53
|
+
live[name] = (0, observable_1.observable)(value);
|
|
54
|
+
}
|
|
55
|
+
const resolution = {
|
|
56
|
+
contractId,
|
|
57
|
+
version: 1,
|
|
58
|
+
observables: live,
|
|
59
|
+
observableWritability: writability,
|
|
60
|
+
};
|
|
61
|
+
const [consumerTransport, providerTransport] = (0, contract_transport_1.inProcessTransportPair)();
|
|
62
|
+
const session = (0, contract_proxy_1.createContractProviderSession)(resolution, providerTransport, {
|
|
63
|
+
permissionCheck: opts.permissionCheck,
|
|
64
|
+
context: opts.context,
|
|
65
|
+
});
|
|
66
|
+
const consumer = (0, contract_proxy_1.createContractConsumer)(makeContract(contractId, Object.keys(values), writability), consumerTransport);
|
|
67
|
+
return { consumer, session, live, contractId, consumerTransport, providerTransport };
|
|
68
|
+
}
|
|
69
|
+
describe("contract observables — initial snapshot", () => {
|
|
70
|
+
it("exposes the provider's initial value synchronously after loadingPromise resolves", async () => {
|
|
71
|
+
const { consumer, session } = wire({ values: { [OBS_NAME]: 42 } });
|
|
72
|
+
await consumer.loadingPromise;
|
|
73
|
+
expect(consumer.observables[OBS_NAME]()).toBe(42);
|
|
74
|
+
session.dispose();
|
|
75
|
+
consumer.dispose();
|
|
76
|
+
});
|
|
77
|
+
it("keeps a provider push that arrives before a stale initial get", async () => {
|
|
78
|
+
const contractId = (0, utils_1.newid)();
|
|
79
|
+
const live = (0, observable_1.observable)(0);
|
|
80
|
+
const staleGet = deferred();
|
|
81
|
+
const [baseConsumerTransport, providerTransport] = (0, contract_transport_1.inProcessTransportPair)();
|
|
82
|
+
const consumerTransport = {
|
|
83
|
+
emit: (channel, ...args) => {
|
|
84
|
+
const call = args[0];
|
|
85
|
+
if (channel === contract_transport_1.CONTRACT_REQUEST_CHANNEL && call.operation === "get") {
|
|
86
|
+
return staleGet.promise;
|
|
87
|
+
}
|
|
88
|
+
return baseConsumerTransport.emit(channel, ...args);
|
|
89
|
+
},
|
|
90
|
+
on: (channel, handler) => baseConsumerTransport.on(channel, handler),
|
|
91
|
+
};
|
|
92
|
+
const session = (0, contract_proxy_1.createContractProviderSession)({
|
|
93
|
+
contractId,
|
|
94
|
+
version: 1,
|
|
95
|
+
observables: { [OBS_NAME]: live },
|
|
96
|
+
observableWritability: { [OBS_NAME]: true },
|
|
97
|
+
}, providerTransport);
|
|
98
|
+
const consumer = (0, contract_proxy_1.createContractConsumer)(makeContract(contractId, [OBS_NAME], { [OBS_NAME]: true }), consumerTransport);
|
|
99
|
+
live(5);
|
|
100
|
+
staleGet.resolve(0);
|
|
101
|
+
await consumer.loadingPromise;
|
|
102
|
+
expect(consumer.observables[OBS_NAME]()).toBe(5);
|
|
103
|
+
consumer.dispose();
|
|
104
|
+
session.dispose();
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
describe("contract observables — always-on streaming", () => {
|
|
108
|
+
it("streams provider changes to the mirror with no explicit subscribe", async () => {
|
|
109
|
+
const { consumer, session, live } = wire({ values: { [OBS_NAME]: 0 } });
|
|
110
|
+
await consumer.loadingPromise;
|
|
111
|
+
// Provider-side change flows over the notify channel to the local mirror.
|
|
112
|
+
live[OBS_NAME](5);
|
|
113
|
+
await flush();
|
|
114
|
+
expect(consumer.observables[OBS_NAME]()).toBe(5);
|
|
115
|
+
live[OBS_NAME](6);
|
|
116
|
+
await flush();
|
|
117
|
+
expect(consumer.observables[OBS_NAME]()).toBe(6);
|
|
118
|
+
session.dispose();
|
|
119
|
+
consumer.dispose();
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
describe("contract observables — writes", () => {
|
|
123
|
+
it("forwards a consumer write to the live observable (awaitable via loadingPromise)", async () => {
|
|
124
|
+
const { consumer, session, live } = wire({ values: { [OBS_NAME]: 0 } });
|
|
125
|
+
await consumer.loadingPromise;
|
|
126
|
+
// The mirror updates synchronously (optimistic) and a fire-and-forget `set` is sent.
|
|
127
|
+
consumer.observables[OBS_NAME](9);
|
|
128
|
+
expect(consumer.observables[OBS_NAME]()).toBe(9);
|
|
129
|
+
// Awaiting loadingPromise waits for the write to reach the live observable (durability).
|
|
130
|
+
await consumer.loadingPromise;
|
|
131
|
+
expect(live[OBS_NAME]()).toBe(9);
|
|
132
|
+
session.dispose();
|
|
133
|
+
consumer.dispose();
|
|
134
|
+
});
|
|
135
|
+
it("rejects read-only writes on both consumer and provider boundaries", async () => {
|
|
136
|
+
const { consumer, session, live, consumerTransport, contractId } = wire({
|
|
137
|
+
values: { [OBS_NAME]: 0 },
|
|
138
|
+
writability: { [OBS_NAME]: false },
|
|
139
|
+
});
|
|
140
|
+
await consumer.loadingPromise;
|
|
141
|
+
const emitSpy = jest.spyOn(consumerTransport, "emit");
|
|
142
|
+
expect(() => consumer.observables[OBS_NAME](9)).toThrow(/read-only/);
|
|
143
|
+
expect(consumer.observables[OBS_NAME]()).toBe(0);
|
|
144
|
+
expect(emitSpy).not.toHaveBeenCalled();
|
|
145
|
+
await expect(consumerTransport.emit(contract_transport_1.CONTRACT_REQUEST_CHANNEL, {
|
|
146
|
+
contractId,
|
|
147
|
+
version: 1,
|
|
148
|
+
member: "observable",
|
|
149
|
+
name: OBS_NAME,
|
|
150
|
+
operation: "set",
|
|
151
|
+
args: [9],
|
|
152
|
+
})).rejects.toThrow(/read-only/);
|
|
153
|
+
expect(live[OBS_NAME]()).toBe(0);
|
|
154
|
+
consumer.dispose();
|
|
155
|
+
session.dispose();
|
|
156
|
+
});
|
|
157
|
+
it("logs a failed writable set once and reconciles on the next provider push", async () => {
|
|
158
|
+
const consoleError = jest.spyOn(console, "error").mockImplementation();
|
|
159
|
+
const { consumer, session, live } = wire({
|
|
160
|
+
values: { [OBS_NAME]: 0 },
|
|
161
|
+
permissionCheck: (call) => call.operation !== "set",
|
|
162
|
+
});
|
|
163
|
+
await consumer.loadingPromise;
|
|
164
|
+
consumer.observables[OBS_NAME](9);
|
|
165
|
+
expect(consumer.observables[OBS_NAME]()).toBe(9);
|
|
166
|
+
await expect(consumer.loadingPromise).resolves.toBeUndefined();
|
|
167
|
+
expect(live[OBS_NAME]()).toBe(0);
|
|
168
|
+
expect(consoleError).toHaveBeenCalledTimes(1);
|
|
169
|
+
expect(consoleError).toHaveBeenCalledWith("Contract observable set failed", expect.objectContaining({ message: expect.stringMatching(/Permission denied/) }));
|
|
170
|
+
live[OBS_NAME](4);
|
|
171
|
+
await flush();
|
|
172
|
+
expect(consumer.observables[OBS_NAME]()).toBe(4);
|
|
173
|
+
consumer.dispose();
|
|
174
|
+
session.dispose();
|
|
175
|
+
});
|
|
176
|
+
it("serializes multiple writes through loadingPromise", async () => {
|
|
177
|
+
const gates = [deferred(), deferred()];
|
|
178
|
+
const starts = [deferred(), deferred()];
|
|
179
|
+
const seen = [];
|
|
180
|
+
let setIndex = 0;
|
|
181
|
+
const { consumer, session, live } = wire({
|
|
182
|
+
values: { [OBS_NAME]: 0 },
|
|
183
|
+
permissionCheck: (call) => {
|
|
184
|
+
if (call.operation !== "set") {
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
const index = setIndex++;
|
|
188
|
+
seen.push(call.args[0]);
|
|
189
|
+
starts[index].resolve();
|
|
190
|
+
return gates[index].promise;
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
await consumer.loadingPromise;
|
|
194
|
+
consumer.observables[OBS_NAME](1);
|
|
195
|
+
consumer.observables[OBS_NAME](2);
|
|
196
|
+
await starts[0].promise;
|
|
197
|
+
expect(seen).toEqual([1]);
|
|
198
|
+
gates[0].resolve(true);
|
|
199
|
+
await starts[1].promise;
|
|
200
|
+
expect(seen).toEqual([1, 2]);
|
|
201
|
+
gates[1].resolve(true);
|
|
202
|
+
await consumer.loadingPromise;
|
|
203
|
+
expect(live[OBS_NAME]()).toBe(2);
|
|
204
|
+
expect(consumer.observables[OBS_NAME]()).toBe(2);
|
|
205
|
+
consumer.dispose();
|
|
206
|
+
session.dispose();
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
describe("contract observables — no echo loop", () => {
|
|
210
|
+
it("does not re-send a set when applying a provider push", async () => {
|
|
211
|
+
const { consumer, session, live, consumerTransport } = wire({ values: { [OBS_NAME]: 0 } });
|
|
212
|
+
await consumer.loadingPromise;
|
|
213
|
+
const emitSpy = jest.spyOn(consumerTransport, "emit");
|
|
214
|
+
live[OBS_NAME](3);
|
|
215
|
+
await flush();
|
|
216
|
+
expect(consumer.observables[OBS_NAME]()).toBe(3);
|
|
217
|
+
// Applying the push must not bounce a `set` back to the provider.
|
|
218
|
+
const setCalls = emitSpy.mock.calls.filter(([channel, call]) => channel === contract_transport_1.CONTRACT_REQUEST_CHANNEL && call.operation === "set");
|
|
219
|
+
expect(setCalls).toHaveLength(0);
|
|
220
|
+
session.dispose();
|
|
221
|
+
consumer.dispose();
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
describe("contract observables — shared loadingPromise", () => {
|
|
225
|
+
it("resolves one proxy-level loadingPromise once every observable snapshot has loaded", async () => {
|
|
226
|
+
const { consumer, session } = wire({ values: { widgetCount: 1, gadgetCount: 2 } });
|
|
227
|
+
await consumer.loadingPromise;
|
|
228
|
+
expect(consumer.observables.widgetCount()).toBe(1);
|
|
229
|
+
expect(consumer.observables.gadgetCount()).toBe(2);
|
|
230
|
+
session.dispose();
|
|
231
|
+
consumer.dispose();
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
describe("contract observables — permission gating", () => {
|
|
235
|
+
it("streams nothing when the implicit subscribe is permission-denied", async () => {
|
|
236
|
+
const consoleError = jest.spyOn(console, "error").mockImplementation();
|
|
237
|
+
const localUserId = (0, utils_1.newid)();
|
|
238
|
+
const { consumer, session, live, providerTransport } = wire({
|
|
239
|
+
values: { [OBS_NAME]: 0 },
|
|
240
|
+
permissionCheck: contract_proxy_1.sameUserContractPermissionCheck,
|
|
241
|
+
context: { localUserId, callerUserId: (0, utils_1.newid)() },
|
|
242
|
+
});
|
|
243
|
+
// Both the initial get and the implicit subscribe are denied; loadingPromise still
|
|
244
|
+
// resolves (failures are logged) and the mirror stays unset.
|
|
245
|
+
await consumer.loadingPromise;
|
|
246
|
+
const notifySpy = jest.spyOn(providerTransport, "emit");
|
|
247
|
+
live[OBS_NAME](5);
|
|
248
|
+
await flush();
|
|
249
|
+
expect(notifySpy).not.toHaveBeenCalled();
|
|
250
|
+
expect(consumer.observables[OBS_NAME]()).toBeUndefined();
|
|
251
|
+
expect(consoleError).toHaveBeenCalledTimes(2);
|
|
252
|
+
expect(consoleError).toHaveBeenCalledWith("Contract subscribe failed", expect.objectContaining({ message: expect.stringMatching(/Permission denied/) }));
|
|
253
|
+
expect(consoleError).toHaveBeenCalledWith("Contract observable initial get failed", expect.objectContaining({ message: expect.stringMatching(/Permission denied/) }));
|
|
254
|
+
session.dispose();
|
|
255
|
+
consumer.dispose();
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
describe("contract observables — teardown", () => {
|
|
259
|
+
it("session.dispose() detaches the live subscription and stops pushes", async () => {
|
|
260
|
+
const { consumer, session, live, providerTransport } = wire({ values: { [OBS_NAME]: 0 } });
|
|
261
|
+
await consumer.loadingPromise;
|
|
262
|
+
live[OBS_NAME](1);
|
|
263
|
+
await flush();
|
|
264
|
+
expect(consumer.observables[OBS_NAME]()).toBe(1);
|
|
265
|
+
const notifySpy = jest.spyOn(providerTransport, "emit");
|
|
266
|
+
session.dispose();
|
|
267
|
+
live[OBS_NAME](2);
|
|
268
|
+
await flush();
|
|
269
|
+
expect(notifySpy).not.toHaveBeenCalled();
|
|
270
|
+
expect(consumer.observables[OBS_NAME]()).toBe(1);
|
|
271
|
+
consumer.dispose();
|
|
272
|
+
});
|
|
273
|
+
it("consumer.dispose() unsubscribes the stream so the provider detaches", async () => {
|
|
274
|
+
const { consumer, session, live } = wire({ values: { [OBS_NAME]: 0 } });
|
|
275
|
+
await consumer.loadingPromise;
|
|
276
|
+
live[OBS_NAME](1);
|
|
277
|
+
await flush();
|
|
278
|
+
expect(consumer.observables[OBS_NAME]()).toBe(1);
|
|
279
|
+
// Exactly one live subscription (the refcounted session handler) is attached.
|
|
280
|
+
expect(live[OBS_NAME].subscriberCount()).toBe(1);
|
|
281
|
+
consumer.dispose();
|
|
282
|
+
await flush();
|
|
283
|
+
// The unsubscribe reached the provider and detached the last (only) subscriber.
|
|
284
|
+
expect(live[OBS_NAME].subscriberCount()).toBe(0);
|
|
285
|
+
live[OBS_NAME](2);
|
|
286
|
+
await flush();
|
|
287
|
+
expect(consumer.observables[OBS_NAME]()).toBe(1);
|
|
288
|
+
session.dispose();
|
|
289
|
+
});
|
|
290
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|