// --------------------------------------------------------------------------- // Fidelity tests for the OAuth popup HTML generator + callback wrapper. // Locks in the escaping rules, postMessage/BroadcastChannel behavior, and // the completeOAuth-to-popup-payload conversion semantics so the google- // discovery port is provably behavior-preserving. // --------------------------------------------------------------------------- import { describe, expect, it } from "@effect/vitest"; import { Data, Effect, Schema } from "effect"; import { encodeOAuthCallbackState } from "@executor-js/sdk"; import { OAUTH_POPUP_MESSAGE_TYPE, popupDocument, runOAuthCallback, type OAuthPopupResult, } from "./oauth-popup"; type GoogleAuth = { kind: "oauth2"; accessTokenSecretId: string; refreshTokenSecretId: string | null; }; const DomainErrorShape = Schema.Struct({ _tag: Schema.Literal("DomainError"), message: Schema.String, }); const isDomainError = Schema.is(DomainErrorShape); // --------------------------------------------------------------------------- // popupDocument // --------------------------------------------------------------------------- describe("popupDocument", () => { const successPayload: OAuthPopupResult = { type: OAUTH_POPUP_MESSAGE_TYPE, ok: true, sessionId: "session-abc", kind: "oauth2", accessTokenSecretId: "secret_1", refreshTokenSecretId: "secret_2", }; it("renders a success page with Connected title and the green check icon", () => { const html = popupDocument(successPayload, "channel-x"); expect(html).toContain("Connected"); expect(html).toContain("#22c55e"); expect(html).toContain("Authentication complete"); }); it("renders a failure page with the error text escaped", () => { const html = popupDocument( { type: OAUTH_POPUP_MESSAGE_TYPE, ok: false, sessionId: null, error: "Token endpoint returned 400 ", }, "channel-x", ); expect(html).toContain("Connection failed"); expect(html).toContain("#ef4444"); expect(html).toContain("Token endpoint returned 400 <script>alert(1)</script>"); expect(html).not.toContain(""); }); it("renders a collapsible details disclosure when errorDetails is present", () => { const html = popupDocument( { type: OAUTH_POPUP_MESSAGE_TYPE, ok: false, sessionId: null, error: "Could not complete authentication", errorDetails: "HTTP 201 Created from https://api.supabase.com/v1/oauth/token", }, "channel-x", ); expect(html).toContain("Could not complete authentication"); expect(html).toContain(""); expect(html).toContain("HTTP 201 Created from https://api.supabase.com/v1/oauth/token"); }); it("escapes HTML inside errorDetails", () => { const html = popupDocument( { type: OAUTH_POPUP_MESSAGE_TYPE, ok: false, sessionId: null, error: "Failed", errorDetails: "", }, "channel-x", ); expect(html).toContain("<script>alert('xss')</script>"); // Ensure the raw script tag does not appear inside the rendered
.
    const preMatch = /]*>([^<]*)<\/pre>/.exec(html);
    expect(preMatch).not.toBeNull();
    expect(preMatch![1]).not.toContain(" breakout", () => {
    const html = popupDocument(
      {
        type: OAUTH_POPUP_MESSAGE_TYPE,
        ok: false,
        sessionId: null,
        error: "",
      },
      "channel-x",
    );
    // The raw `` must not appear in the inline script literal.
    const scriptLiteralMatch = /const p=(\{.*?\});/.exec(html);
    expect(scriptLiteralMatch).not.toBeNull();
    const scriptLiteral = scriptLiteralMatch![1]!;
    expect(scriptLiteral).not.toContain("");
    expect(scriptLiteral).toContain("\\u003c/script\\u003e");
  });

  it("escapes < > & in the serialized channel name to prevent  breakout", () => {
    const html = popupDocument(successPayload, 'channel');
    const scriptMatch = /");
    expect(script).toContain("channel\\u003c/script\\u003e");
  });

  // The assertions below RUN the generated script against stub globals rather
  // than matching its source text. A string check would pass on a script that
  // never executes — and the property at stake here is what the browser is left
  // holding, which only running it can show.
  const runPopupScript = (html: string) => {
    const script = /