forked from unimatrix/web
12 lines
2.9 KiB
JavaScript
12 lines
2.9 KiB
JavaScript
import test from 'node:test';import assert from 'node:assert/strict';import {ACCOUNT_MIGRATION_ALLOWLIST} from '../node/minimal-account-migration.mjs';import {accountImportStatements,importMinimalAccounts,compareImportedAccounts} from '../node/minimal-account-import.mjs';
|
|
const snapshot=()=>({site_accounts:[{id:'a',username:'player',display_name:'Player',password_hash:'hash',password_salt:'salt',role:'user',created_at:1}],site_contact_emails:[{user_id:'a',address:'a@example.com',verified_at:1}],site_game_links:[{user_id:'a',minecraft_uuid:'uuid'}],cp_account_ranks:[{user_id:'a',rank_key:'vip'}],website_badges:[{user_id:'a',badge_key:'alpha-tester',earned_at:1}],seal_entitlements:[]});
|
|
const columns=Object.entries(ACCOUNT_MIGRATION_ALLOWLIST).flatMap(([TABLE_NAME,cols])=>cols.map(COLUMN_NAME=>({TABLE_NAME,COLUMN_NAME})));
|
|
function mock({existing=false,corrupt=false}={}){let inserted={};let executed=[];const source=snapshot();const db={prepare(){return {bind(){return {all:async()=>({results:columns})};}};},transaction:async fn=>fn({first:async sql=>{const table=sql.match(/FROM `([^`]+)`/)?.[1];return {count:(existing?1:0)+(inserted[table]||0)};},query:async(sql,values)=>{if(sql.startsWith('SELECT')){const table=sql.match(/FROM `([^`]+)`/)?.[1];const rows=structuredClone(source[table]);if(corrupt&&table==='site_accounts')rows[0].password_hash='wrong';return rows;}executed.push({sql,values});const table=sql.match(/INTO `([^`]+)`/)?.[1];inserted[table]=(inserted[table]||0)+1;}})};return {db,executed};}
|
|
test('import plan uses only allowlisted insert columns',()=>{const plan=accountImportStatements(snapshot());assert.equal(plan.length,5);assert.ok(plan.every(step=>step.sql.startsWith('INSERT INTO')&&!step.sql.includes('*')));});
|
|
test('import requires explicit empty-target confirmation',async()=>{await assert.rejects(importMinimalAccounts({},snapshot()),/confirmation/);});
|
|
test('nonempty target aborts before writes',async()=>{const x=mock({existing:true});await assert.rejects(importMinimalAccounts(x.db,snapshot(),{confirmedEmptyTarget:true}),/not empty/);assert.equal(x.executed.length,0);});
|
|
test('valid snapshot imports all allowlisted rows',async()=>{const x=mock();const result=await importMinimalAccounts(x.db,snapshot(),{confirmedEmptyTarget:true});assert.equal(result.accounts,1);assert.equal(x.executed.length,5);});
|
|
test('unexpected extra source fields are rejected',()=>{const s=snapshot();s.site_accounts[0].bank_balance=100;assert.throws(()=>accountImportStatements(s),/Unexpected column/);});
|
|
|
|
test('readback mismatch detects altered password hash',async()=>{const x=mock({corrupt:true});await assert.rejects(importMinimalAccounts(x.db,snapshot(),{confirmedEmptyTarget:true}),/Imported data mismatch: site_accounts/);});
|
|
test('readback comparison is order independent',()=>{const expected=snapshot(),actual=structuredClone(expected);assert.equal(compareImportedAccounts(expected,actual),true);});
|

Staff