| [780a7c6] | 1 | // The guardianship module (FEP-633c) — the multi-party handshake (§3).
|
|---|
| 2 | // Everyone lives on one in-memory instance here, so the handshake copies all
|
|---|
| 3 | // converge locally; that also exercises the "multiple local parties" routing.
|
|---|
| [e61c289] | 4 | import { test } from 'node:test';
|
|---|
| 5 | import assert from 'node:assert/strict';
|
|---|
| 6 |
|
|---|
| 7 | process.env.DATABASE_PATH = ':memory:';
|
|---|
| 8 | process.env.PUBLIC_BASE_URL = 'https://test.example';
|
|---|
| 9 |
|
|---|
| 10 | const dbMod = await import('../src/config/database.js');
|
|---|
| 11 | const db = dbMod.default;
|
|---|
| 12 | dbMod.initializeDatabase();
|
|---|
| 13 | const AP = (await import('../src/services/ActivityPubService.js')).default;
|
|---|
| 14 | const G = await import('../src/services/guardianship/index.js');
|
|---|
| 15 |
|
|---|
| [780a7c6] | 16 | function site(id, slug) {
|
|---|
| 17 | db.prepare('INSERT INTO sites (id, slug, title, owner_id, is_primary) VALUES (?,?,?,?,?)').run(id, slug, slug, 'u1', id === 's1' ? 1 : 0);
|
|---|
| 18 | return db.prepare('SELECT * FROM sites WHERE id = ?').get(id);
|
|---|
| 19 | }
|
|---|
| [e61c289] | 20 | db.prepare('INSERT INTO users (id, username, email, password_hash, role) VALUES (?,?,?,?,?)').run('u1', 'u1', 'u1@test', 'x', 'god');
|
|---|
| [780a7c6] | 21 | const parent = site('s1', 'parent'); // first guardian-candidate
|
|---|
| 22 | const kid = site('s2', 'kid'); // ward
|
|---|
| 23 | const gran = site('s3', 'gran'); // second guardian-candidate (co-approver later)
|
|---|
| 24 | const A = (slug) => `https://test.example/ap/users/${slug}`;
|
|---|
| 25 | const [ME, KID, GRAN] = [A('parent'), A('kid'), A('gran')];
|
|---|
| 26 |
|
|---|
| 27 | // No network: the handshake delivers by feeding each activity straight into the
|
|---|
| 28 | // inbound handler of every addressed local party (what real S2S would do).
|
|---|
| [e61c289] | 29 | G.wireHandshake({
|
|---|
| [780a7c6] | 30 | selfId: A,
|
|---|
| 31 | localSlug: (uri) => (uri.startsWith('https://test.example/ap/users/') ? uri.split('/').pop() : null),
|
|---|
| 32 | deriveHandle: (uri) => '@' + uri.split('/').pop() + '@test.example',
|
|---|
| 33 | fetchActor: async () => null,
|
|---|
| 34 | deliverTo: async (fromSite, toUri, activity) => {
|
|---|
| 35 | const slug = toUri.split('/').pop();
|
|---|
| 36 | const s = db.prepare('SELECT * FROM sites WHERE slug = ?').get(slug);
|
|---|
| 37 | if (s) await G.handleGuardianshipInbox(s, activity);
|
|---|
| 38 | return { delivered: true };
|
|---|
| 39 | },
|
|---|
| [e61c289] | 40 | onEvent: null,
|
|---|
| 41 | });
|
|---|
| 42 |
|
|---|
| [780a7c6] | 43 | const offerIdFrom = (r) => r.id;
|
|---|
| [e61c289] | 44 |
|
|---|
| [3ffbedd] | 45 | test('first guardian: a free ward commits on its own single accept', async () => {
|
|---|
| [780a7c6] | 46 | const off = await G.handleGuardianshipOutbox(parent, {
|
|---|
| 47 | type: 'Offer', object: { type: 'Relationship', subject: KID, relationship: 'shaer:Guardian', object: ME },
|
|---|
| [e61c289] | 48 | });
|
|---|
| [780a7c6] | 49 | assert.equal(off.status, 202);
|
|---|
| 50 | const id = offerIdFrom(off);
|
|---|
| [e61c289] | 51 |
|
|---|
| [3ffbedd] | 52 | // The kid sees the offer needing its accept; the candidate already agreed
|
|---|
| 53 | // (the Offer is the candidate's accept), so it just waits.
|
|---|
| [780a7c6] | 54 | const kidQ = G.offersCollection(`${KID}/queues/offers`, 'kid', KID).orderedItems;
|
|---|
| 55 | assert.equal(kidQ.length, 1);
|
|---|
| 56 | assert.equal(kidQ[0]['shaer:needsMyAccept'], true);
|
|---|
| [3ffbedd] | 57 | assert.deepEqual(kidQ[0]['shaer:acceptedBy'], [ME]); // candidate accepted via the offer
|
|---|
| 58 | const parentQ0 = G.offersCollection(`${ME}/queues/offers`, 'parent', ME).orderedItems;
|
|---|
| 59 | assert.equal(parentQ0[0]['shaer:needsMyAccept'], false); // candidate already agreed
|
|---|
| [780a7c6] | 60 |
|
|---|
| [3ffbedd] | 61 | // Not committed until the ward agrees.
|
|---|
| [780a7c6] | 62 | assert.deepEqual(G.listGuardians('kid'), []);
|
|---|
| 63 |
|
|---|
| [3ffbedd] | 64 | // The kid accepts → free ward, no existing guardian to co-approve → commit.
|
|---|
| 65 | const done = await G.handleGuardianshipOutbox(kid, { type: 'Accept', object: id });
|
|---|
| [780a7c6] | 66 | assert.equal(done.committed, true);
|
|---|
| 67 | assert.deepEqual(G.listGuardians('kid').map((g) => g.other_uri), [ME]);
|
|---|
| 68 | assert.deepEqual(G.listWards('parent').map((w) => w.other_uri), [KID]);
|
|---|
| [fcd6964] | 69 | // other_handle is the display @handle (not the escalation inbox handle).
|
|---|
| 70 | assert.equal(G.listGuardians('kid')[0].other_handle, '@parent@test.example');
|
|---|
| 71 | assert.equal(G.listWards('parent')[0].other_handle, '@kid@test.example');
|
|---|
| [780a7c6] | 72 |
|
|---|
| 73 | // The ward actor now names its guardian; parent reads as guardian (§2).
|
|---|
| 74 | assert.deepEqual(AP.buildActor('https://test.example', kid)['shaer:guardians'], [ME]);
|
|---|
| 75 | assert.equal(AP.buildActor('https://test.example', parent)['shaer:isGuardian'], true);
|
|---|
| 76 | // §1 mutual exclusion: the ward is not also a guardian.
|
|---|
| 77 | assert.equal(AP.buildActor('https://test.example', kid)['shaer:isGuardian'], undefined);
|
|---|
| [e61c289] | 78 | });
|
|---|
| 79 |
|
|---|
| [780a7c6] | 80 | test('second guardian needs the EXISTING guardian to co-accept (§3.1.2)', async () => {
|
|---|
| 81 | // Gran offers to also guard the kid (who already has parent).
|
|---|
| 82 | const off = await G.handleGuardianshipOutbox(gran, {
|
|---|
| 83 | type: 'Offer', object: { type: 'Relationship', subject: KID, relationship: 'shaer:Guardian', object: GRAN },
|
|---|
| [e61c289] | 84 | });
|
|---|
| [780a7c6] | 85 | const id = offerIdFrom(off);
|
|---|
| 86 | // The existing guardian (parent) is a party and must accept.
|
|---|
| 87 | const parentQ = G.offersCollection(`${ME}/queues/offers`, 'parent', ME).orderedItems.find((o) => o.id === id);
|
|---|
| 88 | assert.ok(parentQ, 'parent sees the co-guardianship offer');
|
|---|
| 89 | assert.deepEqual(parentQ['shaer:existingGuardians'], [ME]);
|
|---|
| [e61c289] | 90 |
|
|---|
| [3ffbedd] | 91 | // The kid accepts, but now it IS a ward: NOT committed, because the existing
|
|---|
| 92 | // guardian (parent) has not co-accepted (§3.1.2). The candidate (gran) already
|
|---|
| 93 | // agreed via the offer, so no separate gran accept is needed.
|
|---|
| 94 | const early = await G.handleGuardianshipOutbox(kid, { type: 'Accept', object: id });
|
|---|
| [780a7c6] | 95 | assert.equal(early.committed, false);
|
|---|
| 96 | assert.equal(G.listGuardians('kid').length, 1, 'still just the first guardian');
|
|---|
| [e61c289] | 97 |
|
|---|
| [780a7c6] | 98 | // The existing guardian co-accepts → tally complete → commit.
|
|---|
| 99 | await G.handleGuardianshipOutbox(parent, { type: 'Accept', object: id });
|
|---|
| 100 | assert.deepEqual(G.listGuardians('kid').map((g) => g.other_uri).sort(), [GRAN, ME].sort());
|
|---|
| [e61c289] | 101 | });
|
|---|
| 102 |
|
|---|
| [780a7c6] | 103 | test('a single Reject from a required party voids the offer (§3.2)', async () => {
|
|---|
| 104 | // parent offers to guard gran (who is free).
|
|---|
| 105 | const off = await G.handleGuardianshipOutbox(parent, {
|
|---|
| 106 | type: 'Offer', object: { type: 'Relationship', subject: GRAN, relationship: 'shaer:Guardian', object: ME },
|
|---|
| 107 | });
|
|---|
| 108 | const id = offerIdFrom(off);
|
|---|
| 109 | await G.handleGuardianshipOutbox(gran, { type: 'Reject', object: id });
|
|---|
| 110 | const q = G.offersCollection(`${ME}/queues/offers`, 'parent', ME).orderedItems.find((o) => o.id === id);
|
|---|
| 111 | assert.equal(q, undefined, 'voided offer leaves the queue');
|
|---|
| 112 | assert.equal(G.listWards('parent').some((w) => w.other_uri === GRAN), false);
|
|---|
| [e61c289] | 113 | });
|
|---|
| 114 |
|
|---|
| [780a7c6] | 115 | test('a ward cannot become a guardian (§1)', async () => {
|
|---|
| [e61c289] | 116 | const r = await G.handleGuardianshipOutbox(kid, {
|
|---|
| [780a7c6] | 117 | type: 'Offer', object: { type: 'Relationship', subject: A('someone'), relationship: 'shaer:Guardian', object: KID },
|
|---|
| [e61c289] | 118 | });
|
|---|
| 119 | assert.equal(r.status, 403);
|
|---|
| 120 | assert.equal(r.error, 'a_ward_cannot_guard');
|
|---|
| 121 | });
|
|---|
| 122 |
|
|---|
| [780a7c6] | 123 | test('only the candidate may offer (§3.1 fixed initiator)', async () => {
|
|---|
| 124 | const r = await G.handleGuardianshipOutbox(parent, {
|
|---|
| 125 | type: 'Offer', object: { type: 'Relationship', subject: A('newkid'), relationship: 'shaer:Guardian', object: GRAN },
|
|---|
| 126 | });
|
|---|
| 127 | assert.equal(r.status, 403);
|
|---|
| 128 | assert.equal(r.error, 'only_the_candidate_offers');
|
|---|
| 129 | });
|
|---|
| 130 |
|
|---|
| [e61c289] | 131 | test('helpRequest props only ride direct notes', () => {
|
|---|
| 132 | assert.equal(G.isHelpRequest({ 'shaer:helpRequest': true }), true);
|
|---|
| 133 | assert.equal(G.isHelpRequest({}), false);
|
|---|
| 134 | });
|
|---|