Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
6.6 KB
Raw Blame
// @vitest-environment jsdom
import { QueryClient } from "@tanstack/react-query";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { api } from "../api";
import { StoreThemeListSchema, type StoreTheme } from "../api/schemas";
import { INSTALLED_THEMES_STORAGE_KEY, useInstalledThemesStore } from "./installed-themes";
import { writeZip } from "./theme-archive";
import { parseTheme } from "./theme-package";
import { readStoreTheme, storeThemeScreenshotOptions, storeThemeState } from "./theme-store";

vi.mock("../api", () => ({ api: { getStoreThemePackage: vi.fn(), getStoreThemeScreenshot: vi.fn() } }));

const encoder = new TextEncoder();
const manifest = (id: string, version: string) => ({ apiVersion: 1, id, name: "Ink", author: "Someone", version });
const zip = (id: string, version: string, css = ".theme-light { --page-canvas: #f6efdf; }") =>
  writeZip([
    { path: `${id}/manifest.json`, data: encoder.encode(JSON.stringify(manifest(id, version))) },
    { path: `${id}/theme.css`, data: encoder.encode(css) },
  ]);
const entry = (overrides: Partial<StoreTheme> = {}): StoreTheme => ({
  id: "ink",
  name: "Ink",
  author: "Someone",
  repo: "someone/multica-theme-ink",
  modes: ["light", "dark"],
  version: "1.0.0",
  size: 100,
  screenshots: ["light", "dark"],
  downloads: 0,
  ...overrides,
});
const theme = (id: string, version = "1.0.0") => {
  const parsed = parseTheme({ manifest: manifest(id, version), css: "" });
  if (!parsed.ok) throw new Error("fixture");
  return parsed.theme;
};

describe("StoreThemeListSchema", () => {
  it("reads the download count, and counts none from a server that sends none", () => {
    const { downloads: _, ...withoutCount } = entry();
    const parsed = StoreThemeListSchema.parse({
      themes: [entry({ downloads: 12 }), { ...withoutCount, id: "sand" }],
    });
    expect(parsed.themes.map((t) => [t.id, t.downloads])).toEqual([
      ["ink", 12],
      ["sand", 0],
    ]);
  });
});

describe("storeThemeScreenshotOptions", () => {
  it("fetches a screenshot once per revision, so a re-render of the same release is fetched again", async () => {
    const old = new Blob(["old"]);
    const fresh = new Blob(["fresh"]);
    vi.mocked(api.getStoreThemeScreenshot).mockReset().mockResolvedValueOnce(old).mockResolvedValueOnce(fresh);
    const client = new QueryClient();
    const before = entry({ screenshot_revisions: { light: "aaaaaaaaaaaa" } });
    expect(await client.fetchQuery(storeThemeScreenshotOptions(before, "light"))).toBe(old);
    expect(await client.fetchQuery(storeThemeScreenshotOptions(before, "light"))).toBe(old);

    const after = entry({ screenshot_revisions: { light: "bbbbbbbbbbbb" } });
    expect(await client.fetchQuery(storeThemeScreenshotOptions(after, "light"))).toBe(fresh);
    expect(api.getStoreThemeScreenshot).toHaveBeenCalledTimes(2);
    expect(api.getStoreThemeScreenshot).toHaveBeenLastCalledWith("ink", "light", "bbbbbbbbbbbb", expect.anything());
  });

  it("reads revisions per mode and ignores malformed ones", () => {
    const parsed = StoreThemeListSchema.parse({
      themes: [
        entry({ screenshot_revisions: { light: "aaaaaaaaaaaa" } }),
        { ...entry({ id: "sand" }), screenshot_revisions: "nope" },
      ],
    });
    expect(parsed.themes.map((t) => t.screenshot_revisions)).toEqual([{ light: "aaaaaaaaaaaa" }, undefined]);
  });
});

describe("readStoreTheme", () => {
  it("reads the release like an imported theme", async () => {
    vi.mocked(api.getStoreThemePackage).mockResolvedValue(zip("ink", "1.0.0"));
    const result = await readStoreTheme(entry());
    expect(result.ok && result.theme.id).toBe("ink");
  });

  it("runs the same CSS checks as an import", async () => {
    vi.mocked(api.getStoreThemePackage).mockResolvedValue(
      zip("ink", "1.0.0", "body { background: url(https://evil.test/x.png); }"),
    );
    const result = await readStoreTheme(entry());
    expect(result.ok).toBe(false);
  });

  it("refuses a package that is another theme or another release", async () => {
    vi.mocked(api.getStoreThemePackage).mockResolvedValue(zip("other", "1.0.0"));
    expect((await readStoreTheme(entry())).ok).toBe(false);
    vi.mocked(api.getStoreThemePackage).mockResolvedValue(zip("ink", "0.9.0"));
    expect((await readStoreTheme(entry())).ok).toBe(false);
  });
});

describe("store installs", () => {
  beforeEach(() => {
    localStorage.clear();
    useInstalledThemesStore.setState({ themes: [], folders: {}, fromStore: {} });
  });
  const state = () => {
    const { themes, fromStore } = useInstalledThemesStore.getState();
    return storeThemeState(entry(), { themes, fromStore });
  };

  it("offers an update when the store lists another release", () => {
    expect(state()).toBe("available");
    useInstalledThemesStore.getState().installFromStore(theme("ink", "0.9.0"), "0.9.0");
    expect(state()).toBe("update");
    useInstalledThemesStore.getState().installFromStore(theme("ink"), "1.0.0");
    expect(state()).toBe("installed");
    useInstalledThemesStore.getState().uninstall("ink");
    expect(state()).toBe("available");
    expect(useInstalledThemesStore.getState().fromStore).toEqual({});
  });

  it("does not take over a built-in, imported or themes-folder theme with the same id", () => {
    expect(storeThemeState(entry({ id: "default" }), { themes: [], fromStore: {} })).toBe("taken");
    useInstalledThemesStore.getState().install(theme("ink"));
    expect(state()).toBe("taken");
    useInstalledThemesStore.getState().installFromStore(theme("ink"), "1.0.0");
    useInstalledThemesStore.getState().syncFolder([{ folder: "ink", result: { ok: true, theme: theme("ink", "2.0.0") } }]);
    expect(state()).toBe("taken");
    expect(useInstalledThemesStore.getState().fromStore).toEqual({});
  });

  it("forgets the store release when the theme is imported over it", () => {
    useInstalledThemesStore.getState().installFromStore(theme("ink"), "1.0.0");
    useInstalledThemesStore.getState().install(theme("ink", "1.1.0"));
    expect(useInstalledThemesStore.getState().fromStore).toEqual({});
  });

  it("keeps the store release across reloads, for installed themes only", async () => {
    const parsed = parseTheme({ manifest: manifest("ink", "1.0.0"), css: ".theme-light { --page-canvas: #f6efdf; }" });
    if (!parsed.ok) throw new Error("fixture");
    const stored = parsed.theme;
    localStorage.setItem(
      INSTALLED_THEMES_STORAGE_KEY,
      JSON.stringify({ state: { themes: [stored], folders: {}, fromStore: { ink: "1.0.0", gone: "1.0.0", bad: 3 } }, version: 0 }),
    );
    await useInstalledThemesStore.persist.rehydrate();
    expect(useInstalledThemesStore.getState().fromStore).toEqual({ ink: "1.0.0" });
  });
});