|
| 1 | +import * as net from 'net'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { DestroyableServer, makeDestroyable } from 'destroyable-server'; |
| 4 | + |
| 5 | +import { createServer } from '../src/server.js'; |
| 6 | + |
| 7 | +describe("Stream-bytes endpoint", () => { |
| 8 | + |
| 9 | + let server: DestroyableServer; |
| 10 | + let serverPort: number; |
| 11 | + |
| 12 | + beforeEach(async () => { |
| 13 | + server = makeDestroyable(await createServer()); |
| 14 | + await new Promise<void>((resolve) => server.listen(resolve)); |
| 15 | + serverPort = (server.address() as net.AddressInfo).port; |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(async () => { |
| 19 | + await server.destroy(); |
| 20 | + }); |
| 21 | + |
| 22 | + it("streams the specified number of bytes", async () => { |
| 23 | + const response = await fetch(`http://localhost:${serverPort}/stream-bytes/512`); |
| 24 | + expect(response.status).to.equal(200); |
| 25 | + expect(response.headers.get('content-type')).to.equal('application/octet-stream'); |
| 26 | + const body = await response.arrayBuffer(); |
| 27 | + expect(body.byteLength).to.equal(512); |
| 28 | + }); |
| 29 | + |
| 30 | + it("returns zero bytes for /stream-bytes/0", async () => { |
| 31 | + const response = await fetch(`http://localhost:${serverPort}/stream-bytes/0`); |
| 32 | + expect(response.status).to.equal(200); |
| 33 | + const body = await response.arrayBuffer(); |
| 34 | + expect(body.byteLength).to.equal(0); |
| 35 | + }); |
| 36 | + |
| 37 | + it("returns deterministic output with seed", async () => { |
| 38 | + const r1 = await fetch(`http://localhost:${serverPort}/stream-bytes/128?seed=abc`); |
| 39 | + const r2 = await fetch(`http://localhost:${serverPort}/stream-bytes/128?seed=abc`); |
| 40 | + const b1 = Buffer.from(await r1.arrayBuffer()); |
| 41 | + const b2 = Buffer.from(await r2.arrayBuffer()); |
| 42 | + expect(b1.equals(b2)).to.be.true; |
| 43 | + }); |
| 44 | + |
| 45 | + it("respects custom chunk_size", async () => { |
| 46 | + // Just verify it works — we can't easily inspect chunking from fetch, |
| 47 | + // but we can confirm the total byte count is correct |
| 48 | + const response = await fetch( |
| 49 | + `http://localhost:${serverPort}/stream-bytes/100?chunk_size=10` |
| 50 | + ); |
| 51 | + expect(response.status).to.equal(200); |
| 52 | + const body = await response.arrayBuffer(); |
| 53 | + expect(body.byteLength).to.equal(100); |
| 54 | + }); |
| 55 | + |
| 56 | + it("rejects byte counts over the maximum", async () => { |
| 57 | + const response = await fetch(`http://localhost:${serverPort}/stream-bytes/200000`); |
| 58 | + expect(response.status).to.equal(400); |
| 59 | + }); |
| 60 | +}); |
0 commit comments