import { KVStorageHttp } from '../../src/kv-storage/kv-storage-http.js' import { KVStorageHttpServer, type KVStorageHttpServerOptions } from '../../src/kv-storage/kv-storage-http-server.js' import { KVStorageInMemory } from '../../src/kv-storage/kv-storage-in-memory.js' import type { RequestSigner } from '../../src/utils/request-auth.js' import { KVStorageCounting } from '../helpers.js' /** * An in-process server with a network switch. While `online` is false, new requests fail like a * dropped connection (open SSE streams stay up, so pushes can still be simulated). */ export class TestNetwork { online = true readonly store = new KVStorageCounting(new KVStorageInMemory()) readonly server: KVStorageHttpServer private readonly clients: KVStorageHttp[] = [] constructor(options: Partial = {}) { this.server = new KVStorageHttpServer(this.store, { auth: 'none', ...options }) } client(signer?: RequestSigner): KVStorageHttp { const client = new KVStorageHttp({ url: 'http://server', signer, fetch: async (url, init) => { if (!this.online) throw new TypeError('fetch failed') return this.server.handle(new Request(url, init)) }, }) this.clients.push(client) return client } async close(): Promise { await Promise.all(this.clients.map((c) => c.close())) } }