import test from 'node:test';import assert from 'node:assert/strict';import {ACCOUNT_MIGRATION_ALLOWLIST} from '../node/minimal-account-migration.mjs';import {accountExportQueries,readMinimalAccountExport} from '../node/minimal-account-export.mjs'; test('export SQL uses explicit allowed fields and read-only selects',()=>{const queries=accountExportQueries();assert.equal(queries.length,Object.keys(ACCOUNT_MIGRATION_ALLOWLIST).length);for(const {table,sql} of queries){assert.match(sql,/^SELECT /);assert.ok(!sql.includes('*'));assert.ok(!/\b(?:DELETE|UPDATE|DROP|TRUNCATE)\b/i.test(sql));assert.ok(sql.includes('FROM `'+table+'`'));}}); test('schema mismatch blocks export before reading any user data',async()=>{let calls=0;const db={prepare(sql){calls++;assert.match(sql,/information_schema/);return {bind(){return {async all(){return {results:[]};}};}};}};await assert.rejects(readMinimalAccountExport(db),/schema mismatch/);assert.equal(calls,1);}); test('valid empty snapshot can be exported without writing',async()=>{ const columns=Object.entries(ACCOUNT_MIGRATION_ALLOWLIST).flatMap(([TABLE_NAME,cols])=>cols.map(COLUMN_NAME=>({TABLE_NAME,COLUMN_NAME}))); let selects=0; const db={prepare(sql){return { bind(){return {all:async()=>({results:columns})};}, async all(){assert.match(sql,/^SELECT /);selects++;return {results:[]};} };}}; db.readSnapshot=async work=>work(db); const result=await readMinimalAccountExport(db); assert.equal(result.summary.accounts,0); assert.equal(selects,Object.keys(ACCOUNT_MIGRATION_ALLOWLIST).length); }); test('export refuses databases without consistent snapshot support',async()=>{const columns=Object.entries(ACCOUNT_MIGRATION_ALLOWLIST).flatMap(([TABLE_NAME,cols])=>cols.map(COLUMN_NAME=>({TABLE_NAME,COLUMN_NAME})));const db={prepare(){return {bind(){return {all:async()=>({results:columns})};}};}};await assert.rejects(readMinimalAccountExport(db),/Consistent read-only snapshot required/);});