Formatting

This commit is contained in:
2026-08-03 03:40:57 +00:00
parent 97d4422b8f
commit c9845d0f28
6 changed files with 214 additions and 246 deletions
+49 -53
View File
@@ -1,62 +1,58 @@
import { describe, expect, it, vi } from "vitest";
import { describe, expect, it, vi } from 'vitest';
import { HonoSSEStream } from "../../../source/services/stream/hono-sse-stream.js";
import { HttpRequestStream } from "../../../source/services/stream/http-request-stream.js";
import { WSStream } from "../../../source/services/stream/ws-stream.js";
import { HonoSSEStream } from '../../../source/services/stream/hono-sse-stream.ts';
import { HttpRequestStream } from '../../../source/services/stream/http-request-stream.ts';
import { WSStream } from '../../../source/services/stream/ws-stream.ts';
describe("stream lifecycle observers", () => {
it("buffers exactly one normal HTTP response", async () => {
const stream = new HttpRequestStream();
describe('stream lifecycle observers', (): void => {
it('buffers exactly one normal HTTP response', async (): Promise<void> => {
const stream = new HttpRequestStream();
await stream.send({
type: "response",
statusCode: 200,
body: { ok: true },
await stream.send({
type: 'response',
statusCode: 200,
body: { ok: true },
});
expect(stream.getResponse()).toEqual({
type: 'response',
statusCode: 200,
body: { ok: true },
});
await expect(stream.send({
type: 'response',
statusCode: 200,
body: { second: true },
})).rejects.toThrow('only send one response');
});
expect(stream.getResponse()).toEqual({
type: "response",
statusCode: 200,
body: { ok: true },
it('notifies WebSocket observers registered after remote closure', (): void => {
const stream = new WSStream({
send: vi.fn(),
close: vi.fn(),
readyState: 1,
});
const onClose = vi.fn();
stream.markClosed();
stream.onClose(onClose);
expect(onClose).toHaveBeenCalledOnce();
});
await expect(
stream.send({
type: "response",
statusCode: 200,
body: { second: true },
}),
).rejects.toThrow("only send one response");
});
it("notifies WebSocket observers registered after remote closure", () => {
const stream = new WSStream({
send: vi.fn(),
close: vi.fn(),
readyState: 1,
it('notifies SSE observers registered after local closure', async (): Promise<void> => {
const streamApi = {
writeSSE: vi.fn(),
close: vi.fn(),
};
const stream = new HonoSSEStream(streamApi as unknown as ConstructorParameters<typeof HonoSSEStream>[0]);
const onClose = vi.fn();
await stream.close();
stream.onClose(onClose);
await stream.close();
expect(streamApi.close).toHaveBeenCalledOnce();
expect(onClose).toHaveBeenCalledOnce();
});
const onClose = vi.fn();
stream.markClosed();
stream.onClose(onClose);
expect(onClose).toHaveBeenCalledOnce();
});
it("notifies SSE observers registered after local closure", () => {
const streamApi = {
writeSSE: vi.fn(),
close: vi.fn(),
};
const stream = new HonoSSEStream(
streamApi as unknown as ConstructorParameters<typeof HonoSSEStream>[0],
);
const onClose = vi.fn();
stream.close();
stream.onClose(onClose);
stream.close();
expect(streamApi.close).toHaveBeenCalledOnce();
expect(onClose).toHaveBeenCalledOnce();
});
});
+37 -47
View File
@@ -1,58 +1,48 @@
import { describe, expect, it } from "vitest";
import { z } from "zod";
import { WebSocketServer } from "ws";
import { describe, expect, it } from 'vitest';
import { z } from 'zod';
import type { WebSocketServer } from 'ws';
import { ApplicationRouter } from "../../../source/services/router.js";
import {
WsTransportRouter,
} from "../../../source/services/transport/ws-transport.js";
import { Logger } from "../../../source/utils/logger.js";
import { toExtendedJson } from "@xo-cash/utils";
import { ApplicationRouter } from '../../../source/services/router.ts';
import { WsTransportRouter } from '../../../source/services/transport/ws-transport.ts';
import { Logger } from '../../../source/utils/logger.ts';
import { toExtendedJson } from '@xo-cash/utils';
describe("WebSocket request decoding", () => {
it("decodes the minimal route-agnostic envelope and Extended JSON body", async () => {
await expect(
WsTransportRouter.decodeWebSocketRequest(
toExtendedJson({
id: "request-1",
path: "/data/write",
body: { value: new Uint8Array([1, 2, 3]) },
}),
),
).resolves.toEqual({
requestId: "request-1",
path: "/data/write",
body: { value: new Uint8Array([1, 2, 3]) },
describe('WebSocket request decoding', (): void => {
it('decodes the minimal route-agnostic envelope and Extended JSON body', async (): Promise<void> => {
await expect(WsTransportRouter.decodeWebSocketRequest(toExtendedJson({
id: 'request-1',
path: '/data/write',
body: { value: new Uint8Array([ 1, 2, 3 ]) },
}))).resolves.toEqual({
requestId: 'request-1',
path: '/data/write',
body: { value: new Uint8Array([ 1, 2, 3 ]) },
});
});
});
it.each([
"{}",
'{"path":42}',
'{"path":"/data/get","id":1}',
'{"path":"/data/get","method":"POST"}',
])("rejects an invalid envelope: %s", async (payload) => {
await expect(WsTransportRouter.decodeWebSocketRequest(payload)).rejects.toBeInstanceOf(
z.ZodError,
it.each([ '{}', '{"path":42}', '{"path":"/data/get","id":1}', '{"path":"/data/get","method":"POST"}' ])(
'rejects an invalid envelope: %s',
async (payload) => {
await expect(WsTransportRouter.decodeWebSocketRequest(payload)).rejects.toBeInstanceOf(z.ZodError);
},
);
});
it("rejects malformed JSON", async () => {
await expect(WsTransportRouter.decodeWebSocketRequest("{")).rejects.toMatchObject({
statusCode: 400,
message: "Invalid JSON in WebSocket message",
it('rejects malformed JSON', async (): Promise<void> => {
await expect(WsTransportRouter.decodeWebSocketRequest('{')).rejects.toMatchObject({
statusCode: 400,
message: 'Invalid JSON in WebSocket message',
});
});
});
});
describe("WsTransportRouter payload limits", () => {
it("configures Hono's ws server with the requested maxPayload", async () => {
const debug = new Logger("ws-transport-test");
const router = await ApplicationRouter.create([]);
const transport = new WsTransportRouter(router, debug, 1024);
const wsServer = transport.websocketServer as unknown as WebSocketServer;
describe('WsTransportRouter payload limits', (): void => {
it("configures Hono's ws server with the requested maxPayload", async () => {
const debug = new Logger('ws-transport-test');
const router = await ApplicationRouter.create([]);
const transport = new WsTransportRouter(router, debug, 1024);
const wsServer = transport.websocketServer as unknown as WebSocketServer;
expect(wsServer.options.maxPayload).toBe(1024);
await transport.stop();
});
expect(wsServer.options.maxPayload).toBe(1024);
await transport.stop();
});
});