@peers-app/peers-sdk 0.19.14 → 0.20.1
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 +7 -1
- package/dist/contracts/index.js +34 -1
- package/dist/contracts/persistent-registry.js +3 -1
- package/dist/contracts/system/device-operations-contract.d.ts +72 -0
- package/dist/contracts/system/device-operations-contract.js +110 -0
- 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/assistants.d.ts +4 -4
- package/dist/data/channels.d.ts +1 -1
- package/dist/data/devices.d.ts +1 -1
- package/dist/data/embeddings.d.ts +1 -1
- package/dist/data/group-secret.d.ts +21 -0
- package/dist/data/group-secret.js +53 -0
- package/dist/data/index.d.ts +1 -0
- package/dist/data/index.js +1 -0
- package/dist/data/messages.d.ts +1 -1
- package/dist/data/orm/decorators.d.ts +6 -0
- package/dist/data/orm/decorators.js +21 -0
- package/dist/data/package-versions.d.ts +2 -2
- package/dist/data/peer-types.d.ts +1 -1
- package/dist/data/persistent-vars.d.ts +17 -0
- package/dist/data/persistent-vars.js +115 -35
- package/dist/data/persistent-vars.migration.test.d.ts +1 -0
- package/dist/data/persistent-vars.migration.test.js +133 -0
- package/dist/data/table-definitions-table.d.ts +1 -1
- package/dist/data/tool-tests.d.ts +1 -1
- package/dist/data/tools.d.ts +1 -1
- package/dist/data/user-trust-levels.d.ts +3 -1
- 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/data/voice-messages.d.ts +1 -1
- package/dist/data/workflow-logs.d.ts +1 -1
- package/dist/data/workflow-runs.d.ts +2 -2
- package/dist/data/workflows.d.ts +3 -3
- 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/rpc-types.d.ts +37 -0
- package/dist/rpc-types.js +13 -0
- package/dist/system-ids.d.ts +25 -0
- package/dist/system-ids.js +26 -1
- package/dist/types/workflow.d.ts +4 -4
- package/package.json +1 -1
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const user_permissions_1 = require("../../data/user-permissions");
|
|
4
|
+
const user_trust_levels_1 = require("../../data/user-trust-levels");
|
|
5
|
+
const users_1 = require("../../data/users");
|
|
6
|
+
const socket_type_1 = require("../../device/socket.type");
|
|
7
|
+
const keys_1 = require("../../keys");
|
|
8
|
+
const utils_1 = require("../../utils");
|
|
9
|
+
const index_1 = require("../index");
|
|
10
|
+
jest.mock("../../data/user-trust-levels", () => ({
|
|
11
|
+
UserTrustLevels: jest.fn(),
|
|
12
|
+
}));
|
|
13
|
+
jest.mock("../../data/users", () => ({
|
|
14
|
+
Users: jest.fn(),
|
|
15
|
+
}));
|
|
16
|
+
const mockUserTrustLevels = user_trust_levels_1.UserTrustLevels;
|
|
17
|
+
const mockUsers = users_1.Users;
|
|
18
|
+
const mockTrustGet = jest.fn();
|
|
19
|
+
function makeIdentity() {
|
|
20
|
+
const keys = (0, keys_1.newKeys)();
|
|
21
|
+
const userId = (0, utils_1.newid)();
|
|
22
|
+
const user = (0, user_permissions_1.signUserObject)({
|
|
23
|
+
userId,
|
|
24
|
+
name: "Remote user",
|
|
25
|
+
publicKey: keys.publicKey,
|
|
26
|
+
publicBoxKey: keys.publicBoxKey,
|
|
27
|
+
}, keys.secretKey);
|
|
28
|
+
const remoteIdentity = {
|
|
29
|
+
userId,
|
|
30
|
+
deviceId: (0, utils_1.newid)(),
|
|
31
|
+
publicKey: keys.publicKey,
|
|
32
|
+
publicBoxKey: keys.publicBoxKey,
|
|
33
|
+
};
|
|
34
|
+
return { remoteIdentity, user };
|
|
35
|
+
}
|
|
36
|
+
function setupUserContext(localUserId, records) {
|
|
37
|
+
const personalContext = {};
|
|
38
|
+
const groupContext = {};
|
|
39
|
+
const userContext = {
|
|
40
|
+
userId: localUserId,
|
|
41
|
+
userDataContext: personalContext,
|
|
42
|
+
groupDataContexts: new Map([["group", groupContext]]),
|
|
43
|
+
getDataContext: jest.fn(),
|
|
44
|
+
};
|
|
45
|
+
mockUsers.mockImplementation((dataContext) => ({
|
|
46
|
+
get: jest.fn().mockResolvedValue(records.get(dataContext) ?? null),
|
|
47
|
+
}));
|
|
48
|
+
mockUserTrustLevels.mockReturnValue({ get: mockTrustGet });
|
|
49
|
+
return { groupContext, personalContext, userContext: userContext };
|
|
50
|
+
}
|
|
51
|
+
const call = {
|
|
52
|
+
contractId: "test-contract",
|
|
53
|
+
version: 1,
|
|
54
|
+
member: "table",
|
|
55
|
+
name: "Items",
|
|
56
|
+
operation: "list",
|
|
57
|
+
args: [],
|
|
58
|
+
};
|
|
59
|
+
describe("Self-trusted contract caller authorization", () => {
|
|
60
|
+
beforeEach(() => {
|
|
61
|
+
jest.clearAllMocks();
|
|
62
|
+
});
|
|
63
|
+
it.each([
|
|
64
|
+
["same-account", true],
|
|
65
|
+
["explicit cross-account", false],
|
|
66
|
+
])("allows a signed %s identity with personal Self trust", async (_label, sameAccount) => {
|
|
67
|
+
const { remoteIdentity, user } = makeIdentity();
|
|
68
|
+
const localUserId = sameAccount ? remoteIdentity.userId : (0, utils_1.newid)();
|
|
69
|
+
const records = new Map();
|
|
70
|
+
const { personalContext, userContext } = setupUserContext(localUserId, records);
|
|
71
|
+
records.set(personalContext, user);
|
|
72
|
+
mockTrustGet.mockResolvedValue({ trustLevel: socket_type_1.TrustLevel.Self });
|
|
73
|
+
await expect((0, index_1.isSelfTrustedContractCaller)(userContext, remoteIdentity)).resolves.toBe(true);
|
|
74
|
+
if (sameAccount) {
|
|
75
|
+
expect(mockTrustGet).not.toHaveBeenCalled();
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
expect(mockUserTrustLevels).toHaveBeenCalledWith(personalContext);
|
|
79
|
+
expect(mockTrustGet).toHaveBeenCalledWith(remoteIdentity.userId);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
it.each([
|
|
83
|
+
socket_type_1.TrustLevel.Trusted,
|
|
84
|
+
socket_type_1.TrustLevel.Unknown,
|
|
85
|
+
])("denies a signed identity with trust level %s", async (trustLevel) => {
|
|
86
|
+
const { remoteIdentity, user } = makeIdentity();
|
|
87
|
+
const records = new Map();
|
|
88
|
+
const { personalContext, userContext } = setupUserContext((0, utils_1.newid)(), records);
|
|
89
|
+
records.set(personalContext, user);
|
|
90
|
+
mockTrustGet.mockResolvedValue({ trustLevel });
|
|
91
|
+
await expect((0, index_1.isSelfTrustedContractCaller)(userContext, remoteIdentity)).resolves.toBe(false);
|
|
92
|
+
});
|
|
93
|
+
it("accepts a matching signed identity found only in a shared group", async () => {
|
|
94
|
+
const { remoteIdentity, user } = makeIdentity();
|
|
95
|
+
const records = new Map();
|
|
96
|
+
const { groupContext, userContext } = setupUserContext((0, utils_1.newid)(), records);
|
|
97
|
+
records.set(groupContext, user);
|
|
98
|
+
mockTrustGet.mockResolvedValue({ trustLevel: socket_type_1.TrustLevel.Self });
|
|
99
|
+
await expect((0, index_1.isSelfTrustedContractCaller)(userContext, remoteIdentity)).resolves.toBe(true);
|
|
100
|
+
});
|
|
101
|
+
it("denies unsigned discovery stubs", async () => {
|
|
102
|
+
const { remoteIdentity, user } = makeIdentity();
|
|
103
|
+
const records = new Map();
|
|
104
|
+
const { personalContext, userContext } = setupUserContext((0, utils_1.newid)(), records);
|
|
105
|
+
records.set(personalContext, { ...user, signature: undefined });
|
|
106
|
+
mockTrustGet.mockResolvedValue({ trustLevel: socket_type_1.TrustLevel.Self });
|
|
107
|
+
await expect((0, index_1.isSelfTrustedContractCaller)(userContext, remoteIdentity)).resolves.toBe(false);
|
|
108
|
+
expect(mockTrustGet).not.toHaveBeenCalled();
|
|
109
|
+
});
|
|
110
|
+
it.each([
|
|
111
|
+
"publicKey",
|
|
112
|
+
"publicBoxKey",
|
|
113
|
+
])("denies a connection whose %s differs from the signed user", async (field) => {
|
|
114
|
+
const { remoteIdentity, user } = makeIdentity();
|
|
115
|
+
const records = new Map();
|
|
116
|
+
const { personalContext, userContext } = setupUserContext((0, utils_1.newid)(), records);
|
|
117
|
+
records.set(personalContext, user);
|
|
118
|
+
const mismatchedIdentity = { ...remoteIdentity, [field]: `mismatch-${(0, utils_1.newid)()}` };
|
|
119
|
+
mockTrustGet.mockResolvedValue({ trustLevel: socket_type_1.TrustLevel.Self });
|
|
120
|
+
await expect((0, index_1.isSelfTrustedContractCaller)(userContext, mismatchedIdentity)).resolves.toBe(false);
|
|
121
|
+
expect(mockTrustGet).not.toHaveBeenCalled();
|
|
122
|
+
});
|
|
123
|
+
it("denies a malformed user signature", async () => {
|
|
124
|
+
const { remoteIdentity, user } = makeIdentity();
|
|
125
|
+
const records = new Map();
|
|
126
|
+
const { personalContext, userContext } = setupUserContext((0, utils_1.newid)(), records);
|
|
127
|
+
records.set(personalContext, { ...user, signature: `${user.signature}tampered` });
|
|
128
|
+
mockTrustGet.mockResolvedValue({ trustLevel: socket_type_1.TrustLevel.Self });
|
|
129
|
+
await expect((0, index_1.isSelfTrustedContractCaller)(userContext, remoteIdentity)).resolves.toBe(false);
|
|
130
|
+
});
|
|
131
|
+
it("ignores peer-supplied caller context when authorizing the router", async () => {
|
|
132
|
+
const { remoteIdentity, user } = makeIdentity();
|
|
133
|
+
const records = new Map();
|
|
134
|
+
const { personalContext, userContext } = setupUserContext((0, utils_1.newid)(), records);
|
|
135
|
+
records.set(personalContext, { ...user, signature: undefined });
|
|
136
|
+
mockTrustGet.mockResolvedValue({ trustLevel: socket_type_1.TrustLevel.Self });
|
|
137
|
+
const [consumerTransport, providerTransport] = (0, index_1.inProcessTransportPair)();
|
|
138
|
+
const router = (0, index_1.createUserContextContractProviderRouter)(providerTransport, {
|
|
139
|
+
userContext,
|
|
140
|
+
remoteIdentity,
|
|
141
|
+
});
|
|
142
|
+
await expect(consumerTransport.emit(index_1.CONTRACT_REQUEST_CHANNEL, call, {
|
|
143
|
+
callerUserId: userContext.userId,
|
|
144
|
+
localUserId: userContext.userId,
|
|
145
|
+
})).rejects.toThrow("Permission denied");
|
|
146
|
+
router.dispose();
|
|
147
|
+
});
|
|
148
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,353 @@
|
|
|
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
|
+
function deferred() {
|
|
9
|
+
let resolve;
|
|
10
|
+
const promise = new Promise((resolvePromise) => {
|
|
11
|
+
resolve = resolvePromise;
|
|
12
|
+
});
|
|
13
|
+
return { promise, resolve };
|
|
14
|
+
}
|
|
15
|
+
function trackedEvent() {
|
|
16
|
+
const handlers = new Set();
|
|
17
|
+
const event = {
|
|
18
|
+
subscribe: jest.fn((handler) => {
|
|
19
|
+
handlers.add(handler);
|
|
20
|
+
let active = true;
|
|
21
|
+
return {
|
|
22
|
+
unsubscribe: () => {
|
|
23
|
+
if (!active) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
active = false;
|
|
27
|
+
return handlers.delete(handler);
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}),
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
event,
|
|
34
|
+
activeCount: () => handlers.size,
|
|
35
|
+
emit: async (value) => {
|
|
36
|
+
for (const handler of [...handlers]) {
|
|
37
|
+
await handler(value);
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function trackedObservable(initialValue) {
|
|
43
|
+
const source = (0, observable_1.observable)(initialValue);
|
|
44
|
+
const subscribe = source.subscribe;
|
|
45
|
+
let activeCount = 0;
|
|
46
|
+
source.subscribe = (handler) => {
|
|
47
|
+
activeCount += 1;
|
|
48
|
+
const subscription = subscribe(handler);
|
|
49
|
+
let active = true;
|
|
50
|
+
return {
|
|
51
|
+
dispose: () => {
|
|
52
|
+
if (!active) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
active = false;
|
|
56
|
+
activeCount -= 1;
|
|
57
|
+
subscription.dispose();
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
return { source, activeCount: () => activeCount };
|
|
62
|
+
}
|
|
63
|
+
async function settleTransport(emitSpy) {
|
|
64
|
+
let previousCount = -1;
|
|
65
|
+
while (previousCount !== emitSpy.mock.results.length) {
|
|
66
|
+
previousCount = emitSpy.mock.results.length;
|
|
67
|
+
const pending = emitSpy.mock.results
|
|
68
|
+
.map((result) => result.value)
|
|
69
|
+
.filter((value) => value instanceof Promise);
|
|
70
|
+
await Promise.allSettled(pending);
|
|
71
|
+
await Promise.resolve();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const cleanups = [];
|
|
75
|
+
afterEach(() => {
|
|
76
|
+
for (const cleanup of cleanups.splice(0).reverse()) {
|
|
77
|
+
cleanup();
|
|
78
|
+
}
|
|
79
|
+
jest.restoreAllMocks();
|
|
80
|
+
});
|
|
81
|
+
function makeHarness(opts = {}) {
|
|
82
|
+
const contractId = (0, utils_1.newid)();
|
|
83
|
+
const generic = trackedEvent();
|
|
84
|
+
const tableChanged = trackedEvent();
|
|
85
|
+
const count = trackedObservable(0);
|
|
86
|
+
const list = jest.fn(async () => []);
|
|
87
|
+
const run = jest.fn(async () => "ok");
|
|
88
|
+
const contract = {
|
|
89
|
+
contractId,
|
|
90
|
+
version: 1,
|
|
91
|
+
devTag: "dev",
|
|
92
|
+
name: "Lifecycle Contract",
|
|
93
|
+
description: "Exercises every subscription surface",
|
|
94
|
+
tables: [
|
|
95
|
+
{
|
|
96
|
+
name: "Widgets",
|
|
97
|
+
description: "Widgets",
|
|
98
|
+
primaryKeyName: "id",
|
|
99
|
+
fields: [{ name: "id", type: field_type_1.FieldType.id }],
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
tools: [
|
|
103
|
+
{
|
|
104
|
+
name: "run",
|
|
105
|
+
usageDescription: "Run",
|
|
106
|
+
inputFields: [],
|
|
107
|
+
outputFields: [],
|
|
108
|
+
},
|
|
109
|
+
],
|
|
110
|
+
observables: [
|
|
111
|
+
{
|
|
112
|
+
name: "count",
|
|
113
|
+
description: "Count",
|
|
114
|
+
valueType: field_type_1.FieldType.number,
|
|
115
|
+
writable: true,
|
|
116
|
+
},
|
|
117
|
+
],
|
|
118
|
+
events: [
|
|
119
|
+
{
|
|
120
|
+
name: "changed",
|
|
121
|
+
description: "Changed",
|
|
122
|
+
payloadFields: [{ name: "id", type: field_type_1.FieldType.id }],
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
};
|
|
126
|
+
const resolution = {
|
|
127
|
+
contractId,
|
|
128
|
+
version: 1,
|
|
129
|
+
tables: {
|
|
130
|
+
Widgets: { dataChanged: tableChanged.event, list },
|
|
131
|
+
},
|
|
132
|
+
tools: {
|
|
133
|
+
run: {
|
|
134
|
+
tool: { toolId: (0, utils_1.newid)(), name: "run" },
|
|
135
|
+
toolFn: run,
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
observables: { count: count.source },
|
|
139
|
+
observableWritability: { count: true },
|
|
140
|
+
events: { changed: generic.event },
|
|
141
|
+
};
|
|
142
|
+
const [consumerTransport, providerTransport] = (0, contract_transport_1.inProcessTransportPair)();
|
|
143
|
+
const emitSpy = jest.spyOn(consumerTransport, "emit");
|
|
144
|
+
const session = (0, contract_proxy_1.createContractProviderSession)(resolution, providerTransport, {
|
|
145
|
+
permissionCheck: opts.permissionCheck,
|
|
146
|
+
});
|
|
147
|
+
const consumer = (0, contract_proxy_1.createContractConsumer)(contract, consumerTransport);
|
|
148
|
+
cleanups.push(() => {
|
|
149
|
+
consumer.dispose();
|
|
150
|
+
session.dispose();
|
|
151
|
+
});
|
|
152
|
+
return {
|
|
153
|
+
consumer,
|
|
154
|
+
session,
|
|
155
|
+
generic,
|
|
156
|
+
tableChanged,
|
|
157
|
+
count,
|
|
158
|
+
emitSpy,
|
|
159
|
+
consumerTransport,
|
|
160
|
+
providerTransport,
|
|
161
|
+
contractId,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
describe("contract subscription lifecycle — consumer teardown", () => {
|
|
165
|
+
it("detaches generic, table, and observable provider subscriptions", async () => {
|
|
166
|
+
const { consumer, generic, tableChanged, count, emitSpy } = makeHarness();
|
|
167
|
+
consumer.events.changed.subscribe(() => { });
|
|
168
|
+
consumer.tables.Widgets.dataChanged.subscribe(() => { });
|
|
169
|
+
await consumer.loadingPromise;
|
|
170
|
+
expect(generic.activeCount()).toBe(1);
|
|
171
|
+
expect(tableChanged.activeCount()).toBe(1);
|
|
172
|
+
expect(count.activeCount()).toBe(1);
|
|
173
|
+
consumer.dispose();
|
|
174
|
+
await settleTransport(emitSpy);
|
|
175
|
+
expect(generic.activeCount()).toBe(0);
|
|
176
|
+
expect(tableChanged.activeCount()).toBe(0);
|
|
177
|
+
expect(count.activeCount()).toBe(0);
|
|
178
|
+
});
|
|
179
|
+
it("is idempotent and sends each wire unsubscribe at most once", async () => {
|
|
180
|
+
const { consumer, emitSpy } = makeHarness();
|
|
181
|
+
consumer.events.changed.subscribe(() => { });
|
|
182
|
+
await consumer.loadingPromise;
|
|
183
|
+
consumer.dispose();
|
|
184
|
+
consumer.dispose();
|
|
185
|
+
await settleTransport(emitSpy);
|
|
186
|
+
const unsubscribeCalls = emitSpy.mock.calls.filter(([channel, call]) => channel === contract_transport_1.CONTRACT_REQUEST_CHANNEL && call.operation === "unsubscribe");
|
|
187
|
+
expect(unsubscribeCalls).toHaveLength(2);
|
|
188
|
+
});
|
|
189
|
+
it("returns false and emits nothing for a repeated event unsubscribe", async () => {
|
|
190
|
+
const { consumer, emitSpy } = makeHarness();
|
|
191
|
+
const subscription = consumer.events.changed.subscribe(() => { });
|
|
192
|
+
await settleTransport(emitSpy);
|
|
193
|
+
emitSpy.mockClear();
|
|
194
|
+
expect(subscription.unsubscribe()).toBe(true);
|
|
195
|
+
expect(subscription.unsubscribe()).toBe(false);
|
|
196
|
+
await settleTransport(emitSpy);
|
|
197
|
+
const unsubscribeCalls = emitSpy.mock.calls
|
|
198
|
+
.map(([, call]) => call)
|
|
199
|
+
.filter((call) => call.name === "changed" && call.operation === "unsubscribe");
|
|
200
|
+
expect(unsubscribeCalls).toHaveLength(1);
|
|
201
|
+
});
|
|
202
|
+
it("rejects new remote operations and writes after disposal without emitting", async () => {
|
|
203
|
+
const { consumer, emitSpy } = makeHarness();
|
|
204
|
+
await consumer.loadingPromise;
|
|
205
|
+
expect(consumer.observables.count()).toBe(0);
|
|
206
|
+
consumer.dispose();
|
|
207
|
+
await settleTransport(emitSpy);
|
|
208
|
+
emitSpy.mockClear();
|
|
209
|
+
await expect(consumer.tables.Widgets.list()).rejects.toThrow(/consumer .* disposed/i);
|
|
210
|
+
await expect(consumer.tools.run()).rejects.toThrow(/consumer .* disposed/i);
|
|
211
|
+
expect(() => consumer.events.changed.subscribe(() => { })).toThrow(/consumer .* disposed/i);
|
|
212
|
+
expect(() => consumer.tables.Widgets.dataChanged.subscribe(() => { })).toThrow(/consumer .* disposed/i);
|
|
213
|
+
expect(() => consumer.observables.count(1)).toThrow(/consumer .* disposed/i);
|
|
214
|
+
expect(consumer.observables.count()).toBe(0);
|
|
215
|
+
expect(emitSpy).not.toHaveBeenCalled();
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
describe("contract subscription lifecycle — ordering and failures", () => {
|
|
219
|
+
it("queues immediate unsubscribe behind a delayed subscribe acknowledgement", async () => {
|
|
220
|
+
const permissionStarted = deferred();
|
|
221
|
+
const permissionResult = deferred();
|
|
222
|
+
const permissionCheck = async (call) => {
|
|
223
|
+
if (call.operation === "subscribe") {
|
|
224
|
+
permissionStarted.resolve();
|
|
225
|
+
return permissionResult.promise;
|
|
226
|
+
}
|
|
227
|
+
return true;
|
|
228
|
+
};
|
|
229
|
+
const { consumer, generic, emitSpy } = makeHarness({ permissionCheck });
|
|
230
|
+
const subscription = consumer.events.changed.subscribe(() => { });
|
|
231
|
+
await permissionStarted.promise;
|
|
232
|
+
expect(subscription.unsubscribe()).toBe(true);
|
|
233
|
+
expect(generic.activeCount()).toBe(0);
|
|
234
|
+
permissionResult.resolve(true);
|
|
235
|
+
await settleTransport(emitSpy);
|
|
236
|
+
expect(generic.activeCount()).toBe(0);
|
|
237
|
+
const operations = emitSpy.mock.calls
|
|
238
|
+
.filter(([channel]) => channel === contract_transport_1.CONTRACT_REQUEST_CHANNEL)
|
|
239
|
+
.map(([, call]) => call.operation)
|
|
240
|
+
.filter((operation) => operation === "subscribe" || operation === "unsubscribe");
|
|
241
|
+
expect(operations).toEqual(["subscribe", "subscribe", "unsubscribe"]);
|
|
242
|
+
});
|
|
243
|
+
it("does not unsubscribe after a failed subscribe", async () => {
|
|
244
|
+
const consoleError = jest.spyOn(console, "error").mockImplementation();
|
|
245
|
+
const permissionStarted = deferred();
|
|
246
|
+
const permissionResult = deferred();
|
|
247
|
+
const permissionCheck = async (call) => {
|
|
248
|
+
if (call.operation === "subscribe") {
|
|
249
|
+
permissionStarted.resolve();
|
|
250
|
+
return permissionResult.promise;
|
|
251
|
+
}
|
|
252
|
+
return true;
|
|
253
|
+
};
|
|
254
|
+
const { consumer, generic, emitSpy } = makeHarness({ permissionCheck });
|
|
255
|
+
const subscription = consumer.events.changed.subscribe(() => { });
|
|
256
|
+
await permissionStarted.promise;
|
|
257
|
+
subscription.unsubscribe();
|
|
258
|
+
consumer.dispose();
|
|
259
|
+
permissionResult.resolve(false);
|
|
260
|
+
await settleTransport(emitSpy);
|
|
261
|
+
expect(generic.activeCount()).toBe(0);
|
|
262
|
+
const changedCalls = emitSpy.mock.calls
|
|
263
|
+
.map(([, call]) => call)
|
|
264
|
+
.filter((call) => call.name === "changed");
|
|
265
|
+
expect(changedCalls.map((call) => call.operation)).toEqual(["subscribe"]);
|
|
266
|
+
expect(consoleError).toHaveBeenCalledWith("Contract subscribe failed", expect.objectContaining({ message: expect.stringMatching(/Permission denied/) }));
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
describe("contract subscription lifecycle — provider protocol", () => {
|
|
270
|
+
it("rejects duplicate subscription ids without corrupting the existing refcount", async () => {
|
|
271
|
+
const { generic, consumerTransport, contractId } = makeHarness();
|
|
272
|
+
const subscriptionId = (0, utils_1.newid)();
|
|
273
|
+
const call = {
|
|
274
|
+
contractId,
|
|
275
|
+
version: 1,
|
|
276
|
+
member: "event",
|
|
277
|
+
name: "changed",
|
|
278
|
+
operation: "subscribe",
|
|
279
|
+
subscriptionId,
|
|
280
|
+
args: [],
|
|
281
|
+
};
|
|
282
|
+
await consumerTransport.emit(contract_transport_1.CONTRACT_REQUEST_CHANNEL, call);
|
|
283
|
+
await expect(consumerTransport.emit(contract_transport_1.CONTRACT_REQUEST_CHANNEL, call)).rejects.toThrow(/Duplicate contract subscriptionId/);
|
|
284
|
+
expect(generic.activeCount()).toBe(1);
|
|
285
|
+
await consumerTransport.emit(contract_transport_1.CONTRACT_REQUEST_CHANNEL, {
|
|
286
|
+
...call,
|
|
287
|
+
operation: "unsubscribe",
|
|
288
|
+
subscriptionId: (0, utils_1.newid)(),
|
|
289
|
+
});
|
|
290
|
+
expect(generic.activeCount()).toBe(1);
|
|
291
|
+
await consumerTransport.emit(contract_transport_1.CONTRACT_REQUEST_CHANNEL, {
|
|
292
|
+
...call,
|
|
293
|
+
operation: "unsubscribe",
|
|
294
|
+
});
|
|
295
|
+
expect(generic.activeCount()).toBe(0);
|
|
296
|
+
});
|
|
297
|
+
it("rejects missing ids and unsupported member/operation combinations", async () => {
|
|
298
|
+
const { consumerTransport, contractId } = makeHarness();
|
|
299
|
+
const base = {
|
|
300
|
+
contractId,
|
|
301
|
+
version: 1,
|
|
302
|
+
name: "changed",
|
|
303
|
+
args: [],
|
|
304
|
+
};
|
|
305
|
+
await expect(consumerTransport.emit(contract_transport_1.CONTRACT_REQUEST_CHANNEL, {
|
|
306
|
+
...base,
|
|
307
|
+
member: "event",
|
|
308
|
+
operation: "subscribe",
|
|
309
|
+
})).rejects.toThrow(/requires a subscriptionId/);
|
|
310
|
+
await expect(consumerTransport.emit(contract_transport_1.CONTRACT_REQUEST_CHANNEL, {
|
|
311
|
+
...base,
|
|
312
|
+
member: "tool",
|
|
313
|
+
operation: "subscribe",
|
|
314
|
+
subscriptionId: (0, utils_1.newid)(),
|
|
315
|
+
})).rejects.toThrow(/does not support subscriptions/);
|
|
316
|
+
await expect(consumerTransport.emit(contract_transport_1.CONTRACT_REQUEST_CHANNEL, {
|
|
317
|
+
...base,
|
|
318
|
+
member: "event",
|
|
319
|
+
operation: "get",
|
|
320
|
+
subscriptionId: (0, utils_1.newid)(),
|
|
321
|
+
})).rejects.toThrow(/Unsupported subscription operation/);
|
|
322
|
+
await expect(consumerTransport.emit(contract_transport_1.CONTRACT_REQUEST_CHANNEL, {
|
|
323
|
+
...base,
|
|
324
|
+
member: "table",
|
|
325
|
+
name: "Widgets",
|
|
326
|
+
operation: "subscribe",
|
|
327
|
+
event: "privateEvent",
|
|
328
|
+
subscriptionId: (0, utils_1.newid)(),
|
|
329
|
+
})).rejects.toThrow(/Unsupported table event/);
|
|
330
|
+
});
|
|
331
|
+
it("isolates a rejected notify so later subscribers still receive the payload", async () => {
|
|
332
|
+
const consoleError = jest.spyOn(console, "error").mockImplementation();
|
|
333
|
+
const { consumer, generic, emitSpy } = makeHarness();
|
|
334
|
+
consumer.events.changed.subscribe(() => {
|
|
335
|
+
throw new Error("first handler failed");
|
|
336
|
+
});
|
|
337
|
+
const received = [];
|
|
338
|
+
consumer.events.changed.subscribe((value) => received.push(value));
|
|
339
|
+
await settleTransport(emitSpy);
|
|
340
|
+
await generic.emit({ id: "w1" });
|
|
341
|
+
await Promise.resolve();
|
|
342
|
+
expect(received).toEqual([{ id: "w1" }]);
|
|
343
|
+
expect(consoleError).toHaveBeenCalledWith(expect.stringMatching(/Contract notify failed/), expect.objectContaining({ message: "first handler failed" }));
|
|
344
|
+
});
|
|
345
|
+
it("allows provider session disposal more than once", () => {
|
|
346
|
+
const { session, generic } = makeHarness();
|
|
347
|
+
expect(() => {
|
|
348
|
+
session.dispose();
|
|
349
|
+
session.dispose();
|
|
350
|
+
}).not.toThrow();
|
|
351
|
+
expect(generic.activeCount()).toBe(0);
|
|
352
|
+
});
|
|
353
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|