mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-23 23:46:35 +00:00
b64da26651
Adds an in-app assistant that reads and plans the open trip. Users connect their own provider (Sign in with ChatGPT, or an Anthropic, OpenAI, Gemini or OpenRouter key) or use the one the admin set up, including a custom OpenAI-compatible server. It is an addon and only shows while a connection is usable. The chat runs on the existing MCP tools, pinned to the trip. Every change is held as a change card the user applies or discards, and applied changes can be undone. On the desktop it is a floating window that can be moved and resized; on the phone it is fullscreen. Search results show as place cards that can go onto a day, weather lookups as a strip, proposed places as ghost markers on the map with a before/after switch, and an answer can be shared into the trip chat. Booking files can be dropped in and are read into change cards.
116 lines
3.4 KiB
TypeScript
116 lines
3.4 KiB
TypeScript
/**
|
|
* The shared event-stream helpers: headers before the first byte (so the
|
|
* compression middleware leaves the stream alone), writes dropped once the
|
|
* stream ended, a keepalive while it is open, and the client's leaving told
|
|
* apart from the response's own end.
|
|
*/
|
|
import { armKeepalive, onClientGone, openSseStream } from '../../../src/nest/common/sse-stream';
|
|
|
|
import type { Response } from 'express';
|
|
import { EventEmitter } from 'node:events';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
class FakeResponse extends EventEmitter {
|
|
writableEnded = false;
|
|
destroyed = false;
|
|
statusCode = 0;
|
|
readonly written: string[] = [];
|
|
readonly headers: Record<string, string> = {};
|
|
flushed = false;
|
|
|
|
status(code: number): this {
|
|
this.statusCode = code;
|
|
return this;
|
|
}
|
|
|
|
setHeader(name: string, value: string): void {
|
|
this.headers[name] = value;
|
|
}
|
|
|
|
flushHeaders(): void {
|
|
this.flushed = true;
|
|
}
|
|
|
|
write(chunk: string): boolean {
|
|
this.written.push(chunk);
|
|
return true;
|
|
}
|
|
|
|
end(): void {
|
|
this.writableEnded = true;
|
|
}
|
|
}
|
|
|
|
const asResponse = (res: FakeResponse) => res as unknown as Response;
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
describe('openSseStream', () => {
|
|
it('SSE-001: sends the headers at once, then events and comments, and nothing after the end', () => {
|
|
const res = new FakeResponse();
|
|
const stream = openSseStream(asResponse(res));
|
|
expect(res.statusCode).toBe(200);
|
|
expect(res.flushed).toBe(true);
|
|
expect(res.headers).toMatchObject({
|
|
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
'Cache-Control': 'no-cache, no-transform',
|
|
'X-Accel-Buffering': 'no',
|
|
});
|
|
stream.send('{"a":1}');
|
|
stream.comment('keepalive');
|
|
expect(stream.open).toBe(true);
|
|
stream.end();
|
|
stream.send('late');
|
|
stream.end();
|
|
expect(stream.open).toBe(false);
|
|
expect(res.written).toEqual(['data: {"a":1}\n\n', ': keepalive\n\n']);
|
|
});
|
|
|
|
it('SSE-002: a destroyed socket takes no more writes', () => {
|
|
const res = new FakeResponse();
|
|
const stream = openSseStream(asResponse(res));
|
|
res.destroyed = true;
|
|
stream.send('x');
|
|
expect(res.written).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe('armKeepalive', () => {
|
|
it('SSE-010: comments while open, stops on its own once the stream ended, or when told', () => {
|
|
vi.useFakeTimers();
|
|
const res = new FakeResponse();
|
|
const stream = openSseStream(asResponse(res));
|
|
armKeepalive(stream, 1_000);
|
|
vi.advanceTimersByTime(2_500);
|
|
expect(res.written).toEqual([': keepalive\n\n', ': keepalive\n\n']);
|
|
stream.end();
|
|
vi.advanceTimersByTime(5_000);
|
|
expect(res.written).toHaveLength(2);
|
|
|
|
const other = new FakeResponse();
|
|
const stop = armKeepalive(openSseStream(asResponse(other)), 1_000);
|
|
stop();
|
|
vi.advanceTimersByTime(5_000);
|
|
expect(other.written).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe('onClientGone', () => {
|
|
it('SSE-020: fires when the client leaves before the end, not after a finished response', () => {
|
|
const left = new FakeResponse();
|
|
const gone = vi.fn();
|
|
onClientGone(asResponse(left), gone);
|
|
left.emit('close');
|
|
expect(gone).toHaveBeenCalledTimes(1);
|
|
|
|
const finished = new FakeResponse();
|
|
const notGone = vi.fn();
|
|
onClientGone(asResponse(finished), notGone);
|
|
finished.end();
|
|
finished.emit('close');
|
|
expect(notGone).not.toHaveBeenCalled();
|
|
});
|
|
});
|