import {
AuthSessionState as AuthSessionStateSchema,
EnvironmentAuthInvalidError,
type AuthBrowserSessionResult,
type AuthCreatePairingCredentialInput,
type AuthSessionState,
type DesktopBridge,
} from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
import { installEnvironmentHttpTest } from "../test/environmentHttpTest";
type TestWindow = {
location: URL;
history: {
replaceState: (_data: unknown, _unused: string, url: string) => void;
};
desktopBridge?: DesktopBridge;
};
const LOOPBACK_AUTH = {
policy: "loopback-browser",
bootstrapMethods: ["one-time-token"],
sessionMethods: ["browser-session-cookie"],
sessionCookieName: "t3_session",
} as const;
const DESKTOP_AUTH = {
policy: "desktop-managed-local",
bootstrapMethods: ["desktop-bootstrap"],
sessionMethods: ["browser-session-cookie"],
sessionCookieName: "t3_session",
} as const;
const SESSION_EXPIRES_AT = DateTime.makeUnsafe("2026-04-05T00:00:00.000Z");
const encodeAuthSessionState = Schema.encodeSync(AuthSessionStateSchema);
const unauthenticatedSession = (auth: AuthSessionState["auth"]): AuthSessionState => ({
authenticated: false,
auth,
});
const authenticatedSession = (auth: AuthSessionState["auth"]): AuthSessionState => ({
authenticated: true,
auth,
sessionMethod: "browser-session-cookie",
expiresAt: SESSION_EXPIRES_AT,
});
const browserSession = (scopes: AuthBrowserSessionResult["scopes"]): AuthBrowserSessionResult => ({
authenticated: true,
scopes,
sessionMethod: "browser-session-cookie",
expiresAt: SESSION_EXPIRES_AT,
});
function installTestBrowser(url: string) {
const testWindow: TestWindow = {
location: new URL(url),
history: {
replaceState: (_data, _unused, nextUrl) => {
testWindow.location = new URL(nextUrl, testWindow.location.href);
},
},
};
vi.stubGlobal("window", testWindow);
vi.stubGlobal("document", { title: "T3 Code" });
return testWindow;
}
function sequence(...values: ReadonlyArray) {
let index = 0;
return () => values[Math.min(index++, values.length - 1)]!;
}
let disposeHttpTest: (() => Promise) | undefined;
async function installAuthApi(input: {
readonly session?: () => AuthSessionState;
readonly browserSession?: (
credential: string,
) => Effect.Effect;
readonly pairingCredential?: (payload: AuthCreatePairingCredentialInput) => Effect.Effect<{
readonly id: string;
readonly credential: string;
readonly label?: string;
readonly expiresAt: DateTime.Utc;
}>;
}) {
const testApi = await installEnvironmentHttpTest({
...(input.session ? { session: () => Effect.succeed(input.session!()) } : {}),
...(input.browserSession
? { browserSession: (payload) => input.browserSession!(payload.credential) }
: {}),
...(input.pairingCredential
? { pairingCredential: (payload) => input.pairingCredential!(payload) }
: {}),
});
disposeHttpTest = testApi.dispose;
return testApi;
}
describe("resolveInitialServerAuthGateState", () => {
beforeEach(() => {
vi.restoreAllMocks();
vi.useRealTimers();
installTestBrowser("http://localhost/");
});
afterEach(async () => {
await disposeHttpTest?.();
disposeHttpTest = undefined;
const { __resetServerAuthBootstrapForTests } = await import("./environments/primary");
__resetServerAuthBootstrapForTests();
vi.unstubAllEnvs();
vi.useRealTimers();
vi.restoreAllMocks();
});
it("reuses an in-flight silent bootstrap attempt", async () => {
const nextSession = sequence(
unauthenticatedSession(DESKTOP_AUTH),
authenticatedSession(DESKTOP_AUTH),
);
const testApi = await installAuthApi({
session: nextSession,
browserSession: () => Effect.succeed(browserSession(["orchestration:read", "access:write"])),
});
const testWindow = installTestBrowser("http://localhost/");
testWindow.desktopBridge = {
getLocalEnvironmentBootstrap: () => ({
label: "Local environment",
httpBaseUrl: "http://localhost:3773",
wsBaseUrl: "ws://localhost:3773",
bootstrapToken: "desktop-bootstrap-token",
}),
} as DesktopBridge;
const { resolveInitialServerAuthGateState } = await import("./environments/primary");
await Promise.all([resolveInitialServerAuthGateState(), resolveInitialServerAuthGateState()]);
expect(testApi.calls.session).toBe(2);
expect(testApi.calls.browserSession).toEqual([{ credential: "desktop-bootstrap-token" }]);
});
it("uses https urls when the primary environment uses wss", async () => {
await installAuthApi({ session: () => unauthenticatedSession(LOOPBACK_AUTH) });
vi.stubEnv("VITE_HTTP_URL", "https://remote.example.com");
vi.stubEnv("VITE_WS_URL", "wss://remote.example.com");
const { resolveInitialServerAuthGateState, resolvePrimaryEnvironmentHttpUrl } =
await import("./environments/primary");
await expect(resolveInitialServerAuthGateState()).resolves.toEqual({
status: "requires-auth",
auth: LOOPBACK_AUTH,
});
expect(resolvePrimaryEnvironmentHttpUrl("/api/auth/session")).toBe(
"https://remote.example.com/api/auth/session",
);
});
it("uses the current origin as an auth proxy base for local dev environments", async () => {
await installAuthApi({ session: () => unauthenticatedSession(LOOPBACK_AUTH) });
installTestBrowser("http://localhost:5735/");
const { resolveInitialServerAuthGateState, resolvePrimaryEnvironmentHttpUrl } =
await import("./environments/primary");
await expect(resolveInitialServerAuthGateState()).resolves.toEqual({
status: "requires-auth",
auth: LOOPBACK_AUTH,
});
expect(resolvePrimaryEnvironmentHttpUrl("/api/auth/session")).toBe(
"http://localhost:5735/api/auth/session",
);
});
it("uses the vite proxy for desktop-managed loopback auth requests during local dev", async () => {
await installAuthApi({ session: () => unauthenticatedSession(DESKTOP_AUTH) });
vi.stubEnv("VITE_DEV_SERVER_URL", "http://127.0.0.1:5733");
const testWindow = installTestBrowser("http://127.0.0.1:5733/");
testWindow.desktopBridge = {
getLocalEnvironmentBootstrap: () => ({
label: "Local environment",
httpBaseUrl: "http://127.0.0.1:3773",
wsBaseUrl: "ws://127.0.0.1:3773",
}),
} as DesktopBridge;
const { resolveInitialServerAuthGateState, resolvePrimaryEnvironmentHttpUrl } =
await import("./environments/primary");
await expect(resolveInitialServerAuthGateState()).resolves.toEqual({
status: "requires-auth",
auth: DESKTOP_AUTH,
});
expect(resolvePrimaryEnvironmentHttpUrl("/api/auth/session")).toBe(
"http://127.0.0.1:5733/api/auth/session",
);
});
it("returns a requires-auth state instead of throwing when no bootstrap credential exists", async () => {
await installAuthApi({ session: () => unauthenticatedSession(LOOPBACK_AUTH) });
const { resolveInitialServerAuthGateState } = await import("./environments/primary");
await expect(resolveInitialServerAuthGateState()).resolves.toEqual({
status: "requires-auth",
auth: LOOPBACK_AUTH,
});
});
it("retries transient auth session bootstrap failures after restart", async () => {
vi.useFakeTimers();
const fetchMock = vi
.fn()
.mockResolvedValueOnce(new Response("Bad Gateway", { status: 502 }))
.mockResolvedValueOnce(new Response("Bad Gateway", { status: 502 }))
.mockResolvedValueOnce(new Response("Bad Gateway", { status: 502 }))
.mockResolvedValueOnce(
new Response(
JSON.stringify(encodeAuthSessionState(unauthenticatedSession(LOOPBACK_AUTH))),
{ status: 200, headers: { "content-type": "application/json" } },
),
);
vi.stubGlobal("fetch", fetchMock);
const { resolveInitialServerAuthGateState } = await import("./environments/primary");
const gateStatePromise = resolveInitialServerAuthGateState();
await vi.advanceTimersByTimeAsync(2_000);
await expect(gateStatePromise).resolves.toEqual({
status: "requires-auth",
auth: LOOPBACK_AUTH,
});
expect(fetchMock).toHaveBeenCalledTimes(4);
});
it("takes a pairing token from the location hash and strips it immediately", async () => {
const testWindow = installTestBrowser("http://localhost/#token=pairing-token");
const { takePairingTokenFromUrl } = await import("./environments/primary");
expect(takePairingTokenFromUrl()).toBe("pairing-token");
expect(testWindow.location.hash).toBe("");
expect(testWindow.location.searchParams.get("token")).toBeNull();
});
it("accepts query-string pairing tokens as a backward-compatible fallback", async () => {
const testWindow = installTestBrowser("http://localhost/?token=pairing-token");
const { takePairingTokenFromUrl } = await import("./environments/primary");
expect(takePairingTokenFromUrl()).toBe("pairing-token");
expect(testWindow.location.searchParams.get("token")).toBeNull();
});
it("allows manual token submission after the initial auth check requires pairing", async () => {
const nextSession = sequence(
unauthenticatedSession(LOOPBACK_AUTH),
authenticatedSession(LOOPBACK_AUTH),
);
const testApi = await installAuthApi({
session: nextSession,
browserSession: () => Effect.succeed(browserSession(["orchestration:read"])),
});
const { resolveInitialServerAuthGateState, submitServerAuthCredential } =
await import("./environments/primary");
await expect(resolveInitialServerAuthGateState()).resolves.toEqual({
status: "requires-auth",
auth: LOOPBACK_AUTH,
});
await expect(submitServerAuthCredential("retry-token")).resolves.toBeUndefined();
await expect(resolveInitialServerAuthGateState()).resolves.toEqual({
status: "authenticated",
});
expect(testApi.calls.browserSession).toEqual([{ credential: "retry-token" }]);
expect(testApi.calls.session).toBe(2);
});
it("surfaces a friendly error message when an invalid pairing token is submitted", async () => {
const testApi = await installAuthApi({
browserSession: () =>
Effect.fail(
new EnvironmentAuthInvalidError({
code: "auth_invalid",
reason: "invalid_credential",
traceId: "trace-invalid-credential",
}),
),
});
const { submitServerAuthCredential } = await import("./environments/primary");
await expect(submitServerAuthCredential("bad-token")).rejects.toThrow(
"Invalid pairing token. Check the token and try again.",
);
expect(testApi.calls.browserSession).toEqual([{ credential: "bad-token" }]);
});
it("waits for the authenticated session to become observable after silent desktop bootstrap", async () => {
vi.useFakeTimers();
const nextSession = sequence(
unauthenticatedSession(DESKTOP_AUTH),
unauthenticatedSession(DESKTOP_AUTH),
authenticatedSession(DESKTOP_AUTH),
);
const testApi = await installAuthApi({
session: nextSession,
browserSession: () => Effect.succeed(browserSession(["orchestration:read", "access:write"])),
});
const testWindow = installTestBrowser("http://localhost/");
testWindow.desktopBridge = {
getLocalEnvironmentBootstrap: () => ({
label: "Local environment",
httpBaseUrl: "http://localhost:3773",
wsBaseUrl: "ws://localhost:3773",
bootstrapToken: "desktop-bootstrap-token",
}),
} as DesktopBridge;
const { resolveInitialServerAuthGateState } = await import("./environments/primary");
const gateStatePromise = resolveInitialServerAuthGateState();
await vi.advanceTimersByTimeAsync(100);
await expect(gateStatePromise).resolves.toEqual({ status: "authenticated" });
expect(testApi.calls.session).toBe(3);
});
it("memoizes the authenticated gate state after the first successful read", async () => {
const testApi = await installAuthApi({
session: sequence(authenticatedSession(LOOPBACK_AUTH), unauthenticatedSession(LOOPBACK_AUTH)),
});
const { resolveInitialServerAuthGateState } = await import("./environments/primary");
await expect(resolveInitialServerAuthGateState()).resolves.toEqual({
status: "authenticated",
});
await expect(resolveInitialServerAuthGateState()).resolves.toEqual({
status: "authenticated",
});
expect(testApi.calls.session).toBe(1);
});
it("creates a pairing credential from the authenticated auth endpoint", async () => {
const testApi = await installAuthApi({
pairingCredential: (payload) =>
Effect.succeed({
id: "pairing-link-1",
credential: "pairing-token",
...(payload.label === undefined ? {} : { label: payload.label }),
expiresAt: SESSION_EXPIRES_AT,
}),
});
const { createServerPairingCredential } = await import("./environments/primary");
const credential = await createServerPairingCredential({
label: "Julius iPhone",
scopes: ["orchestration:read"],
});
expect(credential).toMatchObject({
id: "pairing-link-1",
credential: "pairing-token",
label: "Julius iPhone",
});
expect(DateTime.formatIso(credential.expiresAt)).toBe("2026-04-05T00:00:00.000Z");
expect(testApi.calls.pairingCredential).toEqual([
{ label: "Julius iPhone", scopes: ["orchestration:read"] },
]);
});
});