Files
order-notification-automation/tests/storage/storage-browse.test.cjs
T

30 lines
3.0 KiB
JavaScript

const {test}=require('node:test'),assert=require('node:assert/strict');
const {StorageService}=require('../../src/modules/storage/service.cjs');
test('a stalled connection check times out and releases pending state',async()=>{
const service=new StorageService({store:{prepareServer:()=>({saved:{host:'server',share:'orders'}})},client:{area:async()=>({list:()=>new Promise(()=>{})})},checkTimeoutMs:40});
await assert.rejects(service.checkConnection({}),e=>e.status===504);assert.equal(service.pendingChecks.size,0);await service.stop();
});
test('cancelled connection checks settle without saving',async()=>{
const controller=new AbortController(),service=new StorageService({store:{prepareServer:()=>({saved:{host:'server',share:'orders'}})},client:{area:async()=>({list:()=>new Promise(()=>{})})},checkTimeoutMs:100});
const promise=service.checkConnection({},{signal:controller.signal});controller.abort();await assert.rejects(promise,e=>e.status===499);assert.equal(service.pendingChecks.size,0);await service.stop();
});
test('shutdown aborts a pending read instead of waiting for the network',async()=>{
const service=new StorageService({store:{prepareServer:()=>({saved:{host:'server',share:'orders'}})},client:{area:async()=>({list:()=>new Promise(()=>{})})},checkTimeoutMs:10000});
const checked=assert.rejects(service.checkConnection({}),e=>e.status===503);await service.stop();await checked;assert.equal(service.pendingChecks.size,0);
});
test('failed Windows tree termination falls back and waits for worker closure',{skip:process.platform!=='win32'},async t=>{
const {EventEmitter}=require('node:events'),{PassThrough}=require('node:stream'),cp=require('node:child_process');let kills=0,closed=false;
const child=new EventEmitter();child.pid=123;child.stdin=new PassThrough();child.stdout=new PassThrough();child.stderr=new PassThrough();child.kill=()=>{kills++;queueMicrotask(()=>{closed=true;child.emit('close',1);});};
t.mock.method(cp,'spawn',(file)=>{if(file===process.execPath)return child;const killer=new EventEmitter();queueMicrotask(()=>killer.emit('exit',1));return killer;});
const key=require.resolve('../../src/modules/storage/smb-probe.cjs');delete require.cache[key];t.after(()=>delete require.cache[key]);const {probe}=require(key);
await assert.rejects(probe({host:'server'},{timeoutMs:5}),e=>e.status===504);assert.equal(kills,1);assert.equal(closed,true);
});
test('share enumeration reserves enough bounded output for hundreds of Unicode names',async t=>{
const cp=require('node:child_process'),names=Array.from({length:501},(_,i)=>'共享目录'.repeat(10)+i),payload=JSON.stringify({ok:true,shares:names});
t.mock.method(cp,'execFile',(file,args,options,callback)=>{assert.ok(options.maxBuffer>=Buffer.byteLength(payload));return {stdin:{on(){},end(){callback(null,payload);}}};});
const key=require.resolve('../../src/modules/storage/smb-native.cjs');delete require.cache[key];t.after(()=>delete require.cache[key]);const {listShares}=require(key);assert.deepEqual(await listShares({host:'server',authMode:'windows'}),names);
});