import assert from 'node:assert/strict'; import test from 'node:test'; import fs from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; import { randomBytes, pbkdf2Sync, createCipheriv, createHmac } from 'node:crypto'; import { isSuppressed, reachedHalf, suppressionDuration } from '../src/utils/drawer-preference.ts'; import { serviceInquiryCopy } from '../src/data/service-inquiry.ts'; import { decryptPortalStream, encryptPortalStream, validatePortalPayload, portalAlgorithm } from '../src/utils/portal-crypto.mjs'; import { collectResource, resourceReferences, rewriteResources } from './portal-resources.mjs'; import { isFormACompletion } from '../src/utils/jotform-completion.ts'; import { portalLinkPresentation } from '../src/utils/portal-presentation.ts'; test('completion envelope requires exact origin, active frame window and string Form A ID', () => { const source = {}; const check = (event, frame, active) => isFormACompletion(event, frame, active, 'https://form.jotform.com/262946078314059?formContext=consultation&language=en'); const data = { action: 'submission-completed', formID: '262946078314059' }; const event = { origin: 'https://form.jotform.com', source, data }; assert.equal(check(event, source, true), true); assert.equal(check({ ...event, data: JSON.stringify(data) }, source, true), true); for (const change of [ { origin: 'https://form.jotform.com.evil.invalid' }, { origin: 'null' }, { origin: 'http://form.jotform.com' }, { source: {} }, { source: null }, { data: { ...data, formID: 'other' } }, { data: { ...data, formID: 262946078314059 } }, { data: { ...data, action: 'load' } }, { data: '{broken' }, { data: [] }, { data: null }, { data: 'x'.repeat(4097) }, ]) assert.equal(check({ ...event, ...change }, source, true), false); assert.equal(check(event, source, false), false); assert.equal(check(event, null, true), false); assert.equal(isFormACompletion(event, source, true, undefined), false); assert.equal(isFormACompletion(event, source, true, 'broken'), false); }); test('portal presentation translates only explicit button tokens/brand fills and contact protocols', () => { assert.equal(portalLinkPresentation(['hs-button', 'untrusted'], '', 'https:'), 'portal-resource-button'); assert.equal(portalLinkPresentation([], 'rgb(255, 189, 89)', 'https:'), 'portal-resource-button'); assert.equal(portalLinkPresentation(['arbitrary'], 'url(https://example.invalid)', 'https:'), undefined); assert.equal(portalLinkPresentation([], '', 'mailto:'), 'portal-contact-link'); assert.equal(portalLinkPresentation([], '', 'tel:'), 'portal-contact-link'); }); test('global preference validates expiry, corrupted values and clock anomalies', () => { const now = 10000000000; for (const raw of [null, '', 'NaN', '-1', '1.5', 'Infinity', String(now), String(now - 1), String(now + suppressionDuration + 1)]) assert.equal(isSuppressed(raw, now), false); assert.equal(isSuppressed(String(now + suppressionDuration), now), true); assert.equal(isSuppressed(String(now + 1), now), true); }); test('half-scroll handles short, restored, resized and overscrolled pages', () => { assert.equal(reachedHalf(0, 500, 800), false); assert.equal(reachedHalf(100, 800, 800), false); assert.equal(reachedHalf(499, 1800, 800), false); assert.equal(reachedHalf(500, 1800, 800), true); assert.equal(reachedHalf(-10, 1800, 800), false); assert.equal(reachedHalf(500, 2800, 800), false); }); test('four service configurations are bilingual without adding an Academy route', async () => { assert.equal(Object.keys(serviceInquiryCopy).length, 4); for (const variants of Object.values(serviceInquiryCopy)) for (const copy of Object.values(variants)) { assert.ok(copy.heading && copy.introduction); assert.ok(!copy.introduction.includes('—')); } const { formAConsultationTestPlacements } = await import('../src/utils/form-integration.ts'); assert.equal(formAConsultationTestPlacements.length, 9); assert.ok(!formAConsultationTestPlacements.some((p) => p.route.includes('preparatory'))); }); test('legacy independent crypto fixture and two-view separation round-trip', async () => { const pass = randomBytes(32).toString('base64'), other = randomBytes(32).toString('base64'); const salt = randomBytes(16), iv = randomBytes(16), keys = pbkdf2Sync(pass, salt, 200000, 64, 'sha256'); const cipher = createCipheriv('aes-256-cbc', keys.subarray(0, 32), iv); const ct = Buffer.concat([cipher.update('
Synthetic
', pass))); const file = path.join(dir, 'fixture.json'); await fs.writeFile(file, JSON.stringify({ alg: portalAlgorithm, streams }), { mode: 0o600 }); const options = { approved: 'true', indexing: 'false', environment: 'isolated-test', file }; assert.ok(readPortalInput(options)); for (const changed of [{ approved: undefined }, { approved: 'false' }, { indexing: 'true' }, { indexing: undefined }, { environment: 'production' }, { file: '/missing-fixture' }]) assert.equal(readPortalInput({ ...options, ...changed }), undefined); await fs.writeFile(file, '