@descix/platform-api 5.1.1 → 5.3.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/LICENSE.md +80 -71
- package/README.md +71 -0
- package/package.json +7 -4
- package/src/account-admin/conformance.js +526 -0
- package/src/account-admin/contract.js +513 -0
- package/src/account-admin/index.js +913 -0
- package/src/email/index.js +50 -5
- package/src/fabric/vocab.js +12 -12
- package/src/index.js +64 -0
- package/src/manifest/index.js +1 -1
- package/src/mcp-tools/chatMedia.js +1 -1
- package/src/mcp-tools/cli-verbs.generated.json +101 -26
- package/src/mcp-tools/cli-verbs.js +2 -2
- package/src/mcp-tools/evidence-contract.js +7 -7
- package/src/mcp-tools/handshake.js +2 -3
- package/src/mcp-tools/index.js +7 -7
- package/src/mcp-tools/nativeTools.js +24 -24
- package/src/mcp-tools/paramValidation.js +4 -5
- package/src/mcp-tools/platform-basics.js +1 -1
- package/src/mcp-tools/setup-playbook.js +6 -6
- package/src/models/index.js +1231 -33
- package/src/naming/index.js +1 -1
- package/src/operator/booted-database.js +48 -0
- package/src/operator/index.js +142 -0
- package/src/permissions/index.js +1 -1
- package/src/service-bootstrap/index.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@descix/platform-api",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.3.0",
|
|
4
4
|
"description": "Shared platform infrastructure: Firestore models, auth/session, permissions, entitlements, email",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
"scripts": {
|
|
13
13
|
"test:migration-receipt-email": "node --test --experimental-test-module-mocks tests/migration-receipt-email.test.js",
|
|
14
14
|
"test:email-identity": "node --test --experimental-test-module-mocks tests/email-identity-owner.test.js",
|
|
15
|
-
"test:user-recovery": "node --test --experimental-test-module-mocks tests/user-recovery-confirmed-owner.test.js"
|
|
15
|
+
"test:user-recovery": "node --test --experimental-test-module-mocks tests/user-recovery-confirmed-owner.test.js",
|
|
16
|
+
"test:account-admin": "node --test --experimental-test-module-mocks tests/account-admin.test.js tests/operator-guards.test.js"
|
|
16
17
|
},
|
|
17
18
|
"exports": {
|
|
18
19
|
".": "./src/index.js",
|
|
@@ -25,7 +26,9 @@
|
|
|
25
26
|
"./manifest": "./src/manifest/index.js",
|
|
26
27
|
"./mcp-tools": "./src/mcp-tools/index.js",
|
|
27
28
|
"./naming": "./src/naming/index.js",
|
|
28
|
-
"./service-bootstrap": "./src/service-bootstrap/index.js"
|
|
29
|
+
"./service-bootstrap": "./src/service-bootstrap/index.js",
|
|
30
|
+
"./operator": "./src/operator/index.js",
|
|
31
|
+
"./account-admin": "./src/account-admin/index.js"
|
|
29
32
|
},
|
|
30
33
|
"files": [
|
|
31
34
|
"src"
|
|
@@ -42,7 +45,7 @@
|
|
|
42
45
|
"node": ">=24.0.0"
|
|
43
46
|
},
|
|
44
47
|
"dependencies": {
|
|
45
|
-
"@descix/cloud-core": "^2.0
|
|
48
|
+
"@descix/cloud-core": "^2.1.0",
|
|
46
49
|
"google-auth-library": "^10.5.0",
|
|
47
50
|
"googleapis": "^169.0.0"
|
|
48
51
|
}
|
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THE conformance check of an account-admin store owner (contract.js): drift between the User owner here
|
|
3
|
+
* and the Cloud and Powch owners is a test failure in the owner's own repo, never a discovery at the
|
|
4
|
+
* operator's first click. It drives the owner only through its contract methods, against a store the
|
|
5
|
+
* caller seeded, and judges collateral writes through the caller's snapshot of that store. Pure of
|
|
6
|
+
* Firestore: the caller's test owns the fake (or emulator) the owner was built on.
|
|
7
|
+
*/
|
|
8
|
+
import {
|
|
9
|
+
AccountAdminRefusal as R,
|
|
10
|
+
AccountAdminError,
|
|
11
|
+
STORE_OWNER_METHODS,
|
|
12
|
+
STORE_OWNER_MERGE_METHODS,
|
|
13
|
+
STORE_OWNER_RESTORE_METHOD,
|
|
14
|
+
USER_OWNER_METHODS,
|
|
15
|
+
canonicalJson,
|
|
16
|
+
credentialValues,
|
|
17
|
+
deleteExpectations,
|
|
18
|
+
entryDataHash,
|
|
19
|
+
footprintViolation,
|
|
20
|
+
isSecretPath,
|
|
21
|
+
moveKey,
|
|
22
|
+
} from './contract.js';
|
|
23
|
+
|
|
24
|
+
const CLAUSES = Object.freeze({
|
|
25
|
+
fixture: 'the fixture can exhibit every clause',
|
|
26
|
+
a: 'name, methods and flags',
|
|
27
|
+
b: 'footprintForUser returns { entries, leftovers, blockers } of the contract, read-only and deterministic',
|
|
28
|
+
c: 'deleteForUser refuses a stale plan (paths or data) before any write and deletes exactly expectPaths, nothing else; every bystander untouched',
|
|
29
|
+
d: 'a failed read throws ACCOUNT_STORE_READ_FAILED',
|
|
30
|
+
e: 'footprintForMerge / mergeForUser / validateRollbackMoves / rollbackMergeForUser round trip',
|
|
31
|
+
f: 'storeTarget() reports the database of the client the caller injected (clientTarget)',
|
|
32
|
+
g: 'no bystander (one sharing the email and wallet, one the wallet only, one the email only) shares a footprint path',
|
|
33
|
+
h: 'a delete while a blocker is present is refused before any write',
|
|
34
|
+
i: 'a merge rollback never overwrites a value changed since the merge',
|
|
35
|
+
j: 'every footprint read is bounded',
|
|
36
|
+
k: 'restoreForUser re-creates absent receipt entries and never overwrites a document',
|
|
37
|
+
l: 'a document changed after the footprint makes deleteForUser refuse ACCOUNT_PLAN_STALE, deleting nothing',
|
|
38
|
+
m: 'restoreForUser refuses an entry that is not this account\'s document, writing nothing',
|
|
39
|
+
n: 'no entry path names a credential: a document id that is a credential is a secretPath',
|
|
40
|
+
});
|
|
41
|
+
const ORDER = Object.freeze(Object.keys(CLAUSES).filter((c) => c !== 'fixture'));
|
|
42
|
+
/** A document path no footprint lists: a stale expectPaths or an edited receipt move names it. */
|
|
43
|
+
const NOT_PLANNED = 'AccountAdminConformance/not-planned';
|
|
44
|
+
|
|
45
|
+
/** Thrown by assertAccountStoreOwnerConforms; `clause` is one of CLAUSES' keys. */
|
|
46
|
+
export class AccountStoreOwnerNonConformance extends Error {
|
|
47
|
+
constructor(ownerName, clause, why) {
|
|
48
|
+
super(`store owner '${ownerName}' does not conform to the account-admin store-owner contract, clause (${clause}) ${CLAUSES[clause]}: ${why}`);
|
|
49
|
+
this.name = 'AccountStoreOwnerNonConformance';
|
|
50
|
+
this.owner = ownerName;
|
|
51
|
+
this.clause = clause;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const sortedUnique = (xs) => [...new Set(xs)].sort();
|
|
56
|
+
const describe = (e) => `${e?.name ?? 'Error'}${e?.code ? ` ${e.code}` : ''}: ${e?.message ?? e}`;
|
|
57
|
+
const nonEmptyString = (v) => typeof v === 'string' && v.length > 0;
|
|
58
|
+
const asText = (v) => (typeof v === 'string' ? v : canonicalJson(v));
|
|
59
|
+
const asDoc = (v) => (v === undefined ? undefined : typeof v === 'string' ? JSON.parse(v) : v);
|
|
60
|
+
|
|
61
|
+
/** The plan an orchestrator hands deleteForUser for a footprint (deleteExpectations). */
|
|
62
|
+
const planOf = (fp) => deleteExpectations(fp.entries);
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Verify, by CALLING it against the caller's seeded store, that `owner` implements the store-owner
|
|
66
|
+
* contract. Clauses, in the order run (the delete clauses, then restore, last: they delete the fixture):
|
|
67
|
+
* (a) `name`, footprintForUser, deleteForUser, storeTarget; all merge methods or none (the User owner, name
|
|
68
|
+
* 'user', also the merge methods and USER_OWNER_METHODS); `credentialKeys` an array of key names,
|
|
69
|
+
* `credentialIdCollections` an array of collection ids, `irrecoverable` a boolean and restoreForUser a
|
|
70
|
+
* function, when present;
|
|
71
|
+
* (f) storeTarget() answers { databaseId: non-empty string, projectId?: non-empty string } EQUAL to
|
|
72
|
+
* `clientTarget`, which the caller read from the client it injected (a storeTarget that reports another
|
|
73
|
+
* database than its client's is RED);
|
|
74
|
+
* (b) footprintForUser(user) is a footprint of the contract (footprintViolation), two reads answer the same,
|
|
75
|
+
* and the snapshot is unchanged by them;
|
|
76
|
+
* (g) footprintForUser(user) lists no path the footprint of any of the three `bystanders` lists;
|
|
77
|
+
* (n) no entry path has a segment equal to a credential value its document holds (credentialValues of the
|
|
78
|
+
* snapshot's raw document: what the receipt redacts); every entry of a `credentialIdCollections`
|
|
79
|
+
* collection, and only of a declared one, is `pathIsSecret` with a secretPath;
|
|
80
|
+
* (j) with `countUnboundedReads`, no footprint read is unbounded; without it SKIPPED;
|
|
81
|
+
* (d) with `withFailingReads`, footprintForUser (and footprintForMerge) throw ACCOUNT_STORE_READ_FAILED;
|
|
82
|
+
* without the hook SKIPPED, never passed;
|
|
83
|
+
* (e) merge owners: footprintForMerge(pair) is a merge footprint of the contract; mergeForUser with a stale or
|
|
84
|
+
* missing expectPaths throws ACCOUNT_PLAN_STALE and writes nothing; with the plan's paths it returns
|
|
85
|
+
* { moved, remaining } covering every move and footprintForMerge lists none of the moved;
|
|
86
|
+
* validateRollbackMoves accepts the receipt's moves and refuses an edited one (ACCOUNT_RECEIPT_INVALID,
|
|
87
|
+
* nothing written), as does rollbackMergeForUser; rollbackMergeForUser(moves, pair) returns
|
|
88
|
+
* { restored, remaining } covering every move and footprintForMerge lists every move again;
|
|
89
|
+
* (i) merge owners with `changeAfterMerge(move)`: after a merge and a change of the first moved value,
|
|
90
|
+
* rollback reports that move remaining and leaves the changed value; without the hook SKIPPED;
|
|
91
|
+
* (h) with `withBlocker(fn)`: inside it the user's footprint shows a blocker and deleteForUser with that
|
|
92
|
+
* footprint's plan throws that blocker's code, the snapshot unchanged; without the hook SKIPPED;
|
|
93
|
+
* (l) with `changeAfterFootprint(path)`: after a footprint, the caller changes the data (never the path) of its
|
|
94
|
+
* first non-volatile entry; deleteForUser with the footprint's plan throws ACCOUNT_PLAN_STALE and the
|
|
95
|
+
* snapshot is unchanged by it. REQUIRED for an `irrecoverable` owner (a missing hook is RED); SKIPPED
|
|
96
|
+
* otherwise without the hook;
|
|
97
|
+
* (c) deleteForUser with a stale or missing expectPaths, a missing expectData, or a planned data hash other
|
|
98
|
+
* than the document's throws ACCOUNT_PLAN_STALE, the snapshot unchanged; with the footprint's plan it
|
|
99
|
+
* returns { deleted, remaining } covering exactly its paths, a fresh footprint agrees, the snapshot
|
|
100
|
+
* changed in exactly `deleted` (no collateral write) and every bystander's footprint is unchanged;
|
|
101
|
+
* (m) owners with restoreForUser: an entry re-pathed outside the store, and a bystander's own entry, each
|
|
102
|
+
* handed with the user's deleted entries, throw ACCOUNT_RECEIPT_INVALID and change nothing;
|
|
103
|
+
* (k) owners with restoreForUser: restoreForUser(user, the deleted entries as a receipt holds them) returns
|
|
104
|
+
* { restored, remaining } covering them, a fresh footprint lists every restored one, the snapshot changed
|
|
105
|
+
* only at restored paths, and a second call (nothing absent) changes nothing.
|
|
106
|
+
*
|
|
107
|
+
* @param {object} owner - the store owner under test
|
|
108
|
+
* @param {{
|
|
109
|
+
* user: { user_id: string, verified_email: string, wallet_address: string },
|
|
110
|
+
* bystanders: Array<{ user_id: string, verified_email: string|null, wallet_address: string|null }>,
|
|
111
|
+
* snapshot: () => Map<string, unknown> | Promise<Map<string, unknown>>,
|
|
112
|
+
* clientTarget: { databaseId: string, projectId?: string },
|
|
113
|
+
* pair?: { from: object, into: object },
|
|
114
|
+
* withFailingReads?: (fn: () => Promise<unknown>) => Promise<unknown>,
|
|
115
|
+
* withBlocker?: (fn: () => Promise<unknown>) => Promise<unknown>,
|
|
116
|
+
* changeAfterMerge?: (move: { path: string, field: string, key?: string, from: unknown, to: unknown }) => Promise<void>,
|
|
117
|
+
* changeAfterFootprint?: (path: string) => Promise<void>,
|
|
118
|
+
* countUnboundedReads?: () => number,
|
|
119
|
+
* emptyFootprint?: boolean,
|
|
120
|
+
* }} fixture - `user`: an account whose footprint has at least one entry and no blocker (or none at all with
|
|
121
|
+
* `emptyFootprint: true`, then checked); `bystanders`: THREE other accounts, in this order: [0] the SAME
|
|
122
|
+
* verified_email and wallet_address as `user`, [1] the same wallet_address and another verified_email, [2] the
|
|
123
|
+
* same verified_email and another wallet_address; each with at least one footprint entry of its own (unless
|
|
124
|
+
* emptyFootprint); `snapshot()`: the whole seeded store as Map<path, value>, keyed by the paths footprints
|
|
125
|
+
* name (a credential-id document by its secretPath), values the RAW documents; `clientTarget`: the
|
|
126
|
+
* { databaseId, projectId? } the caller read from the Firestore client it injected into the owner; `pair`:
|
|
127
|
+
* required for a merge owner, a pair with at least one move and no blocker; `withFailingReads(fn)`: every
|
|
128
|
+
* read of the store throws while fn runs; `withBlocker(fn)`: the store holds something that blocks deleting
|
|
129
|
+
* `user` while fn runs; `changeAfterMerge(move)`: writes a different value at the move's path/field(/key);
|
|
130
|
+
* `changeAfterFootprint(path)`: writes different data (same path) at the entry's document;
|
|
131
|
+
* `countUnboundedReads()`: how many reads without a limit the store has served so far.
|
|
132
|
+
* @returns {Promise<{ owner: string, checks: Array<{ clause: string, title: string, status: 'passed'|'skipped', reason?: string }> }>}
|
|
133
|
+
* @throws {AccountStoreOwnerNonConformance} naming the clause that failed
|
|
134
|
+
*/
|
|
135
|
+
export async function assertAccountStoreOwnerConforms(owner, fixture = {}) {
|
|
136
|
+
const {
|
|
137
|
+
user, bystanders, snapshot, clientTarget, pair, withFailingReads, withBlocker, changeAfterMerge, changeAfterFootprint, countUnboundedReads, emptyFootprint = false,
|
|
138
|
+
} = fixture;
|
|
139
|
+
const name = typeof owner?.name === 'string' && owner.name ? owner.name : '(unnamed)';
|
|
140
|
+
const fail = (clause, why) => new AccountStoreOwnerNonConformance(name, clause, why);
|
|
141
|
+
const checks = [];
|
|
142
|
+
const passed = (clause) => checks.push({ clause, title: CLAUSES[clause], status: 'passed' });
|
|
143
|
+
const skipped = (clause, reason) => checks.push({ clause, title: CLAUSES[clause], status: 'skipped', reason });
|
|
144
|
+
|
|
145
|
+
/** Call an owner method; a throw is the clause's failure. */
|
|
146
|
+
const call = async (clause, label, fn) => {
|
|
147
|
+
try {
|
|
148
|
+
return await fn();
|
|
149
|
+
} catch (e) {
|
|
150
|
+
throw fail(clause, `${label} threw (${describe(e)})`);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
/** The call must throw an AccountAdminError with a code in `codes`. */
|
|
154
|
+
const refuses = async (clause, label, codes, fn) => {
|
|
155
|
+
const want = [codes].flat();
|
|
156
|
+
let result;
|
|
157
|
+
try {
|
|
158
|
+
result = await fn();
|
|
159
|
+
} catch (e) {
|
|
160
|
+
if (!(e instanceof AccountAdminError)) throw fail(clause, `${label} threw ${describe(e)}, not an AccountAdminError (import it from @descix/platform-api/account-admin)`);
|
|
161
|
+
if (!want.includes(e.code)) throw fail(clause, `${label} threw ${e.code}, not ${want.join(' or ')}`);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
throw fail(clause, `${label} answered (${JSON.stringify(result)?.slice(0, 200)}) instead of throwing ${want.join(' or ')}`);
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
// (a) name, methods and flags
|
|
168
|
+
if (!owner || typeof owner !== 'object') throw fail('a', 'the owner is not an object');
|
|
169
|
+
if (!nonEmptyString(owner.name)) throw fail('a', `no name (got ${JSON.stringify(owner.name)})`);
|
|
170
|
+
const lacking = STORE_OWNER_METHODS.filter((m) => typeof owner[m] !== 'function');
|
|
171
|
+
if (lacking.length) throw fail('a', `lacks ${lacking.join(', ')} (every owner implements ${STORE_OWNER_METHODS.join(', ')})`);
|
|
172
|
+
const mergeHas = STORE_OWNER_MERGE_METHODS.filter((m) => typeof owner[m] === 'function');
|
|
173
|
+
if (mergeHas.length && mergeHas.length !== STORE_OWNER_MERGE_METHODS.length) {
|
|
174
|
+
throw fail('a', `implements ${mergeHas.join(', ')} but not ${STORE_OWNER_MERGE_METHODS.filter((m) => !mergeHas.includes(m)).join(', ')}; a merge owner implements all of ${STORE_OWNER_MERGE_METHODS.join(', ')} or none`);
|
|
175
|
+
}
|
|
176
|
+
if (owner.name === 'user') {
|
|
177
|
+
const userLacks = [...STORE_OWNER_MERGE_METHODS, ...USER_OWNER_METHODS].filter((m) => typeof owner[m] !== 'function');
|
|
178
|
+
if (userLacks.length) throw fail('a', `is named 'user' (the User owner) but lacks ${userLacks.join(', ')}`);
|
|
179
|
+
}
|
|
180
|
+
if (owner.credentialKeys !== undefined && (!Array.isArray(owner.credentialKeys) || owner.credentialKeys.some((k) => !nonEmptyString(k)))) {
|
|
181
|
+
throw fail('a', `credentialKeys is not an array of key names (got ${JSON.stringify(owner.credentialKeys)})`);
|
|
182
|
+
}
|
|
183
|
+
if (owner.credentialIdCollections !== undefined && (!Array.isArray(owner.credentialIdCollections) || owner.credentialIdCollections.some((k) => !nonEmptyString(k) || k.includes('/')))) {
|
|
184
|
+
throw fail('a', `credentialIdCollections is not an array of collection ids (got ${JSON.stringify(owner.credentialIdCollections)})`);
|
|
185
|
+
}
|
|
186
|
+
if (owner.irrecoverable !== undefined && typeof owner.irrecoverable !== 'boolean') throw fail('a', `irrecoverable is ${JSON.stringify(owner.irrecoverable)}, not a boolean`);
|
|
187
|
+
if (owner[STORE_OWNER_RESTORE_METHOD] !== undefined && typeof owner[STORE_OWNER_RESTORE_METHOD] !== 'function') throw fail('a', `${STORE_OWNER_RESTORE_METHOD} is not a function`);
|
|
188
|
+
const merges = mergeHas.length === STORE_OWNER_MERGE_METHODS.length;
|
|
189
|
+
const restores = typeof owner[STORE_OWNER_RESTORE_METHOD] === 'function';
|
|
190
|
+
passed('a');
|
|
191
|
+
|
|
192
|
+
// the fixture
|
|
193
|
+
const handle = (h) => h && typeof h === 'object' && nonEmptyString(h.user_id);
|
|
194
|
+
if (!handle(user)) throw fail('fixture', 'a user { user_id, verified_email, wallet_address } is required');
|
|
195
|
+
if (Object.prototype.hasOwnProperty.call(fixture, 'bystander')) {
|
|
196
|
+
throw fail('fixture', '`bystander` is not an option: pass `bystanders: [sameEmailAndWallet, sameWalletOnly, sameEmailOnly]` (three other accounts)');
|
|
197
|
+
}
|
|
198
|
+
if (!nonEmptyString(user.verified_email) || !nonEmptyString(user.wallet_address)) throw fail('fixture', 'the user has a non-empty verified_email and wallet_address (a bystander shares one or both)');
|
|
199
|
+
if (!Array.isArray(bystanders) || bystanders.length !== 3 || !bystanders.every(handle)) {
|
|
200
|
+
throw fail('fixture', 'bystanders: [sameEmailAndWallet, sameWalletOnly, sameEmailOnly] (three other accounts { user_id, verified_email, wallet_address }) is required');
|
|
201
|
+
}
|
|
202
|
+
const ids = new Set([user.user_id, ...bystanders.map((b) => b.user_id)]);
|
|
203
|
+
if (ids.size !== 4) throw fail('fixture', 'the user and the three bystanders are four different accounts (four user_ids)');
|
|
204
|
+
const [both, walletOnly, emailOnly] = bystanders;
|
|
205
|
+
const sameEmail = (b) => b.verified_email === user.verified_email;
|
|
206
|
+
const sameWallet = (b) => b.wallet_address === user.wallet_address;
|
|
207
|
+
if (!sameEmail(both) || !sameWallet(both)) throw fail('fixture', 'bystanders[0] shares the user\'s verified_email and wallet_address');
|
|
208
|
+
if (!sameWallet(walletOnly) || sameEmail(walletOnly)) throw fail('fixture', 'bystanders[1] shares the user\'s wallet_address and NOT its verified_email');
|
|
209
|
+
if (!sameEmail(emailOnly) || sameWallet(emailOnly)) throw fail('fixture', 'bystanders[2] shares the user\'s verified_email and NOT its wallet_address');
|
|
210
|
+
if (typeof snapshot !== 'function') throw fail('fixture', 'snapshot() -> Map<path, value> of the seeded store is required (collateral writes are judged from it)');
|
|
211
|
+
if (!clientTarget || !nonEmptyString(clientTarget.databaseId) || (clientTarget.projectId !== undefined && !nonEmptyString(clientTarget.projectId))) {
|
|
212
|
+
throw fail('fixture', 'clientTarget { databaseId, projectId? }, read by the caller from the Firestore client it injected into the owner, is required (storeTarget() is judged against it)');
|
|
213
|
+
}
|
|
214
|
+
if (merges && (!nonEmptyString(pair?.from?.user_id) || !nonEmptyString(pair?.into?.user_id))) {
|
|
215
|
+
throw fail('fixture', 'a merge owner is checked with a pair { from, into } whose merge footprint has a move');
|
|
216
|
+
}
|
|
217
|
+
const rawSnap = async () => {
|
|
218
|
+
const m = await snapshot();
|
|
219
|
+
if (!(m instanceof Map)) throw fail('fixture', `snapshot() answered ${Object.prototype.toString.call(m)}, not a Map`);
|
|
220
|
+
return m;
|
|
221
|
+
};
|
|
222
|
+
const snap = async () => new Map([...(await rawSnap())].map(([p, v]) => [p, asText(v)]));
|
|
223
|
+
/** Paths whose presence or value differs between two snapshots. */
|
|
224
|
+
const diff = (x, y) => sortedUnique([...x.keys(), ...y.keys()].filter((p) => x.get(p) !== y.get(p)));
|
|
225
|
+
|
|
226
|
+
// (f) the database the owner's client targets
|
|
227
|
+
const t = await call('f', 'storeTarget()', () => owner.storeTarget());
|
|
228
|
+
if (!t || !nonEmptyString(t.databaseId)) throw fail('f', `storeTarget() answered ${JSON.stringify(t ?? null)?.slice(0, 200)}, not { databaseId: string }`);
|
|
229
|
+
if (t.projectId !== undefined && !nonEmptyString(t.projectId)) throw fail('f', `storeTarget().projectId is ${JSON.stringify(t.projectId)}, not a non-empty string`);
|
|
230
|
+
if (t.databaseId !== clientTarget.databaseId) throw fail('f', `storeTarget() reports database '${t.databaseId}', but the client the caller injected targets '${clientTarget.databaseId}'`);
|
|
231
|
+
if (t.projectId !== undefined && clientTarget.projectId !== undefined && t.projectId !== clientTarget.projectId) {
|
|
232
|
+
throw fail('f', `storeTarget() reports project '${t.projectId}', but the client the caller injected targets '${clientTarget.projectId}'`);
|
|
233
|
+
}
|
|
234
|
+
passed('f');
|
|
235
|
+
|
|
236
|
+
// (b) the delete footprint
|
|
237
|
+
const checkFootprint = (clause, label, fp, { merge = false } = {}) => {
|
|
238
|
+
const why = footprintViolation(fp, { merge, credentialKeys: owner.credentialKeys });
|
|
239
|
+
if (why) throw fail(clause, `${label}: ${why}`);
|
|
240
|
+
return fp;
|
|
241
|
+
};
|
|
242
|
+
const footprintOf = (clause, who, label) => call(clause, label, () => owner.footprintForUser(who));
|
|
243
|
+
const footprint = (clause) => footprintOf(clause, user, 'footprintForUser(user)');
|
|
244
|
+
const sameFootprint = (a, b) => canonicalJson({ e: a.entries, l: a.leftovers, b: a.blockers }) === canonicalJson({ e: b.entries, l: b.leftovers, b: b.blockers });
|
|
245
|
+
const samePaths = (a, b) => canonicalJson(sortedUnique(a.entries.map((e) => e.path))) === canonicalJson(sortedUnique(b.entries.map((e) => e.path)));
|
|
246
|
+
|
|
247
|
+
const s0 = await snap();
|
|
248
|
+
const fp1 = checkFootprint('b', 'footprintForUser(user)', await footprint('b'));
|
|
249
|
+
const fp2 = checkFootprint('b', 'footprintForUser(user)', await footprint('b'));
|
|
250
|
+
if (!sameFootprint(fp1, fp2)) throw fail('b', 'two reads with no write between them answered different footprints (read twice); a footprint is read-only and deterministic');
|
|
251
|
+
const wrote = diff(s0, await snap());
|
|
252
|
+
if (wrote.length) throw fail('b', `reading the footprint changed the store at ${wrote.join(', ')}`);
|
|
253
|
+
const paths = fp1.entries.map((e) => e.path);
|
|
254
|
+
if (emptyFootprint && paths.length) throw fail('fixture', `emptyFootprint is declared but the footprint lists ${paths.length} entr(ies)`);
|
|
255
|
+
if (!emptyFootprint && !paths.length) throw fail('fixture', 'the user\'s footprint has no entry, so deleteForUser cannot be shown exact; seed one (or declare emptyFootprint: true for an owner that never lists any)');
|
|
256
|
+
if (fp1.blockers.length) throw fail('fixture', `the user's footprint carries blocker(s) ${fp1.blockers.map((b) => b.code).join(', ')}; seed a user this owner would delete`);
|
|
257
|
+
passed('b');
|
|
258
|
+
|
|
259
|
+
// (g) three bystanders sharing the email and/or the wallet
|
|
260
|
+
const labels = ['bystanders[0] (same email and wallet)', 'bystanders[1] (same wallet only)', 'bystanders[2] (same email only)'];
|
|
261
|
+
const bystanderFps = [];
|
|
262
|
+
for (const [i, b] of bystanders.entries()) {
|
|
263
|
+
const bfp = checkFootprint('g', `footprintForUser(${labels[i]})`, await footprintOf('g', b, `footprintForUser(${labels[i]})`));
|
|
264
|
+
if (!emptyFootprint && !bfp.entries.length) throw fail('fixture', `the footprint of ${labels[i]} has no entry; seed one so an owner that reaches it can be caught`);
|
|
265
|
+
const theirs = new Set(bfp.entries.map((e) => e.path));
|
|
266
|
+
const shared = paths.filter((p) => theirs.has(p));
|
|
267
|
+
if (shared.length) throw fail('g', `footprintForUser(user) lists ${shared.join(', ')}, which the footprint of ${labels[i]} also lists: a footprint is read by the account id, never by its email or wallet`);
|
|
268
|
+
bystanderFps.push(bfp);
|
|
269
|
+
}
|
|
270
|
+
passed('g');
|
|
271
|
+
|
|
272
|
+
// (n) no path names a credential
|
|
273
|
+
{
|
|
274
|
+
const declared = new Set(owner.credentialIdCollections ?? []);
|
|
275
|
+
const raw = await rawSnap();
|
|
276
|
+
for (const e of fp1.entries) {
|
|
277
|
+
const collectionId = e.path.split('/').slice(-2)[0];
|
|
278
|
+
if (declared.has(collectionId) && (e.pathIsSecret !== true || !isSecretPath(e.path))) {
|
|
279
|
+
throw fail('n', `an entry of ${collectionId}, declared in credentialIdCollections, is not a secretPath with pathIsSecret: true`);
|
|
280
|
+
}
|
|
281
|
+
if (e.pathIsSecret === true && !declared.has(collectionId)) {
|
|
282
|
+
throw fail('n', `an entry of ${collectionId} is pathIsSecret, but credentialIdCollections does not declare ${collectionId}`);
|
|
283
|
+
}
|
|
284
|
+
const secrets = credentialValues(asDoc(raw.get(e.path)), { keys: owner.credentialKeys });
|
|
285
|
+
const named = e.path.split('/').some((seg) => secrets.includes(seg));
|
|
286
|
+
if (named) {
|
|
287
|
+
throw fail('n', `an entry path in ${e.path.split('/').slice(0, -1).join('/')} has a segment equal to a credential its document holds (redacted in its data): the document id is a credential; declare ${collectionId} in credentialIdCollections and name it secretPath(collection, id)`);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
passed('n');
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// (j) bounded reads
|
|
294
|
+
if (typeof countUnboundedReads !== 'function') {
|
|
295
|
+
skipped('j', 'no countUnboundedReads hook: the caller gave no way to count reads without a limit');
|
|
296
|
+
} else {
|
|
297
|
+
const reads = [['footprintForUser(user)', () => owner.footprintForUser(user)]];
|
|
298
|
+
if (merges) reads.push(['footprintForMerge(pair)', () => owner.footprintForMerge(pair)]);
|
|
299
|
+
for (const [label, fn] of reads) {
|
|
300
|
+
const n0 = countUnboundedReads();
|
|
301
|
+
await call('j', label, fn);
|
|
302
|
+
const n = countUnboundedReads() - n0;
|
|
303
|
+
if (n) throw fail('j', `${label} made ${n} read(s) without a limit; every read carries .limit(ACCOUNT_READ_LIMIT)`);
|
|
304
|
+
}
|
|
305
|
+
passed('j');
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// (d) a failed read
|
|
309
|
+
if (typeof withFailingReads !== 'function') {
|
|
310
|
+
skipped('d', 'no withFailingReads hook: the caller gave no way to make its store fail');
|
|
311
|
+
} else {
|
|
312
|
+
const underFailure = [['footprintForUser(user)', () => owner.footprintForUser(user)]];
|
|
313
|
+
if (merges) underFailure.push(['footprintForMerge(pair)', () => owner.footprintForMerge(pair)]);
|
|
314
|
+
for (const [label, fn] of underFailure) {
|
|
315
|
+
let answer;
|
|
316
|
+
try {
|
|
317
|
+
answer = await withFailingReads(fn);
|
|
318
|
+
} catch (e) {
|
|
319
|
+
if (!(e instanceof AccountAdminError) || e.code !== R.ACCOUNT_STORE_READ_FAILED) {
|
|
320
|
+
throw fail('d', `${label} under failing reads threw ${describe(e)}, not an AccountAdminError ACCOUNT_STORE_READ_FAILED`);
|
|
321
|
+
}
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
throw fail('d', `${label} answered a footprint while every read failed (${JSON.stringify(answer)?.slice(0, 200)}); a failed read is never an empty footprint`);
|
|
325
|
+
}
|
|
326
|
+
passed('d');
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// (e) the merge round trip, then (i) a rollback over a changed value
|
|
330
|
+
if (!merges) {
|
|
331
|
+
skipped('e', 'not a merge owner (no merge methods): on a merge its footprintForUser entries and leftovers are listed as staying on the from-record');
|
|
332
|
+
skipped('i', 'not a merge owner');
|
|
333
|
+
} else {
|
|
334
|
+
const mergeFootprint = async (clause, label) => checkFootprint(clause, label, await call(clause, label, () => owner.footprintForMerge(pair)), { merge: true });
|
|
335
|
+
const sameMerge = (a, b) => canonicalJson(a.entries) === canonicalJson(b.entries);
|
|
336
|
+
const m1 = await mergeFootprint('e', 'footprintForMerge(pair)');
|
|
337
|
+
if (!m1.entries.length) throw fail('fixture', 'the pair\'s merge footprint has no move; seed one so the merge round trip can fail');
|
|
338
|
+
if (m1.blockers.length) throw fail('fixture', `the pair's merge footprint carries blocker(s) ${m1.blockers.map((b) => b.code).join(', ')}`);
|
|
339
|
+
if (!sameMerge(m1, await mergeFootprint('e', 'footprintForMerge(pair), read twice'))) throw fail('e', 'two merge-footprint reads with no write between them differ');
|
|
340
|
+
const movePaths = sortedUnique(m1.entries.map((m) => m.path));
|
|
341
|
+
for (const [label, opts] of [['mergeForUser(pair, a stale expectPaths)', { expectPaths: [...movePaths.slice(1), NOT_PLANNED] }], ['mergeForUser(pair, no expectPaths)', {}]]) {
|
|
342
|
+
const before = await snap();
|
|
343
|
+
await refuses('e', label, R.ACCOUNT_PLAN_STALE, () => owner.mergeForUser(pair, opts));
|
|
344
|
+
const changed = diff(before, await snap());
|
|
345
|
+
if (changed.length) throw fail('e', `${label} refused but changed the store at ${changed.join(', ')}: it wrote before refusing`);
|
|
346
|
+
}
|
|
347
|
+
const all = sortedUnique(m1.entries.map(moveKey));
|
|
348
|
+
const covers = (label, r, a, b) => {
|
|
349
|
+
if (!Array.isArray(r?.[a]) || !Array.isArray(r?.[b])) throw fail('e', `${label} returned ${JSON.stringify(r)?.slice(0, 200)}, not { ${a}, ${b} }`);
|
|
350
|
+
for (const m of [...r[a], ...r[b]]) if (!nonEmptyString(m?.path) || !nonEmptyString(m?.field)) throw fail('e', `${label}: ${a}/${b} items are { path, field, key? } (got ${JSON.stringify(m)?.slice(0, 200)})`);
|
|
351
|
+
const got = sortedUnique([...r[a], ...r[b]].map(moveKey));
|
|
352
|
+
if (canonicalJson(got) !== canonicalJson(all)) throw fail('e', `${label}: ${a} + ${b} cover ${got.join(', ') || 'nothing'}, not the planned moves ${all.join(', ')}`);
|
|
353
|
+
return new Set(r[a].map(moveKey));
|
|
354
|
+
};
|
|
355
|
+
const merge = async (clause) => covers('mergeForUser(pair, { expectPaths })', await call(clause, 'mergeForUser(pair, { expectPaths })', () => owner.mergeForUser(pair, { expectPaths: movePaths })), 'moved', 'remaining');
|
|
356
|
+
const moved = await merge('e');
|
|
357
|
+
const after = await mergeFootprint('e', 'footprintForMerge(pair) after mergeForUser');
|
|
358
|
+
const still = after.entries.map(moveKey).filter((k) => moved.has(k));
|
|
359
|
+
if (still.length) throw fail('e', `footprintForMerge still lists ${still.join(', ')} after mergeForUser reported it moved`);
|
|
360
|
+
const receiptMoves = () => JSON.parse(JSON.stringify(m1.entries.map(({ summary, ...m }) => m)));
|
|
361
|
+
await call('e', 'validateRollbackMoves(the receipt\'s moves, pair)', () => owner.validateRollbackMoves(receiptMoves(), pair));
|
|
362
|
+
const edited = receiptMoves();
|
|
363
|
+
edited[0].path = NOT_PLANNED;
|
|
364
|
+
for (const [label, fn] of [['validateRollbackMoves(an edited move, pair)', () => owner.validateRollbackMoves(edited, pair)], ['rollbackMergeForUser(an edited move, pair)', () => owner.rollbackMergeForUser(edited, pair)]]) {
|
|
365
|
+
const before = await snap();
|
|
366
|
+
await refuses('e', label, R.ACCOUNT_RECEIPT_INVALID, fn);
|
|
367
|
+
const changed = diff(before, await snap());
|
|
368
|
+
if (changed.length) throw fail('e', `${label} refused but changed the store at ${changed.join(', ')}: every move is validated before the first write`);
|
|
369
|
+
}
|
|
370
|
+
covers('rollbackMergeForUser(moves, pair)', await call('e', 'rollbackMergeForUser(moves, pair)', () => owner.rollbackMergeForUser(receiptMoves(), pair)), 'restored', 'remaining');
|
|
371
|
+
const again = new Set((await mergeFootprint('e', 'footprintForMerge(pair) after rollbackMergeForUser')).entries.map(moveKey));
|
|
372
|
+
const missing = all.filter((k) => !again.has(k));
|
|
373
|
+
if (missing.length) throw fail('e', `${missing.join(', ')} not listed again after rollbackMergeForUser: the rollback did not restore it`);
|
|
374
|
+
passed('e');
|
|
375
|
+
|
|
376
|
+
if (typeof changeAfterMerge !== 'function') {
|
|
377
|
+
skipped('i', 'no changeAfterMerge hook: the caller gave no way to change a moved value after the merge');
|
|
378
|
+
} else {
|
|
379
|
+
const valueAt = (s, m) => {
|
|
380
|
+
const doc = asDoc(s.get(m.path));
|
|
381
|
+
if (doc === undefined) return undefined;
|
|
382
|
+
const v = doc?.[m.field];
|
|
383
|
+
return canonicalJson(m.key === undefined ? (v ?? null) : (v?.[m.key] ?? null));
|
|
384
|
+
};
|
|
385
|
+
const moved2 = await merge('i');
|
|
386
|
+
const target = m1.entries.find((m) => moved2.has(moveKey(m)));
|
|
387
|
+
if (!target) throw fail('fixture', 'the merge moved nothing, so no moved value can be changed');
|
|
388
|
+
await call('i', `changeAfterMerge(${moveKey(target)})`, () => changeAfterMerge(JSON.parse(JSON.stringify(target))));
|
|
389
|
+
const s1 = await snap();
|
|
390
|
+
if (valueAt(s1, target) === canonicalJson(target.to ?? null)) throw fail('fixture', `changeAfterMerge(${moveKey(target)}) left the moved value as the merge wrote it`);
|
|
391
|
+
const r = await call('i', 'rollbackMergeForUser(moves, pair)', () => owner.rollbackMergeForUser(receiptMoves(), pair));
|
|
392
|
+
covers('rollbackMergeForUser(moves, pair) after a change', r, 'restored', 'remaining');
|
|
393
|
+
if (r.restored.some((m) => moveKey(m) === moveKey(target))) throw fail('i', `rollbackMergeForUser reported ${moveKey(target)} restored, but its value had changed since the merge`);
|
|
394
|
+
if (valueAt(await snap(), target) !== valueAt(s1, target)) throw fail('i', `rollbackMergeForUser overwrote ${moveKey(target)}, a value changed since the merge`);
|
|
395
|
+
passed('i');
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// (h) a delete with a blocker present
|
|
400
|
+
if (typeof withBlocker !== 'function') {
|
|
401
|
+
skipped('h', 'no withBlocker hook: the caller gave no way to make deleting the user blocked');
|
|
402
|
+
} else {
|
|
403
|
+
await withBlocker(async () => {
|
|
404
|
+
const fp = checkFootprint('h', 'footprintForUser(user) with a blocker', await footprint('h'));
|
|
405
|
+
if (!fp.blockers.length) throw fail('fixture', 'withBlocker set nothing the user\'s footprint reports as a blocker');
|
|
406
|
+
const before = await snap();
|
|
407
|
+
await refuses('h', 'deleteForUser(user, the plan) with a blocker present', fp.blockers.map((b) => b.code), () => owner.deleteForUser(user, planOf(fp)));
|
|
408
|
+
const changed = diff(before, await snap());
|
|
409
|
+
if (changed.length) throw fail('h', `deleteForUser refused for a blocker but changed the store at ${changed.join(', ')}`);
|
|
410
|
+
});
|
|
411
|
+
passed('h');
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// (l) a document changed after the footprint
|
|
415
|
+
if (emptyFootprint) {
|
|
416
|
+
skipped('l', 'emptyFootprint: no document to change');
|
|
417
|
+
} else if (typeof changeAfterFootprint !== 'function') {
|
|
418
|
+
if (owner.irrecoverable === true) throw fail('l', 'an irrecoverable owner is checked with changeAfterFootprint(path): its in-transaction data comparison is the only thing between a stale plan and a delete nobody can undo');
|
|
419
|
+
skipped('l', 'no changeAfterFootprint hook: the caller gave no way to change a document after the footprint');
|
|
420
|
+
} else {
|
|
421
|
+
const fp = checkFootprint('l', 'footprintForUser(user)', await footprint('l'));
|
|
422
|
+
const target = fp.entries.find((e) => e.volatile !== true);
|
|
423
|
+
if (!target) throw fail('fixture', 'every entry of the user\'s footprint is volatile; no document can be shown stale');
|
|
424
|
+
const before = await snap();
|
|
425
|
+
await call('l', `changeAfterFootprint(${target.path})`, () => changeAfterFootprint(target.path));
|
|
426
|
+
const s1 = await snap();
|
|
427
|
+
if (s1.get(target.path) === before.get(target.path)) throw fail('fixture', `changeAfterFootprint(${target.path}) left the document as it was`);
|
|
428
|
+
if (!samePaths(fp, checkFootprint('l', 'footprintForUser(user) after the change', await footprint('l')))) throw fail('fixture', 'changeAfterFootprint changed which documents the footprint lists; it changes one document\'s data');
|
|
429
|
+
await refuses('l', 'deleteForUser(user, the plan of the footprint before the change)', R.ACCOUNT_PLAN_STALE, () => owner.deleteForUser(user, planOf(fp)));
|
|
430
|
+
const changed = diff(s1, await snap());
|
|
431
|
+
if (changed.length) throw fail('l', `deleteForUser refused a stale plan but changed the store at ${changed.join(', ')}`);
|
|
432
|
+
passed('l');
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// (c) delete exactly, nothing else (last but restore: it deletes the fixture)
|
|
436
|
+
const fpNow = checkFootprint('c', 'footprintForUser(user)', await footprint('c'));
|
|
437
|
+
if (!samePaths(fp1, fpNow)) throw fail('fixture', 'the documents of the user\'s footprint changed during the earlier clauses; check delete against an unchanged set');
|
|
438
|
+
const plan = planOf(fpNow);
|
|
439
|
+
const dataKeys = Object.keys(plan.expectData);
|
|
440
|
+
const staleVariants = [
|
|
441
|
+
['deleteForUser(user, a stale expectPaths)', {
|
|
442
|
+
expectPaths: [...paths.slice(1), NOT_PLANNED],
|
|
443
|
+
expectData: { ...Object.fromEntries(Object.entries(plan.expectData).filter(([p]) => p !== paths[0])), [NOT_PLANNED]: entryDataHash(null) },
|
|
444
|
+
}],
|
|
445
|
+
['deleteForUser(user, no expectPaths)', {}],
|
|
446
|
+
];
|
|
447
|
+
if (dataKeys.length) {
|
|
448
|
+
staleVariants.push(['deleteForUser(user, { expectPaths } without expectData)', { expectPaths: plan.expectPaths }]);
|
|
449
|
+
staleVariants.push(['deleteForUser(user, a planned data hash other than the document\'s)', { expectPaths: plan.expectPaths, expectData: { ...plan.expectData, [dataKeys[0]]: entryDataHash({ conformance: 'not the planned data' }) } }]);
|
|
450
|
+
}
|
|
451
|
+
for (const [label, opts] of staleVariants) {
|
|
452
|
+
const before = await snap();
|
|
453
|
+
await refuses('c', label, R.ACCOUNT_PLAN_STALE, () => owner.deleteForUser(user, opts));
|
|
454
|
+
const changed = diff(before, await snap());
|
|
455
|
+
if (changed.length) throw fail('c', `${label} refused but changed the store at ${changed.join(', ')}: it wrote before refusing`);
|
|
456
|
+
}
|
|
457
|
+
const sBefore = await snap();
|
|
458
|
+
const r = await call('c', 'deleteForUser(user, { expectPaths, expectData })', () => owner.deleteForUser(user, planOf(fpNow)));
|
|
459
|
+
if (!Array.isArray(r?.deleted) || !Array.isArray(r?.remaining) || [...r.deleted, ...r.remaining].some((p) => typeof p !== 'string')) {
|
|
460
|
+
throw fail('c', `deleteForUser returned ${JSON.stringify(r)?.slice(0, 200)}, not { deleted: string[], remaining: string[] }`);
|
|
461
|
+
}
|
|
462
|
+
const covered = [...r.deleted, ...r.remaining];
|
|
463
|
+
if (new Set(covered).size !== covered.length || canonicalJson(sortedUnique(covered)) !== canonicalJson(sortedUnique(paths))) {
|
|
464
|
+
throw fail('c', `deleted + remaining (${covered.join(', ') || 'nothing'}) are not exactly expectPaths (${paths.join(', ') || 'nothing'}), each once`);
|
|
465
|
+
}
|
|
466
|
+
const left = new Set((await footprint('c')).entries.map((e) => e.path));
|
|
467
|
+
const lied = r.deleted.filter((p) => left.has(p));
|
|
468
|
+
if (lied.length) throw fail('c', `a fresh footprint still lists ${lied.join(', ')} that deleteForUser reported deleted`);
|
|
469
|
+
const vanished = r.remaining.filter((p) => !left.has(p));
|
|
470
|
+
if (vanished.length) throw fail('c', `deleteForUser reported ${vanished.join(', ')} remaining but a fresh footprint does not list it`);
|
|
471
|
+
const sAfter = await snap();
|
|
472
|
+
const changed = diff(sBefore, sAfter);
|
|
473
|
+
const collateral = changed.filter((p) => !r.deleted.includes(p));
|
|
474
|
+
if (collateral.length) throw fail('c', `deleteForUser changed ${collateral.join(', ')}, which is not a path it deleted (a collateral write)`);
|
|
475
|
+
const unseen = r.deleted.filter((p) => !changed.includes(p));
|
|
476
|
+
if (unseen.length) throw fail('c', `deleteForUser reported ${unseen.join(', ')} deleted, but the store snapshot shows no change there`);
|
|
477
|
+
for (const [i, b] of bystanders.entries()) {
|
|
478
|
+
const after = checkFootprint('c', `footprintForUser(${labels[i]}) after the delete`, await footprintOf('c', b, `footprintForUser(${labels[i]})`));
|
|
479
|
+
if (!sameFootprint(bystanderFps[i], after)) throw fail('c', `deleting the user changed the footprint of ${labels[i]}`);
|
|
480
|
+
}
|
|
481
|
+
passed('c');
|
|
482
|
+
|
|
483
|
+
// (m), (k) restore
|
|
484
|
+
if (!restores) {
|
|
485
|
+
skipped('m', `no ${STORE_OWNER_RESTORE_METHOD}`);
|
|
486
|
+
skipped('k', `no ${STORE_OWNER_RESTORE_METHOD}: a restoreDelete reports this owner's absent entries as not restorable`);
|
|
487
|
+
} else {
|
|
488
|
+
const asReceipt = (es) => JSON.parse(JSON.stringify(es.filter((e) => e.volatile !== true && e.pathIsSecret !== true)));
|
|
489
|
+
const receiptEntries = asReceipt(fpNow.entries.filter((e) => r.deleted.includes(e.path)));
|
|
490
|
+
if (!receiptEntries.length) throw fail('fixture', 'the delete removed no restorable entry, so restore cannot be shown');
|
|
491
|
+
|
|
492
|
+
// (m) a path that is not this account's: refused before any write
|
|
493
|
+
const foreign = [['an entry re-pathed outside the store', { ...receiptEntries[0], path: NOT_PLANNED }]];
|
|
494
|
+
const theirs = asReceipt(bystanderFps[0].entries)[0];
|
|
495
|
+
if (theirs) foreign.push([`${labels[0]}'s own entry`, theirs]);
|
|
496
|
+
for (const [label, entry] of foreign) {
|
|
497
|
+
const before = await snap();
|
|
498
|
+
await refuses('m', `${STORE_OWNER_RESTORE_METHOD}(user, the user's entries and ${label})`, R.ACCOUNT_RECEIPT_INVALID, () => owner[STORE_OWNER_RESTORE_METHOD](user, [...JSON.parse(JSON.stringify(receiptEntries)), entry]));
|
|
499
|
+
const moved = diff(before, await snap());
|
|
500
|
+
if (moved.length) throw fail('m', `${STORE_OWNER_RESTORE_METHOD} refused ${label} but changed the store at ${moved.join(', ')}: every entry is checked before the first write`);
|
|
501
|
+
}
|
|
502
|
+
passed('m');
|
|
503
|
+
|
|
504
|
+
// (k) create-only
|
|
505
|
+
const s1 = await snap();
|
|
506
|
+
const rr = await call('k', `${STORE_OWNER_RESTORE_METHOD}(user, entries)`, () => owner[STORE_OWNER_RESTORE_METHOD](user, receiptEntries));
|
|
507
|
+
if (!Array.isArray(rr?.restored) || !Array.isArray(rr?.remaining) || [...rr.restored, ...rr.remaining].some((p) => typeof p !== 'string')) {
|
|
508
|
+
throw fail('k', `${STORE_OWNER_RESTORE_METHOD} returned ${JSON.stringify(rr)?.slice(0, 200)}, not { restored: string[], remaining: string[] }`);
|
|
509
|
+
}
|
|
510
|
+
const want = sortedUnique(receiptEntries.map((e) => e.path));
|
|
511
|
+
if (canonicalJson(sortedUnique([...rr.restored, ...rr.remaining])) !== canonicalJson(want)) throw fail('k', `restored + remaining do not cover the entries ${want.join(', ')}`);
|
|
512
|
+
const back = new Set((await footprint('k')).entries.map((e) => e.path));
|
|
513
|
+
const notBack = rr.restored.filter((p) => !back.has(p));
|
|
514
|
+
if (notBack.length) throw fail('k', `reported ${notBack.join(', ')} restored, but a fresh footprint does not list it`);
|
|
515
|
+
const s2 = await snap();
|
|
516
|
+
const outside = diff(s1, s2).filter((p) => !rr.restored.includes(p));
|
|
517
|
+
if (outside.length) throw fail('k', `${STORE_OWNER_RESTORE_METHOD} changed ${outside.join(', ')}, which it did not restore`);
|
|
518
|
+
await call('k', `${STORE_OWNER_RESTORE_METHOD}(user, entries) again`, () => owner[STORE_OWNER_RESTORE_METHOD](user, receiptEntries));
|
|
519
|
+
const overwrote = diff(s2, await snap());
|
|
520
|
+
if (overwrote.length) throw fail('k', `a second ${STORE_OWNER_RESTORE_METHOD} with every document present changed ${overwrote.join(', ')}: it overwrote an existing document`);
|
|
521
|
+
passed('k');
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
checks.sort((x, y) => ORDER.indexOf(x.clause) - ORDER.indexOf(y.clause));
|
|
525
|
+
return Object.freeze({ owner: owner.name, checks: Object.freeze(checks) });
|
|
526
|
+
}
|