pi-mono/packages/connect/test/messages.test.ts
Armin Ronacher 7f2d80920a feat(connect): add pi-connect for browser sign-in
openConnect() gives browser apps a pi-ai Models collection: credentials and model catalogs in IndexedDB (Web Locks serialize token refreshes across tabs), OAuth sign-in in pi.dev's popup, the token requests browsers cannot send routed through pi.dev's relay, and support(), which says for every provider and method whether it works in the browser and why not.

Auth documents (metadata only, bundled and refreshed from pi.dev) describe the browser tiers; scripts/probe-cors.ts checks them against the providers' endpoints. The relay allowlist in relay.ts is the source pi.dev vendors.
2026-10-30 23:13:26 +01:00

47 lines
1.6 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { CONNECT_PROTOCOL, parseHelloMessage, parsePopupMessage } from "../src/messages.ts";
describe("parseHelloMessage", () => {
it("accepts a complete hello and nothing else", () => {
const hello = {
protocol: CONNECT_PROTOCOL,
type: "hello",
nonce: "n",
provider: "xai",
method: "device_code",
manual: false,
};
expect(parseHelloMessage(hello)).toEqual(hello);
expect(parseHelloMessage({ ...hello, manual: "yes" })).toBeUndefined();
expect(parseHelloMessage({ ...hello, protocol: "other" })).toBeUndefined();
});
});
describe("parsePopupMessage", () => {
it("accepts credentials with the fields pi-ai needs and keeps the provider's extra fields", () => {
const credential = { type: "oauth", access: "a", refresh: "r", expires: 1, accountId: "acct" };
expect(parsePopupMessage({ protocol: CONNECT_PROTOCOL, type: "credential", nonce: "n", credential })).toEqual({
protocol: CONNECT_PROTOCOL,
type: "credential",
nonce: "n",
credential,
});
expect(
parsePopupMessage({
protocol: CONNECT_PROTOCOL,
type: "credential",
nonce: "n",
credential: { type: "oauth" },
}),
).toBeUndefined();
});
it("requires a nonce on everything but ready", () => {
expect(parsePopupMessage({ protocol: CONNECT_PROTOCOL, type: "ready" })?.type).toBe("ready");
expect(parsePopupMessage({ protocol: CONNECT_PROTOCOL, type: "cancelled" })).toBeUndefined();
expect(parsePopupMessage({ protocol: CONNECT_PROTOCOL, type: "error", nonce: "n", message: "m" })).toMatchObject({
type: "error",
message: "m",
});
});
});