Switch from Asuka to MyGo, keeping the API client and the feature set: - servers: saved servers, passwordless and password login, proxy, self-signed certificates, per-server device tokens (Keystore), connect form, instance list sheet with switch/disconnect - sessions: virtualized list, search, new session, rename/archive/delete, running indicator, event stream keeps it live - chat: pushed window with system back bar, streaming Markdown via ui/markdown, reasoning and tool rows with image previews, footer menu (read aloud via a Kotlin TTS plugin, copy, fork), user message menu (copy, revert, fork), context usage ring and detail sheet, attachments (photos/files via the document picker), permissions - release builds allow plain http:// LAN servers (Go's net/http dials directly, no cleartext manifest needed) - a local fork of MyGo (third_party/mygo) lowers the Android host's minSdk to 24 (Android 7.0); Go 1.27.4 via mise Version 0.3.0.
165 lines
5.7 KiB
TypeScript
165 lines
5.7 KiB
TypeScript
import { afterEach, expect, test } from "bun:test";
|
|
import { b64, fakeGo, tick, type GoCall } from "../../fake-go";
|
|
import { WebSocket } from "./index";
|
|
|
|
let go: ReturnType<typeof fakeGo> | undefined;
|
|
afterEach(() => go?.uninstall());
|
|
|
|
/** A server that echoes, as the Go side streams it. */
|
|
function echoServer() {
|
|
const sent: unknown[][] = [];
|
|
let conn: GoCall | undefined;
|
|
let finish: (() => void) | undefined;
|
|
go = fakeGo({
|
|
"plugin:websocket.Connect": (c) =>
|
|
new Promise<void>((resolve) => {
|
|
conn = c;
|
|
finish = resolve;
|
|
c.signal.addEventListener("abort", () => resolve());
|
|
c.send(1, { type: "open", protocol: "chat" });
|
|
}),
|
|
"plugin:websocket.Send": (c) => {
|
|
sent.push(c.args);
|
|
const m = c.args[2] as { type: string; text?: string; data?: string; code?: number };
|
|
if (m.type === "close") {
|
|
conn!.send(1, { type: "close", code: m.code, reason: "bye", clean: true });
|
|
finish!();
|
|
} else conn!.send(1, m.type === "text" ? { type: "text", text: m.text } : { type: "binary", data: m.data });
|
|
},
|
|
});
|
|
return { sent, server: () => conn! };
|
|
}
|
|
|
|
const next = (ws: WebSocket, type: string) => new Promise<any>((r) => ws.addEventListener(type, r, { once: true }));
|
|
|
|
test("opens, echoes and closes", async () => {
|
|
const { sent } = echoServer();
|
|
const ws = new WebSocket("https://example.com/live", ["chat"], { headers: { authorization: "Bearer t" } });
|
|
expect(ws.url).toBe("wss://example.com/live");
|
|
expect(ws.readyState).toBe(WebSocket.CONNECTING);
|
|
expect(() => ws.send("early")).toThrow("not open");
|
|
await next(ws, "open");
|
|
expect(ws.readyState).toBe(ws.OPEN);
|
|
expect(ws.protocol).toBe("chat");
|
|
const [connect] = go!.calls[0]![1] as [{ id: string; headers: [string, string][] }];
|
|
expect(connect.headers).toEqual([["authorization", "Bearer t"]]);
|
|
|
|
const got: unknown[] = [];
|
|
ws.onmessage = (e) => got.push(e.data);
|
|
ws.send("hello");
|
|
expect(ws.bufferedAmount).toBe(5);
|
|
ws.binaryType = "arraybuffer";
|
|
ws.send(new Uint8Array([1, 2, 3]));
|
|
while (got.length < 2 || ws.bufferedAmount > 0) await tick();
|
|
expect(got[0]).toBe("hello");
|
|
expect([...new Uint8Array(got[1] as ArrayBuffer)]).toEqual([1, 2, 3]);
|
|
// Messages carry their order.
|
|
expect(sent.map((s) => s[1])).toEqual([1, 2]);
|
|
expect(sent[0]![0]).toBe(connect.id);
|
|
|
|
const closed = next(ws, "close");
|
|
let errored = false;
|
|
ws.onerror = () => (errored = true);
|
|
ws.close(4001, "done");
|
|
expect(ws.readyState).toBe(WebSocket.CLOSING);
|
|
const e = await closed;
|
|
expect([e.code, e.reason, e.wasClean]).toEqual([4001, "bye", true]);
|
|
expect(ws.readyState).toBe(WebSocket.CLOSED);
|
|
expect(errored).toBe(false);
|
|
expect(sent[2]![2]).toEqual({ type: "close", code: 4001, reason: "done" });
|
|
});
|
|
|
|
test("receives blobs", async () => {
|
|
const { server } = echoServer();
|
|
const ws = new WebSocket("ws://example.com");
|
|
await next(ws, "open");
|
|
const msg = next(ws, "message");
|
|
server().send(1, { type: "binary", data: b64("blob") });
|
|
const e = await msg;
|
|
expect(e.data).toBeInstanceOf(Blob);
|
|
expect(await e.data.text()).toBe("blob");
|
|
});
|
|
|
|
test("fails", async () => {
|
|
go = fakeGo({
|
|
"plugin:websocket.Connect": (c) => c.send(1, { type: "close", code: 1006, error: "the server answered the handshake with 403 Forbidden" }),
|
|
});
|
|
const errors: string[] = [];
|
|
const log = console.error;
|
|
console.error = (m: string) => errors.push(m);
|
|
try {
|
|
const ws = new WebSocket("ws://example.com");
|
|
const order: string[] = [];
|
|
ws.onerror = () => order.push("error");
|
|
ws.onclose = (e) => order.push(`close ${e.code} ${e.wasClean}`);
|
|
await next(ws, "close");
|
|
expect(order).toEqual(["error", "close 1006 false"]);
|
|
expect(errors[0]).toContain("403 Forbidden");
|
|
} finally {
|
|
console.error = log;
|
|
}
|
|
});
|
|
|
|
test("closing while connecting stops the handshake", async () => {
|
|
let stopped = false;
|
|
go = fakeGo({
|
|
"plugin:websocket.Connect": (c) =>
|
|
new Promise<void>((r) =>
|
|
c.signal.addEventListener("abort", () => {
|
|
stopped = true;
|
|
r();
|
|
}),
|
|
),
|
|
});
|
|
const log = console.error;
|
|
console.error = () => {};
|
|
try {
|
|
const ws = new WebSocket("ws://example.com");
|
|
await tick();
|
|
ws.close();
|
|
const e = await next(ws, "close");
|
|
expect(e.code).toBe(1006);
|
|
expect(stopped).toBe(true);
|
|
} finally {
|
|
console.error = log;
|
|
}
|
|
});
|
|
|
|
test("validates", () => {
|
|
go = fakeGo({});
|
|
expect(() => new WebSocket("ftp://example.com")).toThrow("scheme");
|
|
expect(() => new WebSocket("ws://example.com/#x")).toThrow("fragment");
|
|
expect(() => new WebSocket("ws://example.com", ["a", "a"])).toThrow("duplicates");
|
|
const ws = new WebSocket("ws://example.com");
|
|
expect(() => ws.close(1001)).toThrow("Invalid close code");
|
|
expect(() => ws.close(1000, "x".repeat(124))).toThrow("123 bytes");
|
|
});
|
|
|
|
test("rejects invalid subprotocols before contacting Go", () => {
|
|
go = fakeGo({});
|
|
for (const protocol of ["", "chat room", "chat,other", "chat/1", "chat;v=1", "chat\n", "\tchat", "chát", "chat\u007f"]) {
|
|
for (const protocols of [protocol, ["valid", protocol]]) {
|
|
let thrown: unknown;
|
|
try {
|
|
new WebSocket("ws://example.com", protocols);
|
|
} catch (error) {
|
|
thrown = error;
|
|
}
|
|
expect(thrown).toBeInstanceOf(DOMException);
|
|
expect(thrown).toHaveProperty("name", "SyntaxError");
|
|
}
|
|
}
|
|
expect(go.calls).toHaveLength(0);
|
|
});
|
|
|
|
test("accepts HTTP token punctuation in subprotocols", async () => {
|
|
const { server } = echoServer();
|
|
const protocols = ["chat.v2", "!#$%&'*+-.^_`|~0123456789AZaz"];
|
|
const ws = new WebSocket("ws://example.com", protocols);
|
|
await next(ws, "open");
|
|
expect((server().args[0] as { protocols: string[] }).protocols).toEqual(protocols);
|
|
const closed = next(ws, "close");
|
|
ws.close();
|
|
await closed;
|
|
});
|