Files
ihaleist/tests/stored-document.test.mjs
T

39 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {test} from 'node:test';
import assert from 'node:assert/strict';
import {retryDocumentExtraction} from '../src/services/storedDocument.js';
test('stored document retry uses owned file ID and stops before a changed session can publish',async()=>{
const originalFetch=globalThis.fetch;
const storage=Object.getOwnPropertyDescriptor(globalThis,'localStorage');
let token='account-a';
Object.defineProperty(globalThis,'localStorage',{configurable:true,value:{getItem:()=>token}});
const doc={id:'owned-doc',ad:'cetvel.docx',ikn:'2026/123',tur:'ihale',isleme:{status:'awaiting_text'}};
const calls=[];
try {
globalThis.fetch=async(url,options)=>{
calls.push({url,options});
if(url.includes('/text?')) return Response.json({success:true,metin:'Source table'});
return Response.json({success:true,sayi:2});
};
assert.equal((await retryDocumentExtraction(doc)).sayi,2);
const payload=JSON.parse(calls[1].options.body);
assert.equal(payload.documentId,doc.id);assert.equal(payload.ikn,doc.ikn);assert.equal(payload.metin,'Source table');
calls.length=0;
globalThis.fetch=async(url)=>{calls.push(url);token='account-b';return Response.json({success:true,metin:'old account document'});};
await assert.rejects(retryDocumentExtraction(doc),/Oturum değişti/);
assert.equal(calls.length,1);
token='account-a';calls.length=0;
globalThis.fetch=async(url,options)=>{
calls.push({url,options});
return url.includes('/text?') ? Response.json({success:false,error:'Okunamadı'},{status:422}) : Response.json({success:true});
};
await assert.rejects(retryDocumentExtraction(doc),/Okunamadı/);
assert.equal(calls[1].url,'/api/me/documents/read-error');
assert.deepEqual(JSON.parse(calls[1].options.body),{id:doc.id,expectedProcessing:doc.isleme});
assert.equal(calls.length,2);
} finally {
globalThis.fetch=originalFetch;
if(storage) Object.defineProperty(globalThis,'localStorage',storage);else delete globalThis.localStorage;
}
});