LifeMate — Personal, Family & Learning Companion
test(qa): assert isolated migration source checksums, scope and drift failures
davoodmehraban89 committed
Oct 15, 2026 at 01:20 UTC
5bda49ef546c98e699415041378947c8d9691cdb
1 file changed
+49
backend/tests/supabase_qa_bundle.test.js
new
+49
@@ -0,0 +1,49 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import { mkdtemp, writeFile, rm } from 'node:fs/promises';
4
+import { tmpdir } from 'node:os';
5
+import { join } from 'node:path';
6
+import { pathToFileURL } from 'node:url';
7
+import { buildSupabaseQaSql } from '../scripts/build-supabase-qa-sql.js';
8
+
9
+test('QA bundle retains 20 original checksums, creates private schema objects and corrects school guard paths', async () => {
10
+ const { sql, manifest } = await buildSupabaseQaSql();
11
+ assert.equal(manifest.length, 20);
12
+ assert.ok(manifest.every((x,i) => x.filename.startsWith(String(i+1).padStart(4,'0')+'_')));
13
+ assert.ok(manifest.every(x => /^[a-f0-9]{64}$/.test(x.sha256)));
14
+ assert.match(sql, /set local search_path=lifeguide_qa,extensions,pg_catalog/);
15
+ assert.equal((sql.match(/set search_path=lifeguide_qa,pg_catalog/gi) ?? []).length, 5);
16
+ assert.equal((sql.match(/set search_path=public,pg_catalog/gi) ?? []).length, 0);
17
+ assert.match(sql, /create table if not exists lifeguide_qa.schema_migration/);
18
+ assert.match(sql, /force row level security/);
19
+ assert.match(sql, /revoke all on schema lifeguide_qa from public, anon, authenticated, service_role/);
20
+ assert.doesNotMatch(sql, /create extension if not exists pgcrypto/i);
21
+ assert.doesNotMatch(sql, /alter schema public owner/i);
22
+ assert.doesNotMatch(sql, /reassign owned/i);
23
+ assert.doesNotMatch(sql, /create role/i);
24
+ assert.equal((sql.match(/^begin;/gm) ?? []).length, 1);
25
+ assert.equal((sql.match(/^commit;/gm) ?? []).length, 1);
26
+});
27
+
28
+test('QA bundle generation fails closed if source migration count is incomplete', async () => {
29
+ const dir = await mkdtemp(join(tmpdir(),'lifeguide-qa-missing-'));
30
+ try {
31
+ await writeFile(join(dir,'0001_identity_family.sql'),'select 1;');
32
+ await assert.rejects(buildSupabaseQaSql({ migrationsDir:pathToFileURL(dir+'/') }),/expected_exactly_20_original_migrations_in_order/);
33
+ } finally { await rm(dir,{recursive:true,force:true}); }
34
+});
35
+
36
+test('QA bundle generation rejects drift adding production schema privileges', async () => {
37
+ const dir = await mkdtemp(join(tmpdir(),'lifeguide-qa-drift-'));
38
+ try {
39
+ for (let n=1;n<=20;n++) {
40
+ const id=String(n).padStart(4,'0');
41
+ const sql=n===1?'create extension if not exists pgcrypto;\nselect 1;':n===19?
42
+ Array(5).fill('create function test_fn() returns int language sql set search_path=public,pg_catalog as $$select 1$$;').join('\n'):
43
+ 'select 1;';
44
+ await writeFile(join(dir,`${id}_test.sql`),sql);
45
+ }
46
+ await writeFile(join(dir,'0004_test.sql'),'create table public.account(id int);');
47
+ await assert.rejects(buildSupabaseQaSql({ migrationsDir:pathToFileURL(dir+'/') }),/source_migration_outside_private_qa:0004_test.sql/);
48
+ } finally { await rm(dir,{recursive:true,force:true}); }
49
+});