Files
cc-switch/tests/lib/skillErrorReason.test.ts
Jasonandwc7086 ef9467ad20 feat(skills): read private skill repos with per-host access tokens
Skill repos on GitHub, GitLab and Gitea/Forgejo could only be read
anonymously, so private repos failed with a bare 404. Users can now
save an access token per host in the repo manager; discovery, install,
update checks and updates send it as `Authorization: Bearer`.

- Tokens are keyed by host, not by repo: `skill_repos` syncs across
  devices and its key (owner, name) carries no host, so binding a token
  to a row could send it to whatever host a synced row names.
- Storage stays on the device and out of cloud sync: macOS keychain via
  /usr/bin/security, Windows Credential Manager, and a 0600 index file
  elsewhere. The index (~/.cc-switch/skill-repo-tokens.json) lists hosts
  only; an unreadable index falls back to anonymous downloads.
- GitHub with a token goes through api.github.com/repos/.../zipball/
  refs/heads/<branch>; the plain branch name would also match a tag of
  the same name.
- Token-bearing requests use an https-only client: reqwest keeps the
  Authorization header on a same-host, same-port https->http redirect.
- Gitea/Forgejo platform probes carry the token (login-required
  instances answer 403 anonymously); saving or removing a token
  re-probes the host and drops its cached discovery results. A
  credential epoch keeps an in-flight probe from caching a verdict made
  under the old identity.
- Download errors now tell a missing token from a rejected one
  (http401Token / http403Token / http404Token / httpAuthRequired), in all
  eight locales.

Verified end to end on macOS against a private GitHub repo with a
fine-grained token (Contents: Read-only).

Refs #9666

Co-authored-by: wc7086 <74053663+wc7086@users.noreply.github.com>
2026-11-18 23:36:13 -08:00

65 lines
2.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type { TFunction } from "i18next";
import { skillErrorReason } from "@/lib/errors/skillErrorParser";
const t = ((key: string, options?: Record<string, unknown>) =>
options && Object.keys(options).length
? `${key}:${JSON.stringify(options)}`
: key) as unknown as TFunction;
describe("skillErrorReason", () => {
it("uses the HTTP explanation for rate-limited downloads", () => {
const error = JSON.stringify({
code: "DOWNLOAD_FAILED",
context: { status: "403" },
suggestion: "http403",
});
expect(skillErrorReason(error, t)).toBe("skills.error.http403");
});
it("explains token problems instead of a bare HTTP status (#9666)", () => {
for (const suggestion of [
"http401Token",
"http403Token",
"http404Token",
"httpAuthRequired",
]) {
const error = JSON.stringify({
code: "DOWNLOAD_FAILED",
context: { status: "401" },
suggestion,
});
expect(skillErrorReason(error, t)).toBe(`skills.error.${suggestion}`);
}
});
it("uses the error code text with its context otherwise", () => {
const error = JSON.stringify({
code: "DOWNLOAD_TIMEOUT",
context: { owner: "a", name: "b", timeout: "60" },
suggestion: "checkNetwork",
});
expect(skillErrorReason(error, t)).toBe(
'skills.error.downloadTimeout:{"owner":"a","name":"b","timeout":"60"}',
);
});
it("tells an oversized download apart from the extraction limit", () => {
// #8421:下载体超限曾复用 ARCHIVE_TOO_LARGE,被说成「解压后超过上限」
const error = JSON.stringify({
code: "ARCHIVE_DOWNLOAD_TOO_LARGE",
context: { limit_mb: "512" },
});
expect(skillErrorReason(error, t)).toBe(
'skills.error.archiveDownloadTooLarge:{"limit_mb":"512"}',
);
});
it("keeps the first line of an unstructured error", () => {
expect(skillErrorReason("connection reset\nstack…", t)).toBe(
"connection reset",
);
expect(skillErrorReason("", t)).toBe("common.error");
});
});