// Fake transport only: no browser, engine, provider or backend execution. const { test } = require('node:test') const assert = require('node:assert/strict') const { seedPagination, COUNT } = require('./pagination.cjs') const folder = 'archy-pagination-00000000-0000-4000-8000-000000000000' const tick = () => new Promise(resolve => setImmediate(resolve)) function transport(failAt = -1, failedStatus = 204) { const state = { calls: [], active: 0, peak: 0, failed: false, admittedAfterFailure: 0 } return { state, async fetch(path, options) { if (state.failed) state.admittedAfterFailure++ const index = state.calls.length state.calls.push({ path, options }) state.peak = Math.max(state.peak, ++state.active) await tick() return { status() { if (index === failAt) { state.failed = true; return failedStatus } return 201 }, async dispose() { if (index === failAt) await new Promise(resolve => setTimeout(resolve, 10)); else await tick(); state.active-- } } } } } test('251 unique creates, no overwrite/redirect, at most four active requests', async () => { const api = transport() const names = await seedPagination(api, folder) assert.equal(names.length, COUNT) assert.equal(api.state.calls.length, COUNT + 1) assert.equal(api.state.calls[0].options.method, 'MKCOL') assert.equal(new Set(api.state.calls.map(call => call.path)).size, COUNT + 1) assert.equal(api.state.peak, 4) assert.equal(api.state.active, 0) for (const { options } of api.state.calls) { assert.equal(options.maxRedirects, 0) assert.equal(options.maxRetries, 0) assert.equal(options.headers['If-None-Match'], '*') assert.ok(options.timeout > 0 && options.timeout <= 5000) } }) test('existing collection stops before any PUT', async () => { const api = transport(0, 405) await assert.rejects(seedPagination(api, folder), /never overwrite/) assert.equal(api.state.calls.length, 1) assert.equal(api.state.active, 0) }) test('first unexpected write status stops new admission before slow disposal', async () => { const api = transport(5) await assert.rejects(seedPagination(api, folder), /never overwrite/) assert.equal(api.state.admittedAfterFailure, 0) assert.equal(api.state.active, 0) }) test('invalid folder cannot issue a request', async () => { const api = transport() await assert.rejects(seedPagination(api, '../foreign')) assert.equal(api.state.calls.length, 0) })