import test from 'node:test';import assert from 'node:assert/strict'; import {fixture} from './rebellion-fixture.mjs'; import {initializeBoardStates,createBoardStateKeys} from '../node/board-state-keys.mjs'; import {initializeBoardDelegations} from '../node/board-control-delegation.mjs'; import {boardSourceObservations} from '../node/board-source-watch.mjs'; import {createBoardDeedResolver} from '../node/board-deed-source.mjs'; import {initializeClanCharters} from '../node/clan-charters.mjs'; import {initializePassiveOrders} from '../node/political-passive-orders.mjs'; import {POLITICAL_APPOINTMENT_SCHEMA} from '../node/political-appointments.mjs'; import {SMUGGLER_COMPANY_ID} from '../node/clandestine-identity.mjs'; test('background source observation skips unchanged full deeds and catches roster, rename and archive changes without touching original keys',async()=>{ const f=await fixture();try{ await f.db.query('ALTER TABLE site_clans ADD COLUMN version INTEGER DEFAULT 1');await f.db.query('ALTER TABLE site_clan_members ADD COLUMN joined_at BIGINT DEFAULT 1'); await initializeClanCharters(f.db,{mysql:false});await initializePassiveOrders(f.db);for(const sql of POLITICAL_APPOINTMENT_SCHEMA)await f.db.query(sql); await initializeBoardStates(f.db);await initializeBoardDelegations(f.db);const id=await f.create(); const resolve=createBoardDeedResolver(f.db,{clock:f.now});let reads=0; const boards=createBoardStateKeys(f.db,f.keys,{resolveSource:async(...args)=>{reads++;return resolve(...args);},authorizePublisher:async()=>false,clock:f.now}); const select=rows=>rows.find(r=>r.kind==='company'&&r.id===id); let change=select(await boardSourceObservations(f.db,{clock:f.now}));assert.ok(change);const first=await boards.refresh(change.kind,change.id,change);const resolved=reads; assert.equal(select(await boardSourceObservations(f.db,{clock:f.now})),undefined);assert.equal(reads,resolved); await f.db.query('INSERT INTO site_company_members(company_id,user_id,joined_at) VALUES(?,?,100)',[id,f.people.Member.id]);change=select(await boardSourceObservations(f.db,{clock:f.now}));assert.ok(change);const roster=await boards.refresh(change.kind,change.id,change);assert.equal(roster.revision,2);assert.equal(roster.desired.source.version,first.desired.source.version); await f.db.query("UPDATE corrino_companies SET name='Renamed' WHERE id=?",[id]);change=select(await boardSourceObservations(f.db,{clock:f.now}));assert.ok(change);await boards.refresh(change.kind,change.id,change); await f.db.query("UPDATE corrino_companies SET status='archived' WHERE id=?",[id]);change=select(await boardSourceObservations(f.db,{clock:f.now}));assert.ok(change);const archived=await boards.refresh(change.kind,change.id,change);assert.equal(archived.desired.organisation.lifecycle,'archived');assert.equal(archived.keyId,first.keyId); assert.equal(select(await boardSourceObservations(f.db,{clock:f.now})),undefined); await f.db.query("INSERT INTO corrino_companies(id,slug,name,founded_by,status,created_at,updated_at) VALUES(?,'hidden-fixture','Hidden institution',?,'active',1,1)",[SMUGGLER_COMPANY_ID,f.people.Owner.id]); assert.equal((await boardSourceObservations(f.db,{clock:f.now})).some(r=>r.id===SMUGGLER_COMPANY_ID),false,'hidden institutions never trigger ordinary forum publication'); }finally{f.db.close();} });