Files
quality_analyse/web/verification/browser_compatibility.test.mjs
T

51 lines
4.2 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { webcrypto } from 'node:crypto';
import { readFileSync } from 'node:fs';
import ts from 'typescript';
import { createRequestUuid } from '../src/requestUuid.ts';
import { needsLegacyComponentStyles } from '../src/browserStyleCompatibility.ts';
import { beginAnalysisSubmission } from '../src/analysisSubmission.ts';
const secureOnly = {getRandomValues:bytes=>webcrypto.getRandomValues(bytes)};
const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
function withoutNativeUuid(run) {
const previous=Object.getOwnPropertyDescriptor(globalThis,'crypto');
Object.defineProperty(globalThis,'crypto',{value:secureOnly,configurable:true});
return Promise.resolve().then(run).finally(()=>{if(previous)Object.defineProperty(globalThis,'crypto',previous);else delete globalThis.crypto;});
}
test('old browser / LAN HTTP uses secure UUID v4 without native randomUUID',()=>{
const ids=new Set(Array.from({length:1000},()=>createRequestUuid(secureOnly)));
assert.equal(ids.size,1000);for(const id of ids)assert.match(id,uuid);
assert.equal(createRequestUuid({getRandomValues:bytes=>bytes.fill(0)}),'00000000-0000-4000-8000-000000000000');
});
test('native UUID retains method binding and missing secure randomness fails explicitly',()=>{
const provider={getRandomValues(){throw Error('fallback should not run');},randomUUID(){assert.equal(this,provider);return 'native-uuid';}};
assert.equal(createRequestUuid(provider),'native-uuid');
assert.throws(()=>createRequestUuid({}),/安全操作编号/);
});
test('capability detection downgrades old selector support while retaining modern styles',()=>{
assert.equal(needsLegacyComponentStyles({supports:()=>false}),true);
assert.equal(needsLegacyComponentStyles({supports:()=>{throw Error('unsupported selector query');}}),true);
assert.equal(needsLegacyComponentStyles({supports:query=>query==='selector(:where(*))'}),false);
});
test('actual multi-file intake closure accepts files when crypto.randomUUID is absent',async()=>withoutNativeUuid(async()=>{
const source=readFileSync(process.env.NO_CODE_WORKFLOW_SOURCE||new URL('../src/NoCodeDataWorkflow.tsx',import.meta.url),'utf8');
const closure=source.slice(source.indexOf(' const readFiles = async'),source.indexOf(' const changePreview = async'));
const prelude=`let drafts=[],error='',busy=false,reading=null;const importBusy=false,actionLock={current:false},input={current:{value:'selected'}};
const options={fields:{black:[{name:'产品'}]},scopes:['ALL']};
const setDrafts=value=>{drafts=typeof value==='function'?value(drafts):value},setError=value=>error=value,setBusy=value=>busy=value,setReading=value=>reading=value;
const onUnauthorized=()=>false,failure=e=>e.message,sourceBatchYear=()=>2025,sourceFilenamePeriod=()=>({year:2025,month:1}),matchFields=()=>({'产品':'产品'});
const api={previewSourceWorkbook:async file=>({sourceFile:file.name,preview:{sheetName:'sheet',headerRow:1,headers:['产品']}})};
const detectSourceWorkbook=async workbook=>({workbook,stage:'black'});\n`;
const compiled=ts.transpileModule(prelude+closure+'\nreturn {readFiles,state:()=>({drafts,error,busy,reading,locked:actionLock.current})};',{compilerOptions:{target:ts.ScriptTarget.ES2020}}).outputText;
const f=new Function('createRequestUuid','crypto',compiled)(createRequestUuid,secureOnly);
const files=Array.from({length:25},(_,i)=>({name:`2025年1月黑片-${i}.xlsx`,size:100+i,lastModified:1}));await f.readFiles(files);
const state=f.state();assert.equal(state.error,'');assert.equal(state.drafts.length,25);assert.equal(state.busy,false);assert.equal(state.reading,null);assert.equal(state.locked,false);
const ids=state.drafts.flatMap(d=>[d.id,...(d.fileId?[d.fileId]:[])]);for(const id of ids)assert.match(id,uuid);assert.equal(new Set(ids).size,ids.length);
}));
test('uncertain AI retries reuse the original secure idempotency key under LAN HTTP',async()=>withoutNativeUuid(()=>{
const input={sessionId:'session',question:'质量变化',context:{year:2025},generation:1};const first=beginAnalysisSubmission(null,input);assert.match(first.idempotencyKey,uuid);
first.uncertain=true;assert.equal(beginAnalysisSubmission(first,input).idempotencyKey,first.idempotencyKey);
}));