Skip to content

Write HTML. Render video. Built for agents.

4.9 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { writeStore } from "../../auth/store.js";
import { setupTempAuthEnv, type EnvFixture } from "../../auth/_test-utils.js";
import { consumeCommandResult } from "../../utils/commandResult.js";

// Mock only AuthClient so the live /v3/users/me probe is controllable;
// keep the real store/resolver/user helpers so the test exercises the
// actual on-disk persisted-user-block surfacing.
//   - apiReject: throw ErrApi on getCurrentUser (simulates an API blip)
//   - user:      the live identity returned on success
const probeState = vi.hoisted(
  () =>
    ({ apiReject: false, user: { email: "[email protected]" } }) as {
      apiReject: boolean;
      user: Record<string, unknown>;
    },
);

vi.mock("../../auth/index.js", async (orig) => {
  const actual = await orig<typeof import("../../auth/index.js")>();
  class MockAuthClient {
    async getCurrentUser(): Promise<Record<string, unknown>> {
      if (probeState.apiReject) {
        const { ErrApi } = await import("../../auth/errors.js");
        throw ErrApi(503, "service unavailable");
      }
      return probeState.user;
    }
  }
  return { ...actual, AuthClient: MockAuthClient };
});

describe("auth status — persisted user block surface", () => {
  let envFixture: EnvFixture;
  let stdout: string[];

  beforeEach(async () => {
    envFixture = await setupTempAuthEnv("hf-status-");
    probeState.apiReject = false;
    probeState.user = { email: "[email protected]" };
    stdout = [];
    consumeCommandResult();
    vi.spyOn(console, "log").mockImplementation((...args: unknown[]) => {
      stdout.push(args.join(" "));
    });
    vi.spyOn(console, "error").mockImplementation(() => {});
  });

  afterEach(async () => {
    consumeCommandResult();
    vi.restoreAllMocks();
    await envFixture.restore();
  });

  async function runStatus(asJson: boolean): Promise<number> {
    const cmd = (await import("./status.js")).default;
    await (cmd.run as (ctx: { args: Record<string, unknown> }) => Promise<void>)({
      args: { json: asJson },
    });
    return consumeCommandResult().exitCode;
  }

  function lastJson(): Record<string, unknown> {
    return JSON.parse(stdout[stdout.length - 1] ?? "{}");
  }

  it("reports host-managed OAuth without showing its token or a stored identity", async () => {
    process.env["HEYGEN_ACCESS_TOKEN"] = "fixture-host-secret";
    await writeStore({ user: { email: "[email protected]" } });
    expect(await runStatus(true)).toBe(0);
    expect(lastJson()).toMatchObject({ source: "env_oauth", persisted_user: null });
    expect(await runStatus(false)).toBe(0);
    expect(stdout.join("\n")).toContain("env (HEYGEN_ACCESS_TOKEN)");
    expect(stdout.join("\n")).not.toContain("fixture-host-secret");
    expect(stdout.join("\n")).not.toContain("[email protected]");
  });

  it("surfaces the persisted user block (with resolved display_name) for a file credential", async () => {
    await writeStore({
      api_key: "hg_x",
      user: { email: "[email protected]", first_name: "Jane", last_name: "Doe", username: "jdoe" },
    });

    const code = await runStatus(true);
    expect(code).toBe(0);
    const payload = lastJson();
    expect(payload["source"]).toBe("file_json");
    expect(payload["persisted_user"]).toEqual({
      email: "[email protected]",
      first_name: "Jane",
      last_name: "Doe",
      username: "jdoe",
      display_name: "[email protected]",
    });
  });

  it("backwards-compat: a file credential with no user block reports persisted_user: null", async () => {
    await writeStore({ api_key: "hg_legacy" });

    const code = await runStatus(true);
    expect(code).toBe(0);
    const payload = lastJson();
    expect(payload["persisted_user"]).toBeNull();
    // The live `user` field (from the API probe) is unchanged / additive.
    expect(payload["user"]).toEqual({ email: "[email protected]" });
  });

  it("skips the persisted block for an env-sourced credential (could be a different key)", async () => {
    // Seed a file-side user block, then resolve via the env key — the
    // active credential is env, so the file block must NOT be surfaced.
    await writeStore({ api_key: "hg_file", user: { email: "[email protected]" } });
    process.env["HEYGEN_API_KEY"] = "hg_env_key";

    const code = await runStatus(true);
    expect(code).toBe(0);
    const payload = lastJson();
    expect(payload["source"]).toBe("env");
    expect(payload["persisted_user"]).toBeNull();
  });

  it("falls back to the cached identity in human output when the live probe fails", async () => {
    await writeStore({ api_key: "hg_x", user: { email: "[email protected]" } });
    probeState.apiReject = true;

    const code = await runStatus(false);
    expect(code).toBe(1); // API failure → non-zero exit
    const text = stdout.join("\n");
    expect(text).toContain("API check failed");
    expect(text).toContain("[email protected]");
    expect(text).toContain("cached");
  });
});