2026-10-20 10:04:17 +08:00
import test from 'node:test' ;
import assert from 'node:assert/strict' ;
import { readFile } from 'node:fs/promises' ;
import { createRequire } from 'node:module' ;
import vm from 'node:vm' ;
2026-10-28 17:30:35 +08:00
import * as review from '../src/core/draft-review.ts' ;
2026-10-20 10:04:17 +08:00
import * as helpers from '../src/core/assistant-display.ts' ;
const require = createRequire ( import . meta . url ) , React = require ( 'react' ) , { renderToStaticMarkup } = require ( 'react-dom/server' ) , ts = require ( 'typescript' ) ;
test ( 'cancelled, completed and parked drafts cannot become active confirmations' , ( ) => {
for ( const status of [ 'CANCELLED' , 'SUCCEEDED' , 'PAUSED' ] ) assert . equal ( helpers . isActiveDraft ( { id : 1 , status } , 1 ) , false ) ;
assert . equal ( helpers . draftStatus ( 'PAUSED' ) , '已暂存' ) ;
assert . equal ( helpers . isActiveDraft ( { id : 1 , status : 'WAIT_CONFIRM' } , '1' ) , true ) ;
assert . equal ( helpers . isActiveDraft ( { id : 1 , status : 'WAIT_CONFIRM' , readOnly : true } , 1 ) , false ) ;
assert . equal ( helpers . isActiveDraft ( { id : 2 , status : 'WAIT_CONFIRM' } , 1 ) , false ) ;
assert . equal ( helpers . isActiveDraft ( { } , undefined ) , false ) ;
} ) ;
test ( 'inventory prioritizes model and quantity; technical, empty and overflow values are recoverable' , ( ) => {
const fields = [ 'version' , 'projectName' , 'id' , 'productName' , 'spec' , 'brand' , 'stockNum' , 'unit' , 'areaName' , 'remark' , 'averageUnitCost' ] . map ( key => ( { key , label : key } ) ) ;
const rows = [ { id : 1 , version : 0 , productName : '路由器' , spec : 'AX5400' , brand : null , stockNum : 0 , unit : '个' , areaName : 'A' , projectName : '项目' , averageUnitCost : 0 } ] ;
const result = helpers . queryColumnGroups ( fields , rows ) ;
assert . deepEqual ( result . primary . map ( c => c . key ) , [ 'productName' , 'spec' , 'stockNum' , 'unit' , 'areaName' , 'projectName' , 'averageUnitCost' ] ) ;
assert . equal ( result . primary . length + result . details . length , fields . length ) ;
assert . ok ( result . details . some ( c => c . key === 'version' ) ) ;
} ) ;
// Render real TSX with strict native View doubles. An empty string is a text node,
// even though a browser may visually ignore it; Android cannot render it in View.
async function renderAssistant ( session = null ) {
const source = await readFile ( new URL ( '../src/screens/Assistant.tsx' , import . meta . url ) , 'utf8' ) ;
const compiled = ts . transpileModule ( source , { compilerOptions : { module : ts . ModuleKind . CommonJS , jsx : ts . JsxEmit . ReactJSX , esModuleInterop : true } } ) . outputText ;
const View = ( { children } ) => { for ( const value of React . Children . toArray ( children ) ) assert . ok ( typeof value !== 'string' && typeof value !== 'number' , 'View contains bare text' ) ; return React . createElement ( 'div' , null , children ) ; } ;
const Text = ( { children } ) => React . createElement ( 'span' , null , children ) ;
let calls = 0 ; const mockReact = { ... React , useState : initial => React . useState ( calls ++ === 0 ? session : initial ) } ;
const module = { exports : { } } ;
2026-10-28 17:30:35 +08:00
const imports = { '../typography' : { Text , TextInput : ( ) => null } , '../core/draft-review' : review , react : mockReact , 'react/jsx-runtime' : require ( 'react/jsx-runtime' ) , 'react-native' : { View , Text , Pressable : View , ScrollView : View , KeyboardAvoidingView : View , ActivityIndicator : ( ) => null , Image : ( ) => null , Platform : { OS : 'web' } } , '../dialogs' : { Dialog : { alert ( ) { } } } , 'expo-document-picker' : { } , '../ui' : { useTheme : ( ) => ( { text : '#000' , muted : '#666' , line : '#ddd' } ) , s : { } , Button : ( { title } ) => React . createElement ( Text , null , title ) , Card : View , Heading : Text , Note : Text , Field : ( ) => null , Icon : ( ) => null } , '../files' : { ProtectedImage : ( ) => null } , '../core/modules' : { display : String } , '../core/assistant-display' : helpers } ;
2026-10-20 10:04:17 +08:00
vm . runInNewContext ( compiled , { module , exports : module . exports , require : name => name . endsWith ( '.png' ) ? 'asset' : name in imports ? imports [ name ] : require ( name ) , setInterval , clearInterval } ) ;
return renderToStaticMarkup ( React . createElement ( module . exports . Assistant , { api : { } } ) ) ;
}
test ( 'initial empty session has no raw empty text under native View' , async ( ) => { assert . match ( await renderAssistant ( ) , /不知道从哪里开始/ ) ; } ) ;
test ( 'historical draft details are initially collapsed; current draft stays fully visible' , async ( ) => {
const draft = { id : 1 , label : '采购' , status : 'CANCELLED' , rows : [ { name : 'HISTORICAL-DETAIL' } ] , fields : [ { key : 'name' , label : '物品' } ] } ;
assert . ok ( ! ( await renderAssistant ( { messages : [ { role : 'assistant' , content : '旧草稿' , draft } ] } ) ) . includes ( 'HISTORICAL-DETAIL' ) ) ;
const active = { ... draft , id : 2 , status : 'WAIT_CONFIRM' , rows : [ { name : 'ACTIVE-DETAIL' } ] , ready : false } ;
assert . match ( await renderAssistant ( { messages : [ { role : 'assistant' , content : '当前草稿' , draft : active } ] , draft : active } ) , /ACTIVE-DETAIL/ ) ;
} ) ;
test ( 'preview confirmation portal is not mounted beneath later business dialogs' , async ( ) => {
const source = await readFile ( new URL ( '../src/dialogs.tsx' , import . meta . url ) , 'utf8' ) ;
assert . match ( source , /if\(Platform\.OS!=='web'\|\|!prompt\)return null/ ) ;
} ) ;
test ( 'move form shows availability, has fill-all action, and keeps overstock validation' , async ( ) => {
2026-10-28 17:30:35 +08:00
const form = await readFile ( new URL ( '../src/screens/BatchOperation.tsx' , import . meta . url ) , 'utf8' ) ;
assert . match ( form , /全部填入可用数量/ ) ; assert . match ( form , /available\(kind,row\)/ ) ;
const core = await readFile ( new URL ( '../src/core/stock-operations.ts' , import . meta . url ) , 'utf8' ) ;
assert . match ( core , /quantity>available\(kind,row\)/ ) ; assert . match ( core , /当前可用数量/ ) ;
2026-10-20 10:04:17 +08:00
} ) ;
test ( 'expanding history and more fields does not force readers back to the latest message' , async ( ) => {
const source = await readFile ( new URL ( '../src/screens/Assistant.tsx' , import . meta . url ) , 'utf8' ) ;
assert . match ( source , /inspectHistory=\(\)=>\{followLatest.current=false/ ) ;
assert . match ( source , /if\(followLatest.current\)scroll.current\?\.scrollToEnd/ ) ;
assert . match ( source , /onInspect\(\);setHistoryOpen/ ) ; assert . match ( source , /onInspect\(\);setMore/ ) ;
} ) ;