import test from 'node:test'; import assert from 'node:assert/strict'; import { apiKeyRequest } from '../src/services/apiKeys.js'; test('API key client distinguishes failure, malformed data, and a genuinely empty list', async () => { const original = globalThis.fetch; try { globalThis.fetch = async () => new Response(JSON.stringify({success:true,anahtarlar:[],limit:0})); assert.deepEqual((await apiKeyRequest()).anahtarlar, []); globalThis.fetch = async () => new Response(JSON.stringify({success:false,error:'Depo erişilemiyor'}),{status:503}); await assert.rejects(apiKeyRequest(), /Depo erişilemiyor/); globalThis.fetch = async () => new Response(JSON.stringify({success:true,anahtarlar:null})); await assert.rejects(apiKeyRequest(), /eksik veya geçersiz/); globalThis.fetch = async () => new Response('502'); await assert.rejects(apiKeyRequest(), /geçerli anahtar bilgisi/); } finally { globalThis.fetch = original; } }); test('API key client rejects late private responses after session change and does not retry uncertain creation', async () => { const original = globalThis.fetch, storage = globalThis.localStorage; let token = 'account-a', calls = 0; globalThis.localStorage = {getItem:()=>token}; try { globalThis.fetch = async () => { token = 'account-b'; return new Response(JSON.stringify({success:true,anahtar:{id:'qa',anahtar:'ih_live_qa'}})); }; await assert.rejects(apiKeyRequest('POST',{ad:'QA'}), /Oturum değişti/); globalThis.fetch = async (_url, options) => { calls++; return new Promise((_resolve,reject)=>options.signal.addEventListener('abort',()=>reject(options.signal.reason),{once:true})); }; await assert.rejects(apiKeyRequest('POST',{ad:'QA',timeoutMs:5}), /sunucuda gerçekleşmiş olabilir/); assert.equal(calls,1); } finally { globalThis.fetch=original; globalThis.localStorage=storage; } });