import {test} from 'node:test'; import assert from 'node:assert/strict'; import {webcrypto} from 'node:crypto'; import {parseCSV,generateBatch,zipCertificates,validateSettings,LABELS} from '../certificate-core.js'; globalThis.crypto ??= webcrypto; const settings={term:'2027A',lecturer:'Example Lecturer',date:'2027-06-30'}; const input='\uFEFFemail,name,grade\r\none@example.com,"Example, אחד",89.999\r\ntwo@example.com,Example Two,90\r\nthree@example.com,Example Three,94.999\r\nfour@example.com,Example Four,95\r\n'; const decode=s=>Buffer.from(s,'base64url'); test('quoted Unicode CSV, exact distinction boundaries and validation',()=>{ const students=parseCSV(input);assert.equal(students[0].name,'Example, אחד'); assert.deepEqual(students.map(s=>s.distinction),[LABELS[0],LABELS[1],LABELS[1],LABELS[2]]); for(const text of ['email,name,grade\na@example.com,Example,NaN','email,name,grade\na@example.com,Example,101','email,name,grade\na@example.com,Example,90\nA@example.com,Other,95','email,name,grade\na@example.com,"unclosed,90','email,name,grade\na@example.com,,90','email,name,grade\na@example.com,Example,100.0000000000000000001'])assert.throws(()=>parseCSV(text)); assert.throws(()=>validateSettings({...settings,date:'2027-02-30'}));assert.throws(()=>validateSettings({...settings,term:'../2027A'})); }); test('independent keys, compatible decryption and ZIP with ciphertext only',async()=>{ const students=parseCSV(input),batch=await generateBatch(settings,students),rows=batch.recipients.trim().split('\r\n'); assert.equal(rows[0],'email,name,grade,link'); const keys=new Set(); for(let i=0;i{ const batch=await generateBatch(settings,parseCSV('email,name,grade\n+fictional@example.com,=1+1,95')); assert.ok(batch.recipients.includes('"\'+fictional@example.com"'));assert.ok(batch.recipients.includes('"\'=1+1"')); const abort=new AbortController();abort.abort();await assert.rejects(generateBatch(settings,parseCSV(input),()=>{},abort.signal),{name:'AbortError'}); assert.throws(()=>zipCertificates([{name:'../secrets',data:new Uint8Array()}])); });