const { test } = require('node:test') const assert = require('node:assert/strict') const crypto = require('node:crypto') const { execFileSync } = require('node:child_process') const { recoveryFingerprintsSQL } = require('./wiki_recovery_budget_sql.cjs') const literal = value => `'${String(value).replaceAll("'", "''")}'` const hash = fields => { const h = crypto.createHash('sha256') for (const field of fields) { const bytes = Buffer.from(field), size = Buffer.alloc(8) size.writeBigUInt64BE(BigInt(bytes.length)) h.update(size).update(bytes) } return h.digest('hex') } test('PostgreSQL fingerprints match byte-framed repository fields including UTF-8 and encrypted model parameters', { skip: process.env.VERIFY_RECOVERY_SQL !== '1' }, () => { const fingerprints = recoveryFingerprintsSQL() const sourceFields = ['wiki-recovery-source-v1', '7', 'kb', 'doc', "中文 ' title", 'chunk', '首行\nsecond', '3', '1', '0', '12', 'text', '', '', 'true'] const modelFields = ['wiki-recovery-model-v1', '{"synthesis_model_id": "chat"}', '{"wiki_enabled": true}', 'chat', 'model', 'KnowledgeQA', 'remote', 'active', '{"api_key": "encrypted-fixture"}', 'embed', 'embedding', 'Embedding', 'remote', 'active', '{}'] const query = `WITH d AS (SELECT 7::bigint AS tenant_id,'kb'::text AS scope_id,'doc'::text AS related_id), n AS (SELECT ${literal(sourceFields[4])}::text AS title), k AS (SELECT ${literal(modelFields[1])}::jsonb AS wiki_config,${literal(modelFields[2])}::jsonb AS indexing_strategy,'chat'::text AS summary_model_id,'embed'::text AS embedding_model_id), chunks AS (SELECT 7 AS tenant_id,'kb'::text AS knowledge_base_id,'doc'::text AS knowledge_id,NULL::timestamp AS deleted_at, 'chunk'::text AS id,${literal(sourceFields[6])}::text AS content,3 AS content_revision,1 AS chunk_index,0 AS start_at,12 AS end_at,'text'::text AS chunk_type,NULL::text AS parent_chunk_id,''::text AS image_info,true AS is_enabled), models AS (SELECT 7 AS tenant_id,'chat'::text AS id,'model'::text AS name,'KnowledgeQA'::text AS type,'remote'::text AS source,'active'::text AS status,${literal(modelFields[8])}::jsonb AS parameters,NULL::timestamp AS deleted_at UNION ALL SELECT 7,'embed','embedding','Embedding','remote','active','{}'::jsonb,NULL::timestamp) SELECT json_build_object('source',${fingerprints.source},'model',${fingerprints.model}) FROM d,k,n;` const result = execFileSync('docker', ['--context', 'colima-yawan', 'exec', '-i', '-e', 'PGOPTIONS=-c default_transaction_read_only=on', 'WeKnora-postgres', 'psql', '-X', '-qAt', '-U', 'postgres', '-d', 'yawan_native_acceptance_20260915', '-v', 'ON_ERROR_STOP=1'], { input: query, encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'] }) assert.deepEqual(JSON.parse(result), { source: hash(sourceFields), model: hash(modelFields) }) })