Formatting

This commit is contained in:
2026-08-03 03:35:09 +00:00
parent 1cee27e78b
commit 161b56c756
3 changed files with 219 additions and 245 deletions
+59 -68
View File
@@ -1,76 +1,67 @@
import { describe, expect, it, vi } from "vitest";
import { describe, expect, it, vi } from 'vitest';
import type { RouteDefinition, RouteModule } from "../source/routes/types.js";
import { ApplicationRouter } from "../source/services/router.js";
import { Broadcaster } from "../source/services/broadcaster.js";
import { Logger } from "../source/utils/logger.js";
import { TestConnection } from "./helpers/test-connection.js";
import type { RouteDefinition, RouteModule } from '../source/routes/types.ts';
import { ApplicationRouter } from '../source/services/router.ts';
import { Broadcaster } from '../source/services/broadcaster.ts';
import { Logger } from '../source/utils/logger.ts';
import { TestConnection } from './helpers/test-connection.ts';
function moduleWith(routes: RouteDefinition[]): RouteModule {
return {
async getRoutes() {
return routes;
},
};
}
async function expectPending(promise: Promise<void>): Promise<void> {
const settled = vi.fn();
void promise.then(settled);
await Promise.resolve();
expect(settled).not.toHaveBeenCalled();
}
describe("long-lived subscription dispatch", () => {
it("allows duplicate subscribe and unsubscribe requests while the original request waits", async () => {
const broadcaster = new Broadcaster(new Logger("subscription-flow-test"));
const router = await ApplicationRouter.create([
moduleWith([
{
url: "/items/subscribe",
handler: async (stream) => {
await broadcaster.subscribe(stream, ["items"]);
},
const moduleWith = (routes: RouteDefinition[]): RouteModule => {
return {
async getRoutes(): Promise<RouteDefinition[]> {
return routes;
},
{
url: "/items/unsubscribe",
handler: async (stream) => {
await broadcaster.unsubscribe(stream, ["items"]);
await stream.send({});
},
},
]),
]);
const connection = new TestConnection(true, true);
};
};
const original = router.dispatch(
{ path: "/items/subscribe", requestId: "subscribe-1" },
connection,
);
await vi.waitFor(() => expect(connection.closeCallbacks).toHaveLength(1));
await expectPending(original);
const expectPending = async (promise: Promise<void>): Promise<void> => {
const settled = vi.fn();
void promise.then(settled);
await Promise.resolve();
expect(settled).not.toHaveBeenCalled();
};
// This request uses a different ApplicationRouteStream over the same
// connection. Since the topic already exists, its dispatch completes.
await router.dispatch(
{ path: "/items/subscribe", requestId: "subscribe-2" },
connection,
);
await expectPending(original);
describe('long-lived subscription dispatch', (): void => {
it('allows duplicate subscribe and unsubscribe requests while the original request waits', async (): Promise<void> => {
const broadcaster = new Broadcaster(new Logger('subscription-flow-test'));
const router = await ApplicationRouter.create([
moduleWith([
{
url: '/items/subscribe',
handler: async (stream): Promise<void> => {
await broadcaster.subscribe(stream, [ 'items' ]);
},
},
{
url: '/items/unsubscribe',
handler: async (stream): Promise<void> => {
await broadcaster.unsubscribe(stream, [ 'items' ]);
await stream.send({});
},
},
]),
]);
const connection = new TestConnection(true, true);
await router.dispatch(
{ path: "/items/unsubscribe", requestId: "unsubscribe-1" },
connection,
);
await original;
const original = router.dispatch({ path: '/items/subscribe', requestId: 'subscribe-1' }, connection);
await vi.waitFor(() => expect(connection.closeCallbacks).toHaveLength(1));
await expectPending(original);
expect(connection.messages).toEqual([
{
id: "unsubscribe-1",
type: "response",
statusCode: 200,
body: {},
},
]);
});
// This request uses a different ApplicationRouteStream over the same
// connection. Since the topic already exists, its dispatch completes.
await router.dispatch({ path: '/items/subscribe', requestId: 'subscribe-2' }, connection);
await expectPending(original);
await router.dispatch({ path: '/items/unsubscribe', requestId: 'unsubscribe-1' }, connection);
await original;
expect(connection.messages).toEqual([
{
id: 'unsubscribe-1',
type: 'response',
statusCode: 200,
body: {},
},
]);
});
});