Add frontend app with terminal UI, sandbox integration, and asset handling.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
91049379f7
commit
b841c2d1f6
19 files changed
+800
-13
No files matched your search
@@ -0,0 +1,99 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { TerminalConnection, terminalUrl, type EndReason } from './connection.ts';
|
||||
|
||||
class FakeSocket
|
||||
{
|
||||
binaryType = 'blob';
|
||||
readyState: number = WebSocket.CONNECTING;
|
||||
sent: (string | Uint8Array)[] = [];
|
||||
onopen?: () => void;
|
||||
onmessage?: (event: { data: unknown }) => void;
|
||||
onclose?: () => void;
|
||||
|
||||
send(data: string | Uint8Array): void {
|
||||
this.sent.push(data);
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.onclose?.();
|
||||
}
|
||||
|
||||
open(): void {
|
||||
this.readyState = WebSocket.OPEN;
|
||||
this.onopen?.();
|
||||
}
|
||||
}
|
||||
|
||||
function connect()
|
||||
{
|
||||
const socket = new FakeSocket();
|
||||
const events = { ready: 0, output: [] as string[], ended: [] as EndReason[] };
|
||||
|
||||
const connection = new TerminalConnection('ws://test/ws', { cols: 80, rows: 24 }, {
|
||||
ready: () => events.ready++,
|
||||
output: (data) => events.output.push(new TextDecoder().decode(data)),
|
||||
ended: (reason) => events.ended.push(reason),
|
||||
}, () => socket as unknown as WebSocket);
|
||||
|
||||
return { socket, events, connection };
|
||||
}
|
||||
|
||||
describe('terminalUrl', () => {
|
||||
it('follows the page protocol', () => {
|
||||
expect(terminalUrl({ protocol: 'https:', host: 'security-command.org' })).toBe('wss://security-command.org/ws');
|
||||
expect(terminalUrl({ protocol: 'http:', host: 'localhost:5173' })).toBe('ws://localhost:5173/ws');
|
||||
});
|
||||
});
|
||||
|
||||
describe('TerminalConnection', () => {
|
||||
it('greets with the terminal size and uses binary frames', () => {
|
||||
const { socket, connection } = connect();
|
||||
|
||||
connection.input('ignored before open');
|
||||
socket.open();
|
||||
connection.input('ls\r');
|
||||
|
||||
expect(socket.binaryType).toBe('arraybuffer');
|
||||
expect(socket.sent[0]).toBe('{"type":"hello","cols":80,"rows":24}');
|
||||
expect(new TextDecoder().decode(socket.sent[1] as Uint8Array)).toBe('ls\r');
|
||||
expect(socket.sent).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('passes output and readiness on', () => {
|
||||
const { socket, events } = connect();
|
||||
socket.open();
|
||||
|
||||
socket.onmessage?.({ data: '{"type":"ready"}' });
|
||||
socket.onmessage?.({ data: new TextEncoder().encode('hi').buffer });
|
||||
|
||||
expect(events.ready).toBe(1);
|
||||
expect(events.output).toEqual(['hi']);
|
||||
});
|
||||
|
||||
it('reports the server reason, a lost connection, or a refusal', () => {
|
||||
const expired = connect();
|
||||
expired.socket.open();
|
||||
expired.socket.onmessage?.({ data: '{"type":"exit","reason":"expired"}' });
|
||||
expired.socket.close();
|
||||
|
||||
const lost = connect();
|
||||
lost.socket.open();
|
||||
lost.socket.close();
|
||||
|
||||
const rejected = connect();
|
||||
rejected.socket.close();
|
||||
rejected.socket.close();
|
||||
|
||||
expect(expired.events.ended).toEqual(['expired']);
|
||||
expect(lost.events.ended).toEqual(['lost']);
|
||||
expect(rejected.events.ended).toEqual(['rejected']);
|
||||
});
|
||||
|
||||
it('encodes binary input byte by byte', () => {
|
||||
const { socket, connection } = connect();
|
||||
socket.open();
|
||||
|
||||
connection.inputBinary('\x1b[M\xff');
|
||||
expect([...(socket.sent[1] as Uint8Array)]).toEqual([0x1b, 0x5b, 0x4d, 0xff]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user