LifeMate — Personal, Family & Learning Companion
test(catalog): isolate invalid-import assertions from concurrent SQL fixture writes
davoodmehraban89 committed
Oct 15, 2026 at 01:37 UTC
5da6c4290e422162c13c3fe5260345770dde7ffa
1 file changed
+22
-3
backend/tests/learning_catalog.test.js
+22
-3
@@ -1,6 +1,8 @@
1
import test, { after } from 'node:test';
2
import assert from 'node:assert/strict';
3
import fs from 'node:fs';
4
+import crypto from 'node:crypto';
5
+import { stableBookKey } from '../src/learning_catalog.js';
6
import os from 'node:os';
7
import path from 'node:path';
8
import { spawnSync } from 'node:child_process';
@@ -17,9 +19,26 @@ test('offline importer keeps repeat IDs stable, HTTP evidence exact and metadata
19
const rows=await read();assert.equal(rows.length,1);assert.equal(rows[0].source_evidence_url,'http://www.chap.sch.ir/');assert.equal(rows[0].publication_status,'draft');assert.equal(run(data).status,0);assert.deepEqual(await read(),rows);
20
});
21
test('invalid hashes and provider injection fail before catalogue mutation',async()=>{
20
- const before=(await pool.query('select count(*)::int n from textbook_catalog')).rows[0].n;
21
- for(const change of [v=>{v.aparatId='../bad?token=x';},v=>{v.creatorUrl='https://www.aparat.com.evil.test/teacher';}]){const data=dataset();data.books[0].bookCode='598';change(data.books[0].units[0].videos[0]);const result=run(data);assert.notEqual(result.status,0);assert.match(result.stderr,/invalid_catalog_(aparat_hash|video_source)/);}
22
- assert.equal((await pool.query('select count(*)::int n from textbook_catalog')).rows[0].n,before);
22
+ // Other fixture tests write to this shared synthetic database concurrently.
23
+ // Scope the invariant to a unique rejected book, not a global row count.
24
+ const code=String(crypto.randomInt(10000000,100000000));
25
+ const book={...dataset().books[0],bookCode:code};
26
+ const key=stableBookKey(book);
27
+ const snapshot=async()=>({
28
+ books:(await pool.query('select id from textbook_catalog where stable_book_key=$1',[key])).rows,
29
+ subjects:(await pool.query('select id from curriculum_subject where code=$1',[key])).rows,
30
+ });
31
+ const before=await snapshot();
32
+ assert.deepEqual(before,{books:[],subjects:[]});
33
+ for(const change of [v=>{v.aparatId='../bad?token=x';},v=>{v.creatorUrl='https://www.aparat.com.evil.test/teacher';}]){
34
+ const data=dataset();
35
+ data.books[0].bookCode=code;
36
+ change(data.books[0].units[0].videos[0]);
37
+ const result=run(data);
38
+ assert.notEqual(result.status,0);
39
+ assert.match(result.stderr,/invalid_catalog_(aparat_hash|video_source)/);
40
+ assert.deepEqual(await snapshot(),before,'rejected fixture must not write its book or subject');
41
+ }
42
});
43
test('publication rejects incomplete pedagogical review despite free metadata',async()=>{await assert.rejects(pool.query("update learning_video_resource set publication_status='published',review_record='{}'::jsonb where aparat_id='Ab123'"),{code:'23514'});});
44
function fullReview(row){return {reviewerName:'Synthetic local reviewer',reviewedAt:'2026-10-09T10:00:00Z',bookCode:'599',schoolYear:'1405-1406',unitKey:'lesson-01',bookEvidenceUrl:'http://www.chap.sch.ir/',unitEvidenceUrl:'http://www.chap.sch.ir/',materialHash:row.material_hash,watchedEntireVideo:true,editionAligned:true,accuracyApproved:true,suitabilityApproved:true,embedRightsApproved:true,questionCoverageReviewed:true,alignmentNotes:'Synthetic alignment evidence',accuracyNotes:'Synthetic accuracy evidence',suitabilityNotes:'Synthetic age suitability',embedRightsEvidence:'Synthetic rights fixture, never a real permission',questionCoverageNotes:'Synthetic question coverage'};}