Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

1.9 KB
Raw Blame
import { afterEach, describe, expect, it, vi } from "vitest";

const fixture = vi.hoisted(() => ({ api: vi.fn() }));
vi.mock("@/state/api", () => ({ api: fixture.api }));

import { openExternalLink } from "./app-links";

const PAGE = "abcdefghijklmnopqrstuvwxyz";
const LINK = `http://127.0.0.1:4100/api/published-pages/${PAGE}/`;

function desktop() {
  const opened: string[] = [];
  vi.stubGlobal("window", { ogb: { openExternal: async (url: string) => { opened.push(url); } }, open: vi.fn() });
  return opened;
}

afterEach(() => {
  vi.unstubAllGlobals();
  fixture.api.mockReset();
});

describe("opening a link from the desktop app", () => {
  it("opens an owner-only page's link with a one-time code, since the system browser is not signed in", async () => {
    const opened = desktop();
    fixture.api.mockResolvedValue({ url: `${LINK}?code=${"c".repeat(43)}` });
    await openExternalLink(LINK);
    expect(fixture.api).toHaveBeenCalledWith(`/api/published-pages/${PAGE}/open`, { method: "POST", body: "{}" });
    expect(opened).toEqual([`${LINK}?code=${"c".repeat(43)}`]);
  });

  it("keeps the link as it is when the server refuses, or answers for another address", async () => {
    const opened = desktop();
    fixture.api.mockRejectedValueOnce(new Error("Not found."));
    await openExternalLink(LINK);
    fixture.api.mockResolvedValueOnce({ url: `https://elsewhere.example/api/published-pages/${PAGE}/?code=x` });
    await openExternalLink(LINK);
    expect(opened).toEqual([LINK, LINK]);
  });

  it("asks nothing for any other link, including a file of the page", async () => {
    const opened = desktop();
    for (const url of ["https://example.com/", `${LINK}data.csv`, `${LINK}?period=1`, "not a url"]) await openExternalLink(url);
    expect(fixture.api).not.toHaveBeenCalled();
    expect(opened).toEqual(["https://example.com/", `${LINK}data.csv`, `${LINK}?period=1`, "not a url"]);
  });
});