source: Klonkt/test/guardianship.test.js@ 780a7c6

main
Last change on this file since 780a7c6 was 780a7c6, checked in by Robin Genis <roboburr@…>, 7 weeks ago

Guardianship Fase 0+1: de echte multi-party handshake (FEP-633c §3)

De eerste versie committeerde na één accept. Nu de spec: geen enkele partij
maakt een voogdij alleen, en een nieuwe guardian erbij kan niet zonder
toestemming van de bestaande. Daemon als blauwdruk, zodat Klonkt en de
test-daemon exact hetzelfde gedragen en de Shaer-clients één contract lezen.

Fase 0 (datamodel): ap_guardian_offers (per lokale partij een kopie van de
handshake, PK slug+offer_id) + ap_guardian_offer_accepts (de accept-tally).
ap_guardianships houdt alleen nog de GECOMMITTE relaties.

Fase 1 (state-machine): offers.js is een getrouwe port van de daemon-Handshake
(accepts over ward+candidate+existing; ready = ward && candidate && (geen
existing OF >=1 existing); een Reject voidt). handshake.js orchestreert het
gedistribueerd: de kandidaat adresseert de Offer aan ward + alle bestaande
guardians (§3.1.1); elke Accept wordt aan alle andere partijen gebroadcast, dus
elke instance-kopie convergeert; zodra een kopie compleet is committeert die
lokaal (ward schrijft shaer:guardians, guardian schrijft z'n ward), met de
kandidaat-inbox als handle (§6). Volgorde-onafhankelijk.

Ook: §1 wederzijdse uitsluiting (een ward is nooit ook guardian in het
actor-doc), de queues vullen nu de echte accept-tally (needsMyAccept/
readyToCommit/acceptedBy/existingGuardians), en de PWA + Berichten beantwoorden
via de C2S Accept/Reject-pijplijn per offer-id. De co-guardian ziet een
mede-voogdij-aanvraag met accepteer/weiger in de PWA.

Changed files:
src/config/database.js

  • tabellen ap_guardian_offers + ap_guardian_offer_accepts

src/services/guardianship/offers.js (NEW)

  • de handshake-state-machine (daemon-port), per-instance in SQLite

src/services/guardianship/relations.js

  • alleen commit-writers + actor-props (§1 uitsluiting)

src/services/guardianship/handshake.js

  • gedistribueerde multi-party C2S/S2S orchestratie

src/services/guardianship/queues.js

  • offers-queue uit de state-machine

src/services/guardianship/index.js

  • exports bijgewerkt

src/services/ActivityPubService.js

  • wire localSlug + fetchActor; inbound-routing naar alle lokale partijen

src/routes/guardian.js

  • dashboard toont offers met tally; POST /guardian/offer (accept/reject)

src/routes/posts.js

  • Berichten toont ward-offers uit de state-machine; accept via offer-id

src/views/pages/messages.ejs, src/assets/js/guardian.js, src/assets/css/guardian.css

  • offer-kaarten per state (mijn aanvraag / mede-voogdij / wachten)

src/services/i18n.js

  • accept/reject/complete/coguard + co-guardian push (nl/en/de)

test/guardianship.test.js

  • multi-party: eerste guardian, co-approval bestaande guardian, reject voidt, ward-mag-niet-guarden, vaste initiator

remarks: Fase 2 (follow-gating), 3 (hasGuardians + Not-a-Teapot), 4 (Undo/
emancipatie) volgen. 164 tests groen.

-robo
Co-Authored-By: Claude Opus 4.8 <noreply@…>

  • Property mode set to 100644
File size: 6.7 KB
RevLine 
[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]4import { test } from 'node:test';
5import assert from 'node:assert/strict';
6
7process.env.DATABASE_PATH = ':memory:';
8process.env.PUBLIC_BASE_URL = 'https://test.example';
9
10const dbMod = await import('../src/config/database.js');
11const db = dbMod.default;
12dbMod.initializeDatabase();
13const AP = (await import('../src/services/ActivityPubService.js')).default;
14const G = await import('../src/services/guardianship/index.js');
15
[780a7c6]16function 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]20db.prepare('INSERT INTO users (id, username, email, password_hash, role) VALUES (?,?,?,?,?)').run('u1', 'u1', 'u1@test', 'x', 'god');
[780a7c6]21const parent = site('s1', 'parent'); // first guardian-candidate
22const kid = site('s2', 'kid'); // ward
23const gran = site('s3', 'gran'); // second guardian-candidate (co-approver later)
24const A = (slug) => `https://test.example/ap/users/${slug}`;
25const [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]29G.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]43const offerIdFrom = (r) => r.id;
[e61c289]44
[780a7c6]45test('first guardian: candidate offers, ward accepts, candidate completes', async () => {
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
[780a7c6]52 // The kid sees the offer and it needs its accept.
53 const kidQ = G.offersCollection(`${KID}/queues/offers`, 'kid', KID).orderedItems;
54 assert.equal(kidQ.length, 1);
55 assert.equal(kidQ[0]['shaer:needsMyAccept'], true);
56 assert.equal(kidQ[0]['shaer:iAmCandidate'], false);
57
58 // Not committed on a lone candidate — the ward has not accepted.
59 assert.deepEqual(G.listGuardians('kid'), []);
60
61 // The kid accepts (C2S from the kid's own Klonkt). Not committed yet: the
62 // candidate must still agree to serve (§3.1.2).
63 await G.handleGuardianshipOutbox(kid, { type: 'Accept', object: id });
64 assert.deepEqual(G.listGuardians('kid'), []);
65 const parentQ = G.offersCollection(`${ME}/queues/offers`, 'parent', ME).orderedItems;
66 assert.equal(parentQ[0]['shaer:iAmCandidate'], true);
67 assert.equal(parentQ[0]['shaer:needsMyAccept'], true); // candidate has not accepted
68
69 // The candidate accepts → tally complete → commit everywhere.
70 const done = await G.handleGuardianshipOutbox(parent, { type: 'Accept', object: id });
71 assert.equal(done.committed, true);
72 assert.deepEqual(G.listGuardians('kid').map((g) => g.other_uri), [ME]);
73 assert.deepEqual(G.listWards('parent').map((w) => w.other_uri), [KID]);
74
75 // The ward actor now names its guardian; parent reads as guardian (§2).
76 assert.deepEqual(AP.buildActor('https://test.example', kid)['shaer:guardians'], [ME]);
77 assert.equal(AP.buildActor('https://test.example', parent)['shaer:isGuardian'], true);
78 // §1 mutual exclusion: the ward is not also a guardian.
79 assert.equal(AP.buildActor('https://test.example', kid)['shaer:isGuardian'], undefined);
[e61c289]80});
81
[780a7c6]82test('second guardian needs the EXISTING guardian to co-accept (§3.1.2)', async () => {
83 // Gran offers to also guard the kid (who already has parent).
84 const off = await G.handleGuardianshipOutbox(gran, {
85 type: 'Offer', object: { type: 'Relationship', subject: KID, relationship: 'shaer:Guardian', object: GRAN },
[e61c289]86 });
[780a7c6]87 const id = offerIdFrom(off);
88 // The existing guardian (parent) is a party and must accept.
89 const parentQ = G.offersCollection(`${ME}/queues/offers`, 'parent', ME).orderedItems.find((o) => o.id === id);
90 assert.ok(parentQ, 'parent sees the co-guardianship offer');
91 assert.deepEqual(parentQ['shaer:existingGuardians'], [ME]);
[e61c289]92
[780a7c6]93 // Kid accepts, then gran (candidate) accepts — still NOT committed, because
94 // the existing guardian (parent) has not co-accepted (§3.1.2).
95 await G.handleGuardianshipOutbox(kid, { type: 'Accept', object: id });
96 const early = await G.handleGuardianshipOutbox(gran, { type: 'Accept', object: id });
97 assert.equal(early.committed, false);
98 assert.equal(G.listGuardians('kid').length, 1, 'still just the first guardian');
[e61c289]99
[780a7c6]100 // The existing guardian co-accepts → tally complete → commit.
101 await G.handleGuardianshipOutbox(parent, { type: 'Accept', object: id });
102 assert.deepEqual(G.listGuardians('kid').map((g) => g.other_uri).sort(), [GRAN, ME].sort());
[e61c289]103});
104
[780a7c6]105test('a single Reject from a required party voids the offer (§3.2)', async () => {
106 // parent offers to guard gran (who is free).
107 const off = await G.handleGuardianshipOutbox(parent, {
108 type: 'Offer', object: { type: 'Relationship', subject: GRAN, relationship: 'shaer:Guardian', object: ME },
109 });
110 const id = offerIdFrom(off);
111 await G.handleGuardianshipOutbox(gran, { type: 'Reject', object: id });
112 const q = G.offersCollection(`${ME}/queues/offers`, 'parent', ME).orderedItems.find((o) => o.id === id);
113 assert.equal(q, undefined, 'voided offer leaves the queue');
114 assert.equal(G.listWards('parent').some((w) => w.other_uri === GRAN), false);
[e61c289]115});
116
[780a7c6]117test('a ward cannot become a guardian (§1)', async () => {
[e61c289]118 const r = await G.handleGuardianshipOutbox(kid, {
[780a7c6]119 type: 'Offer', object: { type: 'Relationship', subject: A('someone'), relationship: 'shaer:Guardian', object: KID },
[e61c289]120 });
121 assert.equal(r.status, 403);
122 assert.equal(r.error, 'a_ward_cannot_guard');
123});
124
[780a7c6]125test('only the candidate may offer (§3.1 fixed initiator)', async () => {
126 const r = await G.handleGuardianshipOutbox(parent, {
127 type: 'Offer', object: { type: 'Relationship', subject: A('newkid'), relationship: 'shaer:Guardian', object: GRAN },
128 });
129 assert.equal(r.status, 403);
130 assert.equal(r.error, 'only_the_candidate_offers');
131});
132
[e61c289]133test('helpRequest props only ride direct notes', () => {
134 assert.equal(G.isHelpRequest({ 'shaer:helpRequest': true }), true);
135 assert.equal(G.isHelpRequest({}), false);
136});
Note: See TracBrowser for help on using the repository browser.