@kinloom/server 0.0.0-stage → 0.1.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/adapters/googleOAuthProvider.d.ts +38 -0
- package/dist/adapters/googleOAuthProvider.d.ts.map +1 -0
- package/dist/adapters/googleOAuthProvider.js +99 -0
- package/dist/adapters/googleOAuthProvider.js.map +1 -0
- package/dist/adapters/inMemory.d.ts +171 -0
- package/dist/adapters/inMemory.d.ts.map +1 -0
- package/dist/adapters/inMemory.js +668 -0
- package/dist/adapters/inMemory.js.map +1 -0
- package/dist/adapters/systemClock.d.ts +16 -0
- package/dist/adapters/systemClock.d.ts.map +1 -0
- package/dist/adapters/systemClock.js +40 -0
- package/dist/adapters/systemClock.js.map +1 -0
- package/dist/crypto.d.ts +19 -0
- package/dist/crypto.d.ts.map +1 -0
- package/dist/crypto.js +159 -0
- package/dist/crypto.js.map +1 -0
- package/dist/errors.d.ts +13 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +73 -0
- package/dist/errors.js.map +1 -0
- package/dist/http/authMiddleware.d.ts +21 -0
- package/dist/http/authMiddleware.d.ts.map +1 -0
- package/dist/http/authMiddleware.js +155 -0
- package/dist/http/authMiddleware.js.map +1 -0
- package/dist/http/router.d.ts +20 -0
- package/dist/http/router.d.ts.map +1 -0
- package/dist/http/router.js +814 -0
- package/dist/http/router.js.map +1 -0
- package/dist/http/session.d.ts +71 -0
- package/dist/http/session.d.ts.map +1 -0
- package/dist/http/session.js +170 -0
- package/dist/http/session.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/services/adminService.d.ts +55 -0
- package/dist/services/adminService.d.ts.map +1 -0
- package/dist/services/adminService.js +168 -0
- package/dist/services/adminService.js.map +1 -0
- package/dist/services/authService.d.ts +71 -0
- package/dist/services/authService.d.ts.map +1 -0
- package/dist/services/authService.js +268 -0
- package/dist/services/authService.js.map +1 -0
- package/dist/services/bundleReadService.d.ts +69 -0
- package/dist/services/bundleReadService.d.ts.map +1 -0
- package/dist/services/bundleReadService.js +177 -0
- package/dist/services/bundleReadService.js.map +1 -0
- package/dist/services/mediaService.d.ts +44 -0
- package/dist/services/mediaService.d.ts.map +1 -0
- package/dist/services/mediaService.js +228 -0
- package/dist/services/mediaService.js.map +1 -0
- package/dist/services/talkService.d.ts +31 -0
- package/dist/services/talkService.d.ts.map +1 -0
- package/dist/services/talkService.js +59 -0
- package/dist/services/talkService.js.map +1 -0
- package/dist/services/wikiWriteService.d.ts +101 -0
- package/dist/services/wikiWriteService.d.ts.map +1 -0
- package/dist/services/wikiWriteService.js +809 -0
- package/dist/services/wikiWriteService.js.map +1 -0
- package/dist/testing/adapterContractSuite.d.ts +23 -0
- package/dist/testing/adapterContractSuite.d.ts.map +1 -0
- package/dist/testing/adapterContractSuite.js +765 -0
- package/dist/testing/adapterContractSuite.js.map +1 -0
- package/package.json +46 -4
- package/README.md +0 -3
|
@@ -0,0 +1,765 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { asBatchId, asRevisionId, KinloomSiteConfigSchema, } from '@kinloom/core';
|
|
3
|
+
import { FakeAuthProvider, InMemoryEmailSender, } from '../adapters/inMemory.js';
|
|
4
|
+
import { FixedClock } from '../adapters/systemClock.js';
|
|
5
|
+
import { createKinloomServer } from '../http/router.js';
|
|
6
|
+
import { signSessionToken } from '../http/session.js';
|
|
7
|
+
export const CONTRACT_TEST_ORIGIN = 'https://archive.example.test';
|
|
8
|
+
export const CONTRACT_SESSION_SECRET = 'kinloom-contract-suite-secret-key-2026-0123456789';
|
|
9
|
+
export function createSyntheticSiteConfig() {
|
|
10
|
+
return KinloomSiteConfigSchema.parse({
|
|
11
|
+
schemaVersion: 1,
|
|
12
|
+
site: {
|
|
13
|
+
title: 'Alder Synthetic Family Archive',
|
|
14
|
+
familyName: 'Alder',
|
|
15
|
+
gedcomSourceName: 'ALDER_SYNTHETIC',
|
|
16
|
+
footerCredit: 'Built with Kinloom',
|
|
17
|
+
},
|
|
18
|
+
branches: [
|
|
19
|
+
{
|
|
20
|
+
id: 'alder',
|
|
21
|
+
label: 'Alder Line',
|
|
22
|
+
surnames: ['Alder'],
|
|
23
|
+
lineageGroups: ['alder_core'],
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
privacy: {
|
|
27
|
+
livingCutoffYears: 101,
|
|
28
|
+
referenceYear: 2026,
|
|
29
|
+
privateModeShowsBirthYear: true,
|
|
30
|
+
stripLivingPortraits: true,
|
|
31
|
+
stripLivingResidenceSpans: true,
|
|
32
|
+
},
|
|
33
|
+
wiki: {
|
|
34
|
+
allowTalk: true,
|
|
35
|
+
allowWatchlists: false,
|
|
36
|
+
maxUploadBytes: 15 * 1024 * 1024,
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
export function createSyntheticHistoricalPerson(overrides = {}) {
|
|
41
|
+
return {
|
|
42
|
+
id: 'per_arthur_alder',
|
|
43
|
+
slug: 'arthur-alder',
|
|
44
|
+
full_name: 'Arthur Alder',
|
|
45
|
+
given_name: 'Arthur',
|
|
46
|
+
surname: 'Alder',
|
|
47
|
+
alt_names: [],
|
|
48
|
+
sex: 'M',
|
|
49
|
+
is_living: false,
|
|
50
|
+
lineage_group: 'alder_core',
|
|
51
|
+
birth_edtf: '1872-04-14',
|
|
52
|
+
death_edtf: '1948-11-02',
|
|
53
|
+
portrait_url: null,
|
|
54
|
+
...overrides,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
export function createSyntheticLivingPerson(overrides = {}) {
|
|
58
|
+
return {
|
|
59
|
+
id: 'per_clara_alder',
|
|
60
|
+
slug: 'clara-alder',
|
|
61
|
+
full_name: 'Clara Alder',
|
|
62
|
+
given_name: 'Clara',
|
|
63
|
+
surname: 'Alder',
|
|
64
|
+
alt_names: [],
|
|
65
|
+
sex: 'F',
|
|
66
|
+
is_living: true,
|
|
67
|
+
lineage_group: 'alder_core',
|
|
68
|
+
birth_edtf: '1988-09-19',
|
|
69
|
+
death_edtf: '',
|
|
70
|
+
portrait_url: '/media/should-be-stripped.jpg',
|
|
71
|
+
...overrides,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
export function createSyntheticEvent(overrides = {}) {
|
|
75
|
+
return {
|
|
76
|
+
id: 'evt_homestead_1898',
|
|
77
|
+
title: 'Homestead deed filed',
|
|
78
|
+
scope: 'family',
|
|
79
|
+
event_kind: 'point_event',
|
|
80
|
+
edtf_date: '1898-06-12',
|
|
81
|
+
place_id: null,
|
|
82
|
+
summary: 'Arthur Alder recorded a homestead claim.',
|
|
83
|
+
participants: [{ person_id: 'per_arthur_alder', role: 'principal' }],
|
|
84
|
+
sources: [],
|
|
85
|
+
...overrides,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function extractSetCookies(response) {
|
|
89
|
+
const headers = response.headers;
|
|
90
|
+
if (typeof headers.getSetCookie === 'function') {
|
|
91
|
+
return headers.getSetCookie();
|
|
92
|
+
}
|
|
93
|
+
const combined = response.headers.get('set-cookie');
|
|
94
|
+
return combined ? [combined] : [];
|
|
95
|
+
}
|
|
96
|
+
function cookiePairFromSetCookie(setCookie) {
|
|
97
|
+
return setCookie.split(';')[0]?.trim() ?? '';
|
|
98
|
+
}
|
|
99
|
+
async function seedAdminCookie(ports, email = 'admin@example.test', displayName = 'Archive Admin') {
|
|
100
|
+
await ports.repository.upsertUser({
|
|
101
|
+
email,
|
|
102
|
+
display_name: displayName,
|
|
103
|
+
role: 'admin',
|
|
104
|
+
added_by: 'bootstrap',
|
|
105
|
+
added_at: ports.clock.nowIso(),
|
|
106
|
+
last_login_at: ports.clock.nowIso(),
|
|
107
|
+
});
|
|
108
|
+
const token = await signSessionToken({
|
|
109
|
+
secret: CONTRACT_SESSION_SECRET,
|
|
110
|
+
clock: ports.clock,
|
|
111
|
+
ttlSeconds: 3600,
|
|
112
|
+
payload: { kind: 'user', email, display_name: displayName, role: 'admin' },
|
|
113
|
+
});
|
|
114
|
+
return `__Host-kinloom_session=${token}`;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Reusable Vitest backend adapter contract suite.
|
|
118
|
+
* Run identically against InMemory adapters (@kinloom/server) and Cloudflare D1 + R2 Miniflare adapters (@kinloom/worker).
|
|
119
|
+
*/
|
|
120
|
+
export function runBackendAdapterContractTests(adapterName, createHarness) {
|
|
121
|
+
describe(`Backend Adapter Contract Suite: ${adapterName}`, () => {
|
|
122
|
+
const withHarness = async (fn) => {
|
|
123
|
+
const defaultConfig = createSyntheticSiteConfig();
|
|
124
|
+
const harness = await createHarness(defaultConfig);
|
|
125
|
+
const siteConfig = harness.siteConfig ?? defaultConfig;
|
|
126
|
+
const server = createKinloomServer({
|
|
127
|
+
siteConfig,
|
|
128
|
+
ports: harness.ports,
|
|
129
|
+
sessionSecret: CONTRACT_SESSION_SECRET,
|
|
130
|
+
bootstrapAdminEmail: 'bootstrap-admin@example.test',
|
|
131
|
+
});
|
|
132
|
+
const advanceClockMs = harness.advanceClockMs ??
|
|
133
|
+
((deltaMs) => {
|
|
134
|
+
if (harness.ports.clock instanceof FixedClock) {
|
|
135
|
+
harness.ports.clock.advanceMs(deltaMs);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
const getSentMagicLinks = harness.getSentMagicLinks ??
|
|
139
|
+
(() => {
|
|
140
|
+
if (harness.ports.emailSender instanceof InMemoryEmailSender) {
|
|
141
|
+
return harness.ports.emailSender.sentMessages;
|
|
142
|
+
}
|
|
143
|
+
return [];
|
|
144
|
+
});
|
|
145
|
+
const registerOAuthCode = harness.registerOAuthCode ??
|
|
146
|
+
((code, profile) => {
|
|
147
|
+
if (harness.ports.authProvider instanceof FakeAuthProvider) {
|
|
148
|
+
harness.ports.authProvider.registerCode(code, profile);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
try {
|
|
152
|
+
await fn({
|
|
153
|
+
siteConfig,
|
|
154
|
+
ports: harness.ports,
|
|
155
|
+
server,
|
|
156
|
+
advanceClockMs,
|
|
157
|
+
getSentMagicLinks,
|
|
158
|
+
registerOAuthCode,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
finally {
|
|
162
|
+
if (harness.dispose) {
|
|
163
|
+
await harness.dispose();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
it('supports entity CRUD, soft-delete, restore, and revision history pagination', async () => {
|
|
168
|
+
await withHarness(async ({ ports, server }) => {
|
|
169
|
+
const cookie = await seedAdminCookie(ports);
|
|
170
|
+
const person = createSyntheticHistoricalPerson();
|
|
171
|
+
const createRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/entities/person`, {
|
|
172
|
+
method: 'POST',
|
|
173
|
+
headers: {
|
|
174
|
+
'content-type': 'application/json',
|
|
175
|
+
origin: CONTRACT_TEST_ORIGIN,
|
|
176
|
+
cookie,
|
|
177
|
+
},
|
|
178
|
+
body: JSON.stringify({
|
|
179
|
+
entity_id: person.id,
|
|
180
|
+
data: person,
|
|
181
|
+
summary: 'Add Arthur Alder',
|
|
182
|
+
has_primary_source: true,
|
|
183
|
+
}),
|
|
184
|
+
}));
|
|
185
|
+
expect(createRes.status).toBe(201);
|
|
186
|
+
const created = (await createRes.json());
|
|
187
|
+
expect(created.entity.full_name).toBe('Arthur Alder');
|
|
188
|
+
expect(created.revision.revision_id).toBe(1);
|
|
189
|
+
expect(created.etag).toBe('"r1"');
|
|
190
|
+
// Update entity
|
|
191
|
+
const updateRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/entities/person/${person.id}`, {
|
|
192
|
+
method: 'PUT',
|
|
193
|
+
headers: {
|
|
194
|
+
'content-type': 'application/json',
|
|
195
|
+
origin: CONTRACT_TEST_ORIGIN,
|
|
196
|
+
cookie,
|
|
197
|
+
},
|
|
198
|
+
body: JSON.stringify({
|
|
199
|
+
expected_parent_revision_id: 1,
|
|
200
|
+
data: { ...person, full_name: 'Arthur Benjamin Alder' },
|
|
201
|
+
summary: 'Add middle name from census',
|
|
202
|
+
has_primary_source: true,
|
|
203
|
+
}),
|
|
204
|
+
}));
|
|
205
|
+
expect(updateRes.status).toBe(200);
|
|
206
|
+
const updated = (await updateRes.json());
|
|
207
|
+
expect(updated.entity.full_name).toBe('Arthur Benjamin Alder');
|
|
208
|
+
expect(updated.revision.revision_id).toBe(2);
|
|
209
|
+
expect(updated.etag).toBe('"r2"');
|
|
210
|
+
// Soft delete
|
|
211
|
+
const deleteRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/entities/person/${person.id}`, {
|
|
212
|
+
method: 'DELETE',
|
|
213
|
+
headers: {
|
|
214
|
+
'content-type': 'application/json',
|
|
215
|
+
origin: CONTRACT_TEST_ORIGIN,
|
|
216
|
+
cookie,
|
|
217
|
+
},
|
|
218
|
+
body: JSON.stringify({
|
|
219
|
+
expected_parent_revision_id: 2,
|
|
220
|
+
summary: 'Soft delete duplicate entry',
|
|
221
|
+
}),
|
|
222
|
+
}));
|
|
223
|
+
expect(deleteRes.status).toBe(200);
|
|
224
|
+
const deleted = (await deleteRes.json());
|
|
225
|
+
expect(deleted.revision.action).toBe('delete');
|
|
226
|
+
expect(deleted.revision.revision_id).toBe(3);
|
|
227
|
+
// Creating over a soft-deleted entity must fail with 409
|
|
228
|
+
const duplicateCreateRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/entities/person`, {
|
|
229
|
+
method: 'POST',
|
|
230
|
+
headers: {
|
|
231
|
+
'content-type': 'application/json',
|
|
232
|
+
origin: CONTRACT_TEST_ORIGIN,
|
|
233
|
+
cookie,
|
|
234
|
+
},
|
|
235
|
+
body: JSON.stringify({
|
|
236
|
+
entity_id: person.id,
|
|
237
|
+
data: person,
|
|
238
|
+
summary: 'Attempt recreate over soft-deleted',
|
|
239
|
+
has_primary_source: true,
|
|
240
|
+
}),
|
|
241
|
+
}));
|
|
242
|
+
expect(duplicateCreateRes.status).toBe(409);
|
|
243
|
+
// Restore soft-deleted entity
|
|
244
|
+
const restoreRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/entities/person/${person.id}/restore`, {
|
|
245
|
+
method: 'POST',
|
|
246
|
+
headers: {
|
|
247
|
+
'content-type': 'application/json',
|
|
248
|
+
origin: CONTRACT_TEST_ORIGIN,
|
|
249
|
+
cookie,
|
|
250
|
+
},
|
|
251
|
+
body: JSON.stringify({
|
|
252
|
+
expected_parent_revision_id: 3,
|
|
253
|
+
summary: 'Restore verified Arthur Alder record',
|
|
254
|
+
}),
|
|
255
|
+
}));
|
|
256
|
+
expect(restoreRes.status).toBe(200);
|
|
257
|
+
const restored = (await restoreRes.json());
|
|
258
|
+
expect(restored.revision.action).toBe('restore');
|
|
259
|
+
expect(restored.revision.revision_id).toBe(4);
|
|
260
|
+
expect(restored.entity.full_name).toBe('Arthur Benjamin Alder');
|
|
261
|
+
// Revision pagination
|
|
262
|
+
const page1Res = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/revisions?entity_type=person&entity_id=${person.id}&limit=2`, { headers: { cookie } }));
|
|
263
|
+
expect(page1Res.status).toBe(200);
|
|
264
|
+
const page1 = (await page1Res.json());
|
|
265
|
+
expect(page1.items.map((r) => r.revision_id)).toEqual([4, 3]);
|
|
266
|
+
expect(page1.next_cursor).toBe(3);
|
|
267
|
+
const page2Res = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/revisions?entity_type=person&entity_id=${person.id}&cursor=${page1.next_cursor}&limit=2`, { headers: { cookie } }));
|
|
268
|
+
const page2 = (await page2Res.json());
|
|
269
|
+
expect(page2.items.map((r) => r.revision_id)).toEqual([2, 1]);
|
|
270
|
+
expect(page2.next_cursor).toBeNull();
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
it('enforces optimistic 409 Conflict with zero partial writes on multi-op atomic rollback', async () => {
|
|
274
|
+
await withHarness(async ({ ports, server }) => {
|
|
275
|
+
const cookie = await seedAdminCookie(ports);
|
|
276
|
+
const p1 = createSyntheticHistoricalPerson({ id: 'per_one', slug: 'person-one' });
|
|
277
|
+
const p2 = createSyntheticHistoricalPerson({
|
|
278
|
+
id: 'per_two',
|
|
279
|
+
slug: 'person-two',
|
|
280
|
+
full_name: 'Martha Alder',
|
|
281
|
+
given_name: 'Martha',
|
|
282
|
+
});
|
|
283
|
+
await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/entities/person`, {
|
|
284
|
+
method: 'POST',
|
|
285
|
+
headers: {
|
|
286
|
+
'content-type': 'application/json',
|
|
287
|
+
origin: CONTRACT_TEST_ORIGIN,
|
|
288
|
+
cookie,
|
|
289
|
+
},
|
|
290
|
+
body: JSON.stringify({
|
|
291
|
+
entity_id: p1.id,
|
|
292
|
+
data: p1,
|
|
293
|
+
summary: 'Create person one',
|
|
294
|
+
has_primary_source: true,
|
|
295
|
+
}),
|
|
296
|
+
}));
|
|
297
|
+
await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/entities/person`, {
|
|
298
|
+
method: 'POST',
|
|
299
|
+
headers: {
|
|
300
|
+
'content-type': 'application/json',
|
|
301
|
+
origin: CONTRACT_TEST_ORIGIN,
|
|
302
|
+
cookie,
|
|
303
|
+
},
|
|
304
|
+
body: JSON.stringify({
|
|
305
|
+
entity_id: p2.id,
|
|
306
|
+
data: p2,
|
|
307
|
+
summary: 'Create person two',
|
|
308
|
+
has_primary_source: true,
|
|
309
|
+
}),
|
|
310
|
+
}));
|
|
311
|
+
// Advance p2 to rev 3
|
|
312
|
+
await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/entities/person/${p2.id}`, {
|
|
313
|
+
method: 'PUT',
|
|
314
|
+
headers: {
|
|
315
|
+
'content-type': 'application/json',
|
|
316
|
+
origin: CONTRACT_TEST_ORIGIN,
|
|
317
|
+
cookie,
|
|
318
|
+
},
|
|
319
|
+
body: JSON.stringify({
|
|
320
|
+
expected_parent_revision_id: 2,
|
|
321
|
+
data: { ...p2, full_name: 'Martha Ellen Alder' },
|
|
322
|
+
summary: 'Update Martha middle name',
|
|
323
|
+
has_primary_source: true,
|
|
324
|
+
}),
|
|
325
|
+
}));
|
|
326
|
+
// Direct atomic commit with op1 valid (p1 at rev 1) and op2 stale (p2 expecting rev 2 when server is rev 3)
|
|
327
|
+
const conflictResult = await ports.repository.commitAtomicMutations({
|
|
328
|
+
batch_id: asBatchId('batch_rollback_test'),
|
|
329
|
+
author: { author_id: 'admin@example.test', author_name: 'Admin', author_type: 'human' },
|
|
330
|
+
nowIso: ports.clock.nowIso(),
|
|
331
|
+
operations: [
|
|
332
|
+
{
|
|
333
|
+
entity_type: 'person',
|
|
334
|
+
entity_id: p1.id,
|
|
335
|
+
action: 'update',
|
|
336
|
+
expected_parent_revision_id: asRevisionId(1),
|
|
337
|
+
summary: 'Update p1 in doomed batch',
|
|
338
|
+
has_primary_source: true,
|
|
339
|
+
before_json: p1,
|
|
340
|
+
after_json: { ...p1, full_name: 'Should Rollback' },
|
|
341
|
+
},
|
|
342
|
+
{
|
|
343
|
+
entity_type: 'person',
|
|
344
|
+
entity_id: p2.id,
|
|
345
|
+
action: 'update',
|
|
346
|
+
expected_parent_revision_id: asRevisionId(2),
|
|
347
|
+
summary: 'Stale p2 update triggers rollback',
|
|
348
|
+
has_primary_source: true,
|
|
349
|
+
before_json: p2,
|
|
350
|
+
after_json: { ...p2, full_name: 'Stale Write' },
|
|
351
|
+
},
|
|
352
|
+
],
|
|
353
|
+
});
|
|
354
|
+
expect(conflictResult.kind).toBe('conflict');
|
|
355
|
+
if (conflictResult.kind === 'conflict') {
|
|
356
|
+
expect(conflictResult.entity_id).toBe(p2.id);
|
|
357
|
+
expect(conflictResult.server_revision_id).toBe(3);
|
|
358
|
+
expect(conflictResult.diff.hasChanges).toBe(true);
|
|
359
|
+
}
|
|
360
|
+
// Verify ZERO partial writes: p1 stayed at rev 1 and head revision stayed at 3
|
|
361
|
+
expect(await ports.repository.getHeadRevisionId()).toBe(3);
|
|
362
|
+
const storedP1 = await ports.repository.getEntity('person', p1.id);
|
|
363
|
+
expect(storedP1?.current_revision_id).toBe(1);
|
|
364
|
+
expect(storedP1 && !('edge' in storedP1) ? storedP1.data.full_name : null).toBe('Arthur Alder');
|
|
365
|
+
});
|
|
366
|
+
});
|
|
367
|
+
it('submits multi-step batches and unwinds batch revert strictly in reverse revision order', async () => {
|
|
368
|
+
await withHarness(async ({ ports, server }) => {
|
|
369
|
+
const cookie = await seedAdminCookie(ports);
|
|
370
|
+
const person = createSyntheticHistoricalPerson({ id: 'per_batch_01', slug: 'batch-person' });
|
|
371
|
+
// Batch performs CREATE followed by UPDATE on the SAME person
|
|
372
|
+
const submitRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/batches`, {
|
|
373
|
+
method: 'POST',
|
|
374
|
+
headers: {
|
|
375
|
+
'content-type': 'application/json',
|
|
376
|
+
origin: CONTRACT_TEST_ORIGIN,
|
|
377
|
+
cookie,
|
|
378
|
+
},
|
|
379
|
+
body: JSON.stringify({
|
|
380
|
+
batch_id: 'batch_census_1900',
|
|
381
|
+
summary: 'Import 1900 census findings',
|
|
382
|
+
has_primary_source: true,
|
|
383
|
+
patches: [
|
|
384
|
+
{
|
|
385
|
+
action: 'create',
|
|
386
|
+
entity_type: 'person',
|
|
387
|
+
entity_id: person.id,
|
|
388
|
+
data: person,
|
|
389
|
+
summary: 'Create Arthur from index',
|
|
390
|
+
},
|
|
391
|
+
{
|
|
392
|
+
action: 'update',
|
|
393
|
+
entity_type: 'person',
|
|
394
|
+
entity_id: person.id,
|
|
395
|
+
data: { full_name: 'Arthur J. Alder' },
|
|
396
|
+
merge: true,
|
|
397
|
+
summary: 'Add middle initial from sheet 4B',
|
|
398
|
+
},
|
|
399
|
+
],
|
|
400
|
+
}),
|
|
401
|
+
}));
|
|
402
|
+
expect(submitRes.status).toBe(201);
|
|
403
|
+
const submitted = (await submitRes.json());
|
|
404
|
+
expect(submitted.revisions.map((r) => r.revision_id)).toEqual([1, 2]);
|
|
405
|
+
expect(submitted.head_revision_id).toBe(2);
|
|
406
|
+
// Revert entire batch: must unwind r2 then r1 so person converges to softDeleted (not resurrected!)
|
|
407
|
+
const revertRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/batches/batch_census_1900/revert`, {
|
|
408
|
+
method: 'POST',
|
|
409
|
+
headers: {
|
|
410
|
+
'content-type': 'application/json',
|
|
411
|
+
origin: CONTRACT_TEST_ORIGIN,
|
|
412
|
+
cookie,
|
|
413
|
+
},
|
|
414
|
+
body: JSON.stringify({ summary: 'Undo unverified census batch' }),
|
|
415
|
+
}));
|
|
416
|
+
expect(revertRes.status).toBe(201);
|
|
417
|
+
const reverted = (await revertRes.json());
|
|
418
|
+
expect(reverted.revisions.map((r) => r.reverted_revision_id)).toEqual([2, 1]);
|
|
419
|
+
expect(reverted.head_revision_id).toBe(4);
|
|
420
|
+
const rowAfterRevert = await ports.repository.getEntity('person', person.id);
|
|
421
|
+
expect(rowAfterRevert?.deleted_at).not.toBeNull();
|
|
422
|
+
expect(rowAfterRevert?.current_revision_id).toBe(4);
|
|
423
|
+
});
|
|
424
|
+
});
|
|
425
|
+
it('enforces living-person privacy on writes, reads, exports, and media attach', async () => {
|
|
426
|
+
await withHarness(async ({ ports, server }) => {
|
|
427
|
+
const cookie = await seedAdminCookie(ports);
|
|
428
|
+
const living = createSyntheticLivingPerson();
|
|
429
|
+
const createLivingRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/entities/person`, {
|
|
430
|
+
method: 'POST',
|
|
431
|
+
headers: {
|
|
432
|
+
'content-type': 'application/json',
|
|
433
|
+
origin: CONTRACT_TEST_ORIGIN,
|
|
434
|
+
cookie,
|
|
435
|
+
},
|
|
436
|
+
body: JSON.stringify({
|
|
437
|
+
entity_id: living.id,
|
|
438
|
+
data: living,
|
|
439
|
+
summary: 'Add living relative Clara',
|
|
440
|
+
has_primary_source: false,
|
|
441
|
+
}),
|
|
442
|
+
}));
|
|
443
|
+
expect(createLivingRes.status).toBe(201);
|
|
444
|
+
const createdLiving = (await createLivingRes.json());
|
|
445
|
+
// Exact birthdate stripped to year only, portrait stripped to null
|
|
446
|
+
expect(createdLiving.entity.birth_edtf).toBe('1988');
|
|
447
|
+
expect(createdLiving.entity.portrait_url).toBeNull();
|
|
448
|
+
// Living-participant event redacts exact date, place_id, and summary
|
|
449
|
+
const livingEvent = createSyntheticEvent({
|
|
450
|
+
id: 'evt_living_01',
|
|
451
|
+
edtf_date: '2015-08-22',
|
|
452
|
+
place_id: 'plc_springfield',
|
|
453
|
+
summary: 'Private home address 123 Maple St.',
|
|
454
|
+
participants: [{ person_id: living.id, role: 'principal' }],
|
|
455
|
+
});
|
|
456
|
+
const eventRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/entities/event`, {
|
|
457
|
+
method: 'POST',
|
|
458
|
+
headers: {
|
|
459
|
+
'content-type': 'application/json',
|
|
460
|
+
origin: CONTRACT_TEST_ORIGIN,
|
|
461
|
+
cookie,
|
|
462
|
+
},
|
|
463
|
+
body: JSON.stringify({
|
|
464
|
+
entity_id: livingEvent.id,
|
|
465
|
+
data: livingEvent,
|
|
466
|
+
summary: 'Add living event',
|
|
467
|
+
has_primary_source: false,
|
|
468
|
+
}),
|
|
469
|
+
}));
|
|
470
|
+
expect(eventRes.status).toBe(201);
|
|
471
|
+
const createdEvent = (await eventRes.json());
|
|
472
|
+
expect(createdEvent.entity.edtf_date).toBe('2015');
|
|
473
|
+
expect(createdEvent.entity.place_id).toBeNull();
|
|
474
|
+
expect(createdEvent.entity.summary).not.toContain('123 Maple');
|
|
475
|
+
// Uploading a portrait linked to living Clara must return 422 LIVING_MEDIA_FORBIDDEN
|
|
476
|
+
const form = new FormData();
|
|
477
|
+
form.append('file', new File([new Uint8Array([0xff, 0xd8, 0xff, 0xe0])], 'clara.jpg', { type: 'image/jpeg' }));
|
|
478
|
+
form.append('metadata', JSON.stringify({
|
|
479
|
+
kind: 'portrait',
|
|
480
|
+
person_ids: [living.id],
|
|
481
|
+
caption: 'Portrait of Clara',
|
|
482
|
+
}));
|
|
483
|
+
form.append('summary', 'Attempt portrait upload for living person');
|
|
484
|
+
const uploadRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/media/upload`, {
|
|
485
|
+
method: 'POST',
|
|
486
|
+
headers: { origin: CONTRACT_TEST_ORIGIN, cookie },
|
|
487
|
+
body: form,
|
|
488
|
+
}));
|
|
489
|
+
expect(uploadRes.status).toBe(422);
|
|
490
|
+
const errBody = (await uploadRes.json());
|
|
491
|
+
expect(errBody.error.code).toBe('LIVING_MEDIA_FORBIDDEN');
|
|
492
|
+
});
|
|
493
|
+
});
|
|
494
|
+
it('deduplicates media by SHA-256 and streams HTTP 206 Partial Content byte ranges', async () => {
|
|
495
|
+
await withHarness(async ({ ports, server }) => {
|
|
496
|
+
const cookie = await seedAdminCookie(ports);
|
|
497
|
+
const person = createSyntheticHistoricalPerson();
|
|
498
|
+
await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/entities/person`, {
|
|
499
|
+
method: 'POST',
|
|
500
|
+
headers: {
|
|
501
|
+
'content-type': 'application/json',
|
|
502
|
+
origin: CONTRACT_TEST_ORIGIN,
|
|
503
|
+
cookie,
|
|
504
|
+
},
|
|
505
|
+
body: JSON.stringify({
|
|
506
|
+
entity_id: person.id,
|
|
507
|
+
data: person,
|
|
508
|
+
summary: 'Add Arthur for media test',
|
|
509
|
+
has_primary_source: true,
|
|
510
|
+
}),
|
|
511
|
+
}));
|
|
512
|
+
const videoBytes = new TextEncoder().encode('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ');
|
|
513
|
+
const uploadOnce = async (id) => {
|
|
514
|
+
const form = new FormData();
|
|
515
|
+
form.append('file', new File([videoBytes], 'story.mp4', { type: 'video/mp4' }));
|
|
516
|
+
form.append('metadata', JSON.stringify({
|
|
517
|
+
id,
|
|
518
|
+
kind: 'video_story',
|
|
519
|
+
person_ids: [person.id],
|
|
520
|
+
caption: 'Arthur oral history clip',
|
|
521
|
+
duration_seconds: 42,
|
|
522
|
+
}));
|
|
523
|
+
form.append('summary', 'Upload video story clip');
|
|
524
|
+
const res = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/media/upload`, {
|
|
525
|
+
method: 'POST',
|
|
526
|
+
headers: { origin: CONTRACT_TEST_ORIGIN, cookie },
|
|
527
|
+
body: form,
|
|
528
|
+
}));
|
|
529
|
+
expect(res.status).toBe(201);
|
|
530
|
+
return (await res.json());
|
|
531
|
+
};
|
|
532
|
+
const first = await uploadOnce('med_clip_1');
|
|
533
|
+
const second = await uploadOnce('med_clip_2');
|
|
534
|
+
expect(first.url).toBe(second.url);
|
|
535
|
+
// Full GET 200
|
|
536
|
+
const fullRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}${first.url}`, { headers: { cookie } }));
|
|
537
|
+
expect(fullRes.status).toBe(200);
|
|
538
|
+
expect(fullRes.headers.get('accept-ranges')).toBe('bytes');
|
|
539
|
+
expect(fullRes.headers.get('cache-control')).toBe('private, max-age=31536000, immutable');
|
|
540
|
+
expect(await fullRes.text()).toBe('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ');
|
|
541
|
+
// Range GET 206 Partial Content (explicit start-end, open-ended, and suffix ranges)
|
|
542
|
+
const rangeRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}${first.url}`, {
|
|
543
|
+
headers: { cookie, range: 'bytes=10-19' },
|
|
544
|
+
}));
|
|
545
|
+
expect(rangeRes.status).toBe(206);
|
|
546
|
+
expect(rangeRes.headers.get('content-range')).toBe(`bytes 10-19/${videoBytes.byteLength}`);
|
|
547
|
+
expect(rangeRes.headers.get('content-length')).toBe('10');
|
|
548
|
+
expect(await rangeRes.text()).toBe('ABCDEFGHIJ');
|
|
549
|
+
const openEndedRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}${first.url}`, {
|
|
550
|
+
headers: { cookie, range: 'bytes=30-' },
|
|
551
|
+
}));
|
|
552
|
+
expect(openEndedRes.status).toBe(206);
|
|
553
|
+
expect(openEndedRes.headers.get('content-range')).toBe(`bytes 30-35/${videoBytes.byteLength}`);
|
|
554
|
+
expect(await openEndedRes.text()).toBe('UVWXYZ');
|
|
555
|
+
const suffixRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}${first.url}`, {
|
|
556
|
+
headers: { cookie, range: 'bytes=-6' },
|
|
557
|
+
}));
|
|
558
|
+
expect(suffixRes.status).toBe(206);
|
|
559
|
+
expect(suffixRes.headers.get('content-range')).toBe(`bytes 30-35/${videoBytes.byteLength}`);
|
|
560
|
+
expect(await suffixRes.text()).toBe('UVWXYZ');
|
|
561
|
+
// Unsatisfiable range -> 416
|
|
562
|
+
const badRangeRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}${first.url}`, {
|
|
563
|
+
headers: { cookie, range: 'bytes=999-1005' },
|
|
564
|
+
}));
|
|
565
|
+
expect(badRangeRes.status).toBe(416);
|
|
566
|
+
});
|
|
567
|
+
});
|
|
568
|
+
it('auto-enrolls invited relatives strictly as editor and enforces single-use magic links', async () => {
|
|
569
|
+
await withHarness(async ({ ports, server, advanceClockMs, getSentMagicLinks, registerOAuthCode }) => {
|
|
570
|
+
const adminCookie = await seedAdminCookie(ports);
|
|
571
|
+
// Admin creates an invite link
|
|
572
|
+
const inviteCreateRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/admin/invites`, {
|
|
573
|
+
method: 'POST',
|
|
574
|
+
headers: {
|
|
575
|
+
'content-type': 'application/json',
|
|
576
|
+
origin: CONTRACT_TEST_ORIGIN,
|
|
577
|
+
cookie: adminCookie,
|
|
578
|
+
},
|
|
579
|
+
body: JSON.stringify({ label: 'Reunion invite', expires_in_days: 7 }),
|
|
580
|
+
}));
|
|
581
|
+
expect(inviteCreateRes.status).toBe(201);
|
|
582
|
+
const createdInvite = (await inviteCreateRes.json());
|
|
583
|
+
// Relative redeems invite -> gets viewer cookie
|
|
584
|
+
const redeemRes = await server.fetch(new Request(createdInvite.invite_url));
|
|
585
|
+
expect(redeemRes.status).toBe(302);
|
|
586
|
+
const viewerCookie = cookiePairFromSetCookie(extractSetCookies(redeemRes)[0] ?? '');
|
|
587
|
+
expect(viewerCookie).toContain('__Host-kinloom_session=');
|
|
588
|
+
// Viewer can read bundle, cannot edit
|
|
589
|
+
const bundleRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/bundle`, { headers: { cookie: viewerCookie } }));
|
|
590
|
+
expect(bundleRes.status).toBe(200);
|
|
591
|
+
const etag = bundleRes.headers.get('etag');
|
|
592
|
+
expect(etag).toBe('"r0"');
|
|
593
|
+
// Repeat GET /api/bundle with If-None-Match -> 304 Not Modified
|
|
594
|
+
const notModifiedRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/bundle`, {
|
|
595
|
+
headers: { cookie: viewerCookie, 'if-none-match': etag ?? '' },
|
|
596
|
+
}));
|
|
597
|
+
expect(notModifiedRes.status).toBe(304);
|
|
598
|
+
// Upgrade via Google OAuth PKCE while carrying viewer invite cookie
|
|
599
|
+
const startRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/auth/google/start?next=/people`, {
|
|
600
|
+
headers: { cookie: viewerCookie },
|
|
601
|
+
}));
|
|
602
|
+
expect(startRes.status).toBe(302);
|
|
603
|
+
const oauthCookie = cookiePairFromSetCookie(extractSetCookies(startRes)[0] ?? '');
|
|
604
|
+
const authUrl = new URL(startRes.headers.get('location') ?? '');
|
|
605
|
+
const state = authUrl.searchParams.get('state') ?? '';
|
|
606
|
+
registerOAuthCode('code_cousin_sam', {
|
|
607
|
+
email: 'sam.alder@example.test',
|
|
608
|
+
displayName: 'Sam Alder',
|
|
609
|
+
emailVerified: true,
|
|
610
|
+
});
|
|
611
|
+
const cbRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/auth/google/callback?code=code_cousin_sam&state=${encodeURIComponent(state)}`, { headers: { cookie: oauthCookie } }));
|
|
612
|
+
expect(cbRes.status).toBe(302);
|
|
613
|
+
expect(cbRes.headers.get('location')).toBe('/people');
|
|
614
|
+
const samCookie = extractSetCookies(cbRes)
|
|
615
|
+
.map(cookiePairFromSetCookie)
|
|
616
|
+
.find((c) => c.startsWith('__Host-kinloom_session=') && c !== '__Host-kinloom_session=') ??
|
|
617
|
+
'';
|
|
618
|
+
// Verify Sam enrolled strictly as editor (never admin!)
|
|
619
|
+
const meRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/auth/me`, { headers: { cookie: samCookie } }));
|
|
620
|
+
const me = (await meRes.json());
|
|
621
|
+
expect(me.session.email).toBe('sam.alder@example.test');
|
|
622
|
+
expect(me.session.role).toBe('editor');
|
|
623
|
+
expect(me.capabilities.canEdit).toBe(true);
|
|
624
|
+
expect(me.capabilities.canManageSite).toBe(false);
|
|
625
|
+
// Magic-link request with invite cookie -> single-use verify -> second use rejected
|
|
626
|
+
const magicReqRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/auth/magic-link/request`, {
|
|
627
|
+
method: 'POST',
|
|
628
|
+
headers: {
|
|
629
|
+
'content-type': 'application/json',
|
|
630
|
+
origin: CONTRACT_TEST_ORIGIN,
|
|
631
|
+
cookie: viewerCookie,
|
|
632
|
+
},
|
|
633
|
+
body: JSON.stringify({ email: 'nora.alder@example.test' }),
|
|
634
|
+
}));
|
|
635
|
+
expect(magicReqRes.status).toBe(202);
|
|
636
|
+
const sent = getSentMagicLinks();
|
|
637
|
+
const noraEmail = sent.find((m) => m.toEmail === 'nora.alder@example.test');
|
|
638
|
+
expect(noraEmail).toBeDefined();
|
|
639
|
+
const verify1Res = await server.fetch(new Request(noraEmail.magicLinkUrl));
|
|
640
|
+
expect(verify1Res.status).toBe(302);
|
|
641
|
+
const noraUser = await ports.repository.getUser('nora.alder@example.test');
|
|
642
|
+
expect(noraUser?.role).toBe('editor');
|
|
643
|
+
// Second use of the exact same magic link must fail with 400 TOKEN_INVALID_OR_EXPIRED
|
|
644
|
+
const verify2Res = await server.fetch(new Request(noraEmail.magicLinkUrl));
|
|
645
|
+
expect(verify2Res.status).toBe(400);
|
|
646
|
+
const verify2Body = (await verify2Res.json());
|
|
647
|
+
expect(verify2Body.error.code).toBe('TOKEN_INVALID_OR_EXPIRED');
|
|
648
|
+
// Expired magic link fails when clock advances past TTL
|
|
649
|
+
await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/auth/magic-link/request`, {
|
|
650
|
+
method: 'POST',
|
|
651
|
+
headers: {
|
|
652
|
+
'content-type': 'application/json',
|
|
653
|
+
origin: CONTRACT_TEST_ORIGIN,
|
|
654
|
+
cookie: viewerCookie,
|
|
655
|
+
},
|
|
656
|
+
body: JSON.stringify({ email: 'eli.alder@example.test' }),
|
|
657
|
+
}));
|
|
658
|
+
const eliEmail = getSentMagicLinks().find((m) => m.toEmail === 'eli.alder@example.test');
|
|
659
|
+
expect(eliEmail).toBeDefined();
|
|
660
|
+
advanceClockMs(16 * 60 * 1000);
|
|
661
|
+
const expiredRes = await server.fetch(new Request(eliEmail.magicLinkUrl));
|
|
662
|
+
expect(expiredRes.status).toBe(400);
|
|
663
|
+
});
|
|
664
|
+
});
|
|
665
|
+
it('enforces scoped agent tokens (write_batch vs read_backup)', async () => {
|
|
666
|
+
await withHarness(async ({ ports, server }) => {
|
|
667
|
+
const adminCookie = await seedAdminCookie(ports);
|
|
668
|
+
// Create write_batch and read_backup tokens
|
|
669
|
+
const writeTokRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/admin/agent-tokens`, {
|
|
670
|
+
method: 'POST',
|
|
671
|
+
headers: {
|
|
672
|
+
'content-type': 'application/json',
|
|
673
|
+
origin: CONTRACT_TEST_ORIGIN,
|
|
674
|
+
cookie: adminCookie,
|
|
675
|
+
},
|
|
676
|
+
body: JSON.stringify({ label: 'Research Agent', scope: 'write_batch' }),
|
|
677
|
+
}));
|
|
678
|
+
expect(writeTokRes.status).toBe(201);
|
|
679
|
+
const writeTok = (await writeTokRes.json());
|
|
680
|
+
const backupTokRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/admin/agent-tokens`, {
|
|
681
|
+
method: 'POST',
|
|
682
|
+
headers: {
|
|
683
|
+
'content-type': 'application/json',
|
|
684
|
+
origin: CONTRACT_TEST_ORIGIN,
|
|
685
|
+
cookie: adminCookie,
|
|
686
|
+
},
|
|
687
|
+
body: JSON.stringify({ label: 'GitHub Nightly Backup', scope: 'read_backup' }),
|
|
688
|
+
}));
|
|
689
|
+
expect(backupTokRes.status).toBe(201);
|
|
690
|
+
const backupTok = (await backupTokRes.json());
|
|
691
|
+
// write_batch submits batch cleanly
|
|
692
|
+
const person = createSyntheticHistoricalPerson({ id: 'per_agent_01', slug: 'agent-person' });
|
|
693
|
+
const batchRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/batches`, {
|
|
694
|
+
method: 'POST',
|
|
695
|
+
headers: {
|
|
696
|
+
'content-type': 'application/json',
|
|
697
|
+
authorization: `Bearer ${writeTok.plaintext_secret}`,
|
|
698
|
+
},
|
|
699
|
+
body: JSON.stringify({
|
|
700
|
+
batch_id: 'batch_agent_01',
|
|
701
|
+
summary: 'Agent cited insertion',
|
|
702
|
+
has_primary_source: true,
|
|
703
|
+
patches: [
|
|
704
|
+
{ action: 'create', entity_type: 'person', entity_id: person.id, data: person },
|
|
705
|
+
],
|
|
706
|
+
}),
|
|
707
|
+
}));
|
|
708
|
+
expect(batchRes.status).toBe(201);
|
|
709
|
+
// write_batch is blocked from /api/admin/* and /api/export/yaml
|
|
710
|
+
const writeAdminRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/admin/users`, {
|
|
711
|
+
headers: { authorization: `Bearer ${writeTok.plaintext_secret}` },
|
|
712
|
+
}));
|
|
713
|
+
expect(writeAdminRes.status).toBe(403);
|
|
714
|
+
const writeExportRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/export/yaml`, {
|
|
715
|
+
headers: { authorization: `Bearer ${writeTok.plaintext_secret}` },
|
|
716
|
+
}));
|
|
717
|
+
expect(writeExportRes.status).toBe(403);
|
|
718
|
+
// read_backup can export YAML and snapshot and read bundle, but gets 403 on ALL write routes
|
|
719
|
+
const yamlRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/export/yaml`, {
|
|
720
|
+
headers: { authorization: `Bearer ${backupTok.plaintext_secret}` },
|
|
721
|
+
}));
|
|
722
|
+
expect(yamlRes.status).toBe(200);
|
|
723
|
+
expect(yamlRes.headers.get('x-kinloom-head-revision')).toBe('1');
|
|
724
|
+
const yamlBody = (await yamlRes.json());
|
|
725
|
+
expect(yamlBody.files['people.yaml']).toContain('per_agent_01');
|
|
726
|
+
const backupBundleRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/bundle`, {
|
|
727
|
+
headers: { authorization: `Bearer ${backupTok.plaintext_secret}` },
|
|
728
|
+
}));
|
|
729
|
+
expect(backupBundleRes.status).toBe(200);
|
|
730
|
+
const bundleJson = (await backupBundleRes.json());
|
|
731
|
+
expect(bundleJson.people.map((p) => p.id)).toContain('per_agent_01');
|
|
732
|
+
const backupWriteBatchRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/batches`, {
|
|
733
|
+
method: 'POST',
|
|
734
|
+
headers: {
|
|
735
|
+
'content-type': 'application/json',
|
|
736
|
+
authorization: `Bearer ${backupTok.plaintext_secret}`,
|
|
737
|
+
},
|
|
738
|
+
body: JSON.stringify({
|
|
739
|
+
batch_id: 'batch_forbidden',
|
|
740
|
+
summary: 'Backup token must not write',
|
|
741
|
+
has_primary_source: true,
|
|
742
|
+
patches: [
|
|
743
|
+
{ action: 'create', entity_type: 'person', entity_id: 'per_nope', data: person },
|
|
744
|
+
],
|
|
745
|
+
}),
|
|
746
|
+
}));
|
|
747
|
+
expect(backupWriteBatchRes.status).toBe(403);
|
|
748
|
+
const backupEntityWriteRes = await server.fetch(new Request(`${CONTRACT_TEST_ORIGIN}/api/entities/person`, {
|
|
749
|
+
method: 'POST',
|
|
750
|
+
headers: {
|
|
751
|
+
'content-type': 'application/json',
|
|
752
|
+
authorization: `Bearer ${backupTok.plaintext_secret}`,
|
|
753
|
+
},
|
|
754
|
+
body: JSON.stringify({
|
|
755
|
+
data: person,
|
|
756
|
+
summary: 'Backup token must not create entity',
|
|
757
|
+
has_primary_source: true,
|
|
758
|
+
}),
|
|
759
|
+
}));
|
|
760
|
+
expect(backupEntityWriteRes.status).toBe(403);
|
|
761
|
+
});
|
|
762
|
+
});
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
//# sourceMappingURL=adapterContractSuite.js.map
|