16 lines
5.8 KiB
JavaScript
16 lines
5.8 KiB
JavaScript
import test from 'node:test';import assert from 'node:assert/strict';import {DatabaseSync} from 'node:sqlite';
|
|
import {createOrganisationKeys,hashOrganisationKey} from '../node/organisation-keys.mjs';
|
|
import {createCodexEditions} from '../node/codex-editions.mjs';
|
|
import {formatOrganisationCode,parseOrganisationCode} from '../control-panel/public/codex-code-format.mjs';
|
|
import {describeRedemption} from '../control-panel/public/cp-v2/cp-v2-code-redemption.mjs';
|
|
const owner='11111111-1111-4111-8111-111111111111',other='22222222-2222-4222-8222-222222222222',target='33333333-3333-4333-8333-333333333333',request='44444444-4444-4444-8444-444444444444';
|
|
const initial={stage:'alpha',version:'1.0.1.0',season:'S01'};
|
|
function fixture(){const sql=new DatabaseSync(':memory:');sql.exec(`CREATE TABLE organisation_keys(id TEXT PRIMARY KEY,account_id TEXT,key_type TEXT,scope_id TEXT,code_hash TEXT UNIQUE,code_sealed TEXT,created_at INT,expires_at INT,revoked_at INT,used_at INT,target_id TEXT,request_id TEXT,UNIQUE(account_id,key_type,scope_id));CREATE TABLE organisation_key_events(id TEXT,key_id TEXT,account_id TEXT,event_type TEXT,target_id TEXT,created_at INT);CREATE TABLE codex_instance_editions(instance_id TEXT PRIMARY KEY,release_stage TEXT,release_version TEXT,season_id TEXT,recorded_at INT,legacy_baseline INT);`);const db={async query(statement,args=[]){try{statement=statement.replace(/ FOR UPDATE/g,'').replace('INSERT IGNORE','INSERT OR IGNORE');const q=sql.prepare(statement);if(/^SELECT/.test(statement))return q.all(...args);return q.run(...args);}catch(e){if(/UNIQUE constraint/.test(e.message))e.code='ER_DUP_ENTRY';throw e;}},async first(statement,args=[]){return(await db.query(statement,args))[0]||null;},async transaction(fn){sql.exec('BEGIN');try{const result=await fn(db);sql.exec('COMMIT');return result;}catch(e){sql.exec('ROLLBACK');throw e;}}};return{sql,db,close:()=>sql.close()};}
|
|
|
|
test('edition envelopes preserve all 192 secret bits and the existing lookup identity',()=>{const raw='CLAN-'+ 'ABCDEF12'.repeat(6),code=formatOrganisationCode(raw,initial);assert.match(code,/^CLAN-ALPHA-1\.0\.1\.0-S01-/);assert.equal(parseOrganisationCode(code).canonical,raw);assert.equal(hashOrganisationKey(code,'clan-founding'),hashOrganisationKey(raw,'clan-founding'));assert.equal(describeRedemption(code).code,code);assert.equal(describeRedemption(code).available,true);for(const invalid of [code.replace('-S01-','-../-'),code+'X',code.replace('-1.0.1.0-','-01.0.1.0-')])assert.throws(()=>parseOrganisationCode(invalid));});
|
|
|
|
test('legacy backfill preserves secrets, rights and first edition across later seasons',async()=>{const f=fixture();try{const legacy=createOrganisationKeys(f.db,{secret:'ab'.repeat(32)}),issued=await legacy.issue({accountId:owner,keyType:'clan-founding',scopeId:'original'}),before=f.sql.prepare('SELECT * FROM organisation_keys').get();let current=initial;const editions=createCodexEditions(f.db,{edition:initial,resolveEdition:async()=>current});await editions.backfill();await editions.backfill();const keys=createOrganisationKeys(f.db,{secret:'ab'.repeat(32),editions}),rows=await keys.list(owner);assert.equal(rows.length,1);assert.equal(rows[0].id,issued.id);assert.equal(rows[0].edition.legacyBaseline,true);assert.equal(parseOrganisationCode(rows[0].code).canonical,issued.code);assert.deepEqual(f.sql.prepare('SELECT * FROM organisation_keys').get(),before);current={stage:'beta',version:'1.1.0.0',season:'S02'};await createCodexEditions(f.db,{edition:current}).backfill();assert.equal((await keys.list(owner))[0].edition.season,'S01');assert.equal((await keys.issue({accountId:owner,keyType:'clan-founding',scopeId:'original'})).created,false);const fresh=await keys.issue({accountId:other,keyType:'company-founding',scopeId:'original'});assert.match(fresh.code,/^COM-BETA-1\.1\.0\.0-S02-/);assert.equal(f.sql.prepare('SELECT COUNT(*) n FROM organisation_keys').get().n,2);}finally{f.close();}});
|
|
|
|
test('altered edition and foreign owner cannot redeem; old and new code forms share single-use state',async()=>{const f=fixture();try{const editions=createCodexEditions(f.db,{edition:initial}),keys=createOrganisationKeys(f.db,{secret:'ab'.repeat(32),editions}),issued=await keys.issue({accountId:owner,keyType:'clan-founding',scopeId:'original'});let applied=0;const args={accountId:owner,keyType:'clan-founding',code:issued.code,targetId:target,requestId:request,apply:async()=>{applied++;}};await assert.rejects(keys.consume({...args,code:issued.code.replace('-S01-','-S02-')}),/edition/);await assert.rejects(keys.consume({...args,accountId:other}),/account/);assert.equal((await keys.consume(args)).replayed,false);assert.equal((await keys.consume({...args,code:parseOrganisationCode(issued.code).canonical})).replayed,true);await assert.rejects(keys.consume({...args,requestId:other}),/already used/);assert.equal(applied,1);assert.equal((await keys.list(owner))[0].code,null);}finally{f.close();}});
|
|
|
|
test('a failed domain action rolls back usage and a failed edition write creates no entitlement',async()=>{const f=fixture();try{const keys=createOrganisationKeys(f.db,{secret:'ab'.repeat(32),editions:createCodexEditions(f.db,{edition:initial})}),issued=await keys.issue({accountId:owner,keyType:'clan-founding',scopeId:'original'});await assert.rejects(keys.consume({accountId:owner,keyType:'clan-founding',code:issued.code,targetId:target,requestId:request,apply:async()=>{throw Error('domain unavailable');}}),/domain unavailable/);assert.equal((await keys.list(owner))[0].status,'available');f.sql.exec("CREATE TRIGGER fail_edition BEFORE INSERT ON codex_instance_editions BEGIN SELECT RAISE(ABORT,'edition unavailable');END");await assert.rejects(keys.issue({accountId:other,keyType:'company-founding',scopeId:'original'}),/edition unavailable/);assert.equal(f.sql.prepare('SELECT COUNT(*) n FROM organisation_keys').get().n,1);}finally{f.close();}});
|

Staff