Files
ihaleist/tests/account-request.test.mjs
T

52 lines
3.1 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 {accountRequest} from '../src/services/accountRequest.js';
import {getAllUserData,listDocuments,getTenderStates} from '../src/services/account.js';
test('account requests preserve conflict codes, distinguish malformed lists and reject HTTP errors',async()=>{
const original=globalThis.fetch;
try {
globalThis.fetch=async()=>Response.json({success:true});
for(const call of [listDocuments,getAllUserData,getTenderStates])await assert.rejects(call(),/eksik veya geçersiz/);
globalThis.fetch=async()=>Response.json({success:false,error:'Conflict',kod:'VERSION_CONFLICT'},{status:409});
await assert.rejects(accountRequest('/state'),{kod:'VERSION_CONFLICT'});
globalThis.fetch=async()=>Response.json({success:true},{status:503});
await assert.rejects(accountRequest('/state'),/503/);
globalThis.fetch=async()=>Response.json({success:true,belgeler:[]});
assert.deepEqual((await listDocuments()).belgeler,[]);
} finally {globalThis.fetch=original;}
});
test('stalled response bodies time out and uncertain writes are not retried',async()=>{
const original=globalThis.fetch;let calls=0;
try {
globalThis.fetch=async()=>{calls++;return {ok:true,json:()=>new Promise(()=>{})};};
await assert.rejects(accountRequest('/state',{method:'PUT',timeoutMs:5}),/sunucuda gerçekleşmiş olabilir/);
assert.equal(calls,1);
await assert.rejects(accountRequest('/state',{timeoutMs:5}),/Bilgiler zamanında/);
} finally {globalThis.fetch=original;}
});
test('late private account responses and explicit cancellation do not reach the caller',async()=>{
const original=globalThis.fetch,storage=Object.getOwnPropertyDescriptor(globalThis,'localStorage');
let token='first';Object.defineProperty(globalThis,'localStorage',{configurable:true,value:{getItem:()=>token}});
try {
globalThis.fetch=async()=>({ok:true,json:async()=>{token='second';return {success:true,veri:{private:'old account'}};}});
await assert.rejects(getAllUserData(),/Oturum değişti/);
const controller=new AbortController();controller.abort();
globalThis.fetch=async()=>{throw new Error('must not be called');};
await assert.rejects(accountRequest('/state',{signal:controller.signal}),{name:'AbortError'});
} finally {globalThis.fetch=original;if(storage)Object.defineProperty(globalThis,'localStorage',storage);else delete globalThis.localStorage;}
});
test('Telegram test does not report success without a provider receipt or retry an ambiguous response',async()=>{
const {telegramTest}=await import('../src/services/account.js');
const original=globalThis.fetch;let calls=0;
try{
globalThis.fetch=async()=>{calls++;return Response.json({success:true});};
await assert.rejects(telegramTest(),/kabulü doğrulanamadı/);assert.equal(calls,1);
globalThis.fetch=async()=>Response.json({success:true,id:'123:42'});
assert.equal((await telegramTest()).id,'123:42');
globalThis.fetch=async()=>Response.json({success:false,error:'Telegram hizmeti etkin değil.'},{status:503});
await assert.rejects(telegramTest(),/etkin değil/);
}finally{globalThis.fetch=original;}
});