Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

10.9 KB
Raw Blame
import { readFile } from "node:fs/promises";
import test from "node:test";
import assert from "node:assert/strict";
import { BUILTIN_SKILL_CATALOG } from "../../../packages/shared/dist/index.js";

const read = (path) => readFile(new URL(path, import.meta.url), "utf8");

const [panel, page, en, zh] = await Promise.all([
  read("../src/components/settings/SkillMarketPanel.tsx"),
  read("../src/components/settings/AgentSkillsPage.tsx"),
  read("../../../packages/i18n/src/locales/en/index.ts"),
  read("../../../packages/i18n/src/locales/zh-CN/index.ts"),
]);

test("skill market installs through the existing create path only", () => {
  assert.match(panel, /api\.fetchSkillMarketDocument\(/);
  assert.match(panel, /api\.createUserSkill\(/);
  assert.match(panel, /assembleSkillInstall\(/);
  assert.match(panel, /level: "global"/);
  assert.match(panel, /scope: GLOBAL_SCOPE/);
  assert.doesNotMatch(panel, /skillImport|writeFile|host\.call\(/);
});

test("install sheet previews the assembled document and blocks oversized bodies", () => {
  assert.match(panel, /settings\.sklm\.willInstall/);
  assert.match(panel, /settings\.sklm\.preview/);
  assert.match(panel, /settings\.sklm\.documentTooLarge/);
  assert.match(panel, /setDocumentBody\(assembled\.body\)/);
  assert.match(panel, /documentTooLarge/);
});

test("installed state comes from matching skill ids", () => {
  assert.match(panel, /installedIds\.includes\(entry\.id\)/);
  assert.match(page, /installedIds=\{\[\.\.\.globalSkills, \.\.\.projectSkills\]/);
});

test("source badges use the catalog sourceId, including default GitHub sources", () => {
  assert.match(panel, /DEFAULT_SKILL_SOURCES, \.\.\.sources/);
  assert.match(panel, /entry\.sourceId \?/);
  assert.doesNotMatch(panel, /remoteIds\.has\(entry\.id\)/);
});

test("skills page wires the market view with reload on exit", () => {
  assert.match(page, /view === "market"/);
  assert.match(page, /setView\("skills"\);\s*\n\s*void load\(\)/);
  assert.match(page, /<SkillMarketPanel/);
});

test("skill market strings exist in en and zh-CN", () => {
  for (const locale of [en, zh]) {
    assert.match(locale, /sklm: \{/);
    assert.match(locale, /browse: "/);
    assert.match(locale, /installSuccess: "/);
    assert.match(locale, /documentTooLarge: "/);
  }
});

test("builtin catalog keeps the offline promise in English", () => {
  assert.ok(BUILTIN_SKILL_CATALOG.skills.length >= 8);
  const ids = BUILTIN_SKILL_CATALOG.skills.map((s) => s.id);
  assert.equal(new Set(ids).size, ids.length, "duplicate catalog ids");
  for (const skill of BUILTIN_SKILL_CATALOG.skills) {
    assert.equal(/[^\u0000-\u007f]/.test(skill.name), false, skill.name);
  }
});

test("preview race gate tokens guard in-flight responses and close invalidates them", () => {
  // Review round 2 (#290): a slow preview A must never land after a faster B,
  // and closing the sheet must invalidate whatever is still in flight.
  assert.match(panel, /previewGate = useRef\(new LatestWinsGate\(\)\)/);
  assert.match(panel, /const token = previewGate\.current\.begin\(\)/);
  assert.match(panel, /previewGate\.current\.isCurrent\(token\)/);
  assert.match(panel, /if \(!installFor\) previewGate\.current\.invalidate\(\)/);
  // A fresh open resets the previous document before the fetch resolves.
  assert.match(panel, /setDocumentBody\(null\)/);
  assert.match(panel, /setDocumentTooLarge\(false\)/);
});

test("a failed preview is reported instead of silently disabling install", () => {
  // Issue #419: the catch used to reset the body and say nothing, so the
  // install button sat disabled behind the word "Loading…" with no reason and
  // no way to try again. The preview-before-save gate itself is intentional and
  // stays — what changes is that a failure is now legible and recoverable.
  assert.match(panel, /setPreviewFailure\(\{/);
  assert.match(panel, /const kind = classifySkillMarketFailure\(error\)/);
  assert.match(panel, /detail: skillMarketFailureDetail\(error\)/);
  assert.match(panel, /settings\.sklm\.previewError/);
  assert.match(panel, /settings\.sklm\.previewPolicyError/);
  assert.match(panel, /settings\.sklm\.proxyHint/);
  assert.match(panel, /settings\.sklm\.failureDetail/);
  assert.match(panel, /settings\.sklm\.retryPreview/);
  // Announced once, by the toast host, with the classified copy: the sheet
  // keeps the hints, the host's detail and its retry rather than an error row.
  assert.match(
    panel,
    /showToast\(\s*t\(\s*kind === "policy"[\s\S]{0,400}\{ variant: "error" \}/,
  );
  assert.match(panel, /disabled=\{installing \|\| documentBody === null \|\| documentTooLarge\}/);
  assert.match(panel, /const retryPreview = \(\) => \{/);
  assert.match(panel, /if \(installFor\) loadDocument\(installFor\)/);
});

test("the market list explains a policy refusal instead of a bare unreachable", () => {
  assert.match(panel, /hasPolicyFailure\(remote\.failureKinds\)/);
  assert.match(panel, /settings\.sklm\.remoteErrorPolicy/);
  assert.match(panel, /settings\.sklm\.remoteErrorQuery/);
  // A partial outage (some sources up, some refused) used to say nothing at all.
  assert.match(panel, /remote\.status === "ready" && remote\.failed\.length/);
  assert.match(panel, /settings\.sklm\.remotePartial/);
  assert.match(panel, /failureKinds: result\.failureKinds \?\? \{\}/);
  // The whole-query rejection used to be discarded with no trace at all.
  assert.match(panel, /queryError: skillMarketFailureDetail\(error\)/);
});

test("failed and partial skill catalog searches can be retried", () => {
  assert.match(panel, /const \[catalogRetry, setCatalogRetry\] = useState\(0\)/);
  assert.match(panel, /\[search, sources, catalogRetry\]/);
  assert.match(panel, /const retryCatalog = \(\) => \{/);
  assert.match(panel, /settings\.sklm\.retryCatalog/);
  assert.match(panel, /setRemote\(\(current\) => \(\{ \.\.\.current, status: "loading" \}\)\)/);
});

test("a failed query keeps the last successful list with its timestamp", () => {
  // Issue #1692 follow-up: a source failure used to drop the whole list back to
  // built-in picks, discarding what the user was just looking at. The panel now
  // keeps the last successful result and says when it was taken.
  assert.match(panel, /useState<MarketSnapshot<MarketItem> \| null>\(null\)/);
  assert.match(panel, /snapshotMatchesQuery\(snapshot, search\)/);
  assert.match(panel, /setSnapshot\(\{ entries, at: Date\.now\(\), query \}\)/);
  // The fallback list answers the current query only, and only while the
  // query itself has nothing to show — an empty `ready` answer is real.
  assert.match(
    panel,
    /const showingSnapshot =\s*\n\s*snapshotForQuery != null &&\s*\n\s*remote\.entries\.length === 0 &&\s*\n\s*\(remote\.status === "error" \|\| remote\.status === "loading"\)/,
  );
  assert.match(panel, /displayEntries\.filter\(matches\)/);
  assert.match(panel, /settings\.sklm\.remoteStaleSnapshot/);
  assert.match(panel, /snapshotTimeText\(\s*\n?\s*snapshotForQuery\.at/);
  // An empty response must not overwrite the snapshot: that would pin the
  // pane to an empty list after one successful-but-empty query.
  assert.match(panel, /if \(entries\.length > 0\) \{\s*\n\s*setSnapshot\(/);
});

test("a resolver with no answer is explained as that, not as an address check", () => {
  // Issue #419, second round: one `policy` bucket meant a local resolver that
  // answered nothing produced the same sentence as an address the guard had
  // judged — "blocked by the app's address check" — for a check that had never
  // reached a verdict. The two now carry different copy and different hints.
  assert.match(
    panel,
    /if \(hasUnresolvedFailure\(remote\.failureKinds\)\) return t\("settings\.sklm\.remoteErrorUnresolved"\)/,
  );
  assert.match(panel, /settings\.sklm\.dnsHint/);
  assert.match(
    panel,
    /kind === "unresolved"\s*\? "settings\.sklm\.previewResolveError"/,
  );
  // The reason itself is announced by the toast host: the sheet keeps its
  // recovery hints, its retry control and the host's own detail, and has no
  // error row left occupying the preview field.
  assert.match(panel, /showToast\(\s*t\(\s*kind === "policy"/);
  assert.doesNotMatch(panel, /sklm-note is-error/);
  assert.match(panel, /onClick=\{retryPreview\}/);
  // The refusal names the host it is about, not just the source label: a policy
  // refusal is a statement about one address.
  assert.match(panel, /settings\.sklm\.failureSourceHost/);
  assert.match(panel, /remote\.failureDetails\[name\]\?\.host/);
  // And the host reaches the state from the IPC result.
  assert.match(panel, /failureDetails: result\.failureDetails \?\? \{\}/);
  // The retry loading state preserves the last host details until the request resolves.
  assert.match(panel, /setRemote\(\(current\) => \(\{ \.\.\.current, status: "loading" \}\)\)/);
});

test("a proxy fake-IP gets its own copy and names the address it came from", () => {
  // Issue #419, third round: Clash's `198.18.0.0/15` is refused as a non-public
  // address, but it is the proxy's placeholder for the name, not the target's
  // own address. The panel must say so — and name both the host and the address,
  // because `198.18.0.1` is what a user recognises as fake-IP mode.
  assert.match(
    panel,
    /if \(hasFakeIpFailure\(remote\.failureKinds\)\) return t\("settings\.sklm\.remoteErrorFakeIp"\)/,
  );
  assert.match(
    panel,
    /kind === "fake-ip"\s*\? "settings\.sklm\.previewFakeIpError"/,
  );
  assert.match(panel, /settings\.sklm\.fakeIpHint/);
  assert.match(panel, /settings\.sklm\.fakeIpHintPlain/);
  // The hint names what actually happened: which host, and which address the
  // local proxy answered with.
  assert.match(panel, /const fakeIpFailureText = \(\) => \{/);
  assert.match(
    panel,
    /t\("settings\.sklm\.fakeIpHint", \{ host: detail\.host, address: detail\.address \}\)/,
  );
  assert.match(panel, /remote\.failureKinds\[entry\] === "fake-ip"/);
  // The address has to survive the state type, or the hint above cannot read it.
  assert.match(panel, /address\?: string/);
});

test("every shipped locale carries the new skill market strings", async () => {
  const { readFile } = await import("node:fs/promises");
  const ids = ["en", "zh-CN", "zh-TW", "de", "es", "fr", "ko", "tr", "pt-BR"];
  const keys = [
    "previewError",
    "previewPolicyError",
    "previewResolveError",
    "previewFakeIpError",
    "proxyHint",
    "dnsHint",
    "fakeIpHint",
    "fakeIpHintPlain",
    "failureDetail",
    "retryPreview",
    "retryCatalog",
    "remoteErrorPolicy",
    "remoteErrorUnresolved",
    "remoteErrorFakeIp",
    "remoteErrorQuery",
    "remotePartial",
    "failureSourceHost",
    "remoteStaleSnapshot",
  ];
  for (const id of ids) {
    const locale = await readFile(
      new URL(`../../../packages/i18n/src/locales/${id}/index.ts`, import.meta.url),
      "utf8",
    );
    for (const key of keys) {
      // Both layouts ship in these files: `key: "text"` on one line, and the
      // long strings broken after the colon. Either one carries the key.
      assert.match(locale, new RegExp(`${key}:\\s*"`), `${id} ${key}`);
    }
  }
});