import assert from 'node:assert/strict'; import { existsSync, readFileSync } from 'node:fs'; import { Buffer } from 'node:buffer'; import ts from 'typescript'; const moduleUrl = new URL('../client/src/utils/captchaImage.ts', import.meta.url); assert.ok( existsSync(moduleUrl), 'client/src/utils/captchaImage.ts should export captcha image source helpers', ); const source = readFileSync(moduleUrl, 'utf8'); const { outputText } = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022, }, }); const dataUrl = `data:text/javascript;base64,${Buffer.from(outputText, 'utf8').toString('base64')}`; const { normalizeCaptchaImageSrc, isDisplayableCaptchaImageSrc } = await import(dataUrl); assert.equal( normalizeCaptchaImageSrc('/9j/4AAQSkZJRgABAQAAAQABAAD'), 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD', ); assert.equal( normalizeCaptchaImageSrc('iVBORw0KGgoAAAANSUhEUgAAAAUA'), 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA', ); assert.equal( normalizeCaptchaImageSrc('data:image/webp;base64,UklGRiIAAABXRUJQVlA4'), 'data:image/webp;base64,UklGRiIAAABXRUJQVlA4', ); assert.equal( normalizeCaptchaImageSrc('https://example.com/captcha.png'), 'https://example.com/captcha.png', ); assert.equal(normalizeCaptchaImageSrc('captcha required'), ''); assert.equal(normalizeCaptchaImageSrc(''), ''); assert.equal(isDisplayableCaptchaImageSrc('/9j/4AAQSkZJRgABAQAAAQABAAD'), true); assert.equal(isDisplayableCaptchaImageSrc('data:image/png;base64,iVBORw0KGgo='), true); assert.equal(isDisplayableCaptchaImageSrc('http://127.0.0.1/captcha.jpg'), true); assert.equal(isDisplayableCaptchaImageSrc('captcha required'), false); console.log('captcha image source checks passed');