/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import assert from 'node:assert/strict'; import { setImmediate } from 'node:timers/promises'; import test from 'node:test'; import axios from 'axios'; import { installRequestLifecycle } from '../../src/helpers/requestLifecycle.js'; function fixture() { const pending = new Map(); const errors = []; let expirations = 0; const client = axios.create({ adapter: (config) => new Promise((resolve, reject) => { const abort = () => reject(new axios.CanceledError(undefined, config)); config.signal?.addEventListener('abort', abort, { once: true }); pending.set(config.url, (status = 200) => { config.signal?.removeEventListener('abort', abort); const response = { status, config, data: {} }; if (status < 400) resolve(response); else reject( new axios.AxiosError( `HTTP ${status}`, 'HTTP_ERROR', config, null, response, ), ); }); }), }); const retire = installRequestLifecycle(client, { onError: (error) => errors.push(error.response?.status), onSessionExpired: () => expirations++, }); return { client, pending, errors, retire, expirations: () => expirations }; } test('healthy sessions dispatch independent reads concurrently', async () => { const { client, pending } = fixture(); const paths = ['/api/org/list', '/api/org/info', '/api/org/project']; const responses = Promise.all(paths.map((path) => client.get(path))); await setImmediate(); assert.equal(pending.size, paths.length); for (const path of paths) pending.get(path)(); assert.deepEqual( (await responses).map((response) => response.status), [200, 200, 200], ); }); test('the first 401 cancels session reads and blocks new ones without canceling public reads or dispatched writes', async () => { const { client, pending, errors, expirations } = fixture(); const outcomes = Promise.allSettled([ client.get('/api/org/info'), client.get('/api/org/list'), client.get('/api/status'), client.post('/api/org/write', {}), ]); await setImmediate(); pending.get('/api/org/info')(401); pending.get('/api/org/list')(401); await setImmediate(); await assert.rejects(client.get('/api/org/late'), axios.isCancel); assert.equal(pending.has('/api/org/late'), false); pending.get('/api/status')(); pending.get('/api/org/write')(); const [first, sibling, publicRead, write] = await outcomes; assert.equal(first.reason.response.status, 401); assert.equal(axios.isCancel(sibling.reason), true); assert.equal(publicRead.status, 'fulfilled'); assert.equal(write.status, 'fulfilled'); assert.equal(expirations(), 1); assert.deepEqual(errors, []); }); test('a 500 cancels its explicit group only, preserving standalone reads', async () => { const { client, pending, errors, expirations } = fixture(); const requestGroup = new AbortController(); const outcomes = Promise.allSettled([ client.get('/api/org/budget', { requestGroup }), client.get('/api/org/dept', { requestGroup }), client.get('/api/org/unrelated'), ]); await setImmediate(); pending.get('/api/org/budget')(500); await setImmediate(); pending.get('/api/org/unrelated')(); const [first, sibling, other] = await outcomes; assert.equal(first.reason.response.status, 500); assert.equal(axios.isCancel(sibling.reason), true); assert.equal(other.status, 'fulfilled'); assert.equal(expirations(), 0); assert.deepEqual(errors, [500]); }); test('caller cancellations, public 401 and locally handled 401 do not invalidate the session', async () => { const { client, pending, errors, expirations } = fixture(); const controller = new AbortController(); const outcomes = Promise.allSettled([ client.get('/api/org/canceled', { signal: controller.signal }), client.get('/api/user/self', { skipErrorHandler: true }), client.post('/api/org/login/direct', {}), client.get('/api/org/list'), ]); await setImmediate(); controller.abort(); pending.get('/api/user/self')(401); pending.get('/api/org/login/direct')(401); await setImmediate(); pending.get('/api/org/list')(); const [canceled, local, login, read] = await outcomes; assert.equal(axios.isCancel(canceled.reason), true); assert.equal(local.reason.response.status, 401); assert.equal(login.reason.response.status, 401); assert.equal(read.status, 'fulfilled'); assert.equal(expirations(), 0); assert.deepEqual(errors, [401]); }); test('retiring an instance cancels stale reads without affecting a new instance', async () => { const old = fixture(); const current = fixture(); const outcomes = Promise.allSettled([ old.client.get('/api/org/list'), current.client.get('/api/org/list'), ]); await setImmediate(); old.retire(); current.pending.get('/api/org/list')(); const [stale, active] = await outcomes; assert.equal(axios.isCancel(stale.reason), true); assert.equal(active.status, 'fulfilled'); assert.equal(old.expirations(), 0); });