a98dc285a3
The CI gate failed in several non-overlapping ways once the full coding-agent suite ran here: tests wrote into the real $HOME (`/srv/agent-home`) which is read-only, a fixture rotated stored Anthropic API keys but Settings reloaded the user models.yml and shadowed them, an OAuth callback server bound to `hostname:"localhost"` (loopback unreachable in this runtime), a built-in tool metadata assertion only saw `github` when `gh` was installed, and the GithubTool `pr_checkout` worktree assertions assumed `~/.omp/wt` but `XDG_DATA_HOME` redirected `getWorktreesDir()` to `$XDG_DATA_HOME/omp/wt`. Fixes: - `packages/ai/src/registry/oauth/callback-server.ts`: drop `hostname: "localhost"` when no caller-supplied hostname overrides it. Bun on Linux refused inbound connections to the listener when bound explicitly to `localhost`; defaulting to Bun.serves default binding restores loopback connectivity. - `packages/coding-agent/test/status-line-path.test.ts`: route the `~/Projects` fixtures through a writable temp home (spy `os.homedir()`), housed under the repo `.wt/` worktree scratch so the temp home is not classified as a status-line scratch root. - `packages/coding-agent/test/skills.test.ts`: ditto for the `~/.pi-skills-test-*` mkdtemp in the tilde-expansion test. - `packages/coding-agent/test/marketplace/project-scope.test.ts`: stop writing `~/.git`; build the entire home-dir guard fixture in a temp dir and spy `os.homedir()`. - `packages/coding-agent/test/oauth-flow.test.ts`: wrap each callback fetch in a brief retry so the simulated browser redirect tolerates the few-ms gap before the Bun callback server starts accepting connections. - `packages/coding-agent/test/tools/gh.test.ts`: extend `setupTempHome()` to clear `XDG_DATA_HOME`/`XDG_STATE_HOME`/`XDG_CACHE_HOME` for the duration of the test so the rebuilt dirs resolver routes `getWorktreesDir()` back through the spied home, then restore them on cleanup. - `packages/coding-agent/test/tool-discovery/initial-tools.test.ts`: instantiate `GithubTool` directly in the metadata fixture so the assertion runs even when `gh` is unavailable (GithubTool.createIf returns null without `gh`). - `packages/coding-agent/test/agent-session-retry-cap.test.ts`: pass an isolated `models.yml` path to `ModelRegistry` so the two-Anthropic-key fixture is the authoritative credential source instead of any user-level command-backed Anthropic key. Verification: - `bun check` → passed - `bun run test` (full coding-agent suite, 4 buckets, all chunks) → 0 fails Fixes #3639
235 lines
7.8 KiB
TypeScript
235 lines
7.8 KiB
TypeScript
import { afterEach, beforeAll, describe, expect, it, vi } from "bun:test";
|
|
import * as fs from "node:fs";
|
|
import * as os from "node:os";
|
|
import * as path from "node:path";
|
|
import type { SegmentContext } from "@oh-my-pi/pi-coding-agent/modes/components/status-line/segments";
|
|
import { renderSegment } from "@oh-my-pi/pi-coding-agent/modes/components/status-line/segments";
|
|
import { initTheme, theme } from "@oh-my-pi/pi-coding-agent/modes/theme/theme";
|
|
import { getProjectDir, removeSyncWithRetries, setProjectDir } from "@oh-my-pi/pi-utils";
|
|
|
|
const originalProjectDir = getProjectDir();
|
|
beforeAll(async () => {
|
|
await initTheme();
|
|
});
|
|
|
|
function createPathContext(): SegmentContext {
|
|
return {
|
|
session: {
|
|
state: {},
|
|
isFastModeEnabled: () => false,
|
|
modelRegistry: { isUsingOAuth: () => false },
|
|
sessionManager: undefined,
|
|
} as unknown as SegmentContext["session"],
|
|
width: 120,
|
|
options: {
|
|
path: {
|
|
abbreviate: false,
|
|
maxLength: 120,
|
|
stripWorkPrefix: true,
|
|
},
|
|
},
|
|
planMode: null,
|
|
loopMode: null,
|
|
goalMode: null,
|
|
collab: null,
|
|
usageStats: {
|
|
input: 0,
|
|
output: 0,
|
|
cacheRead: 0,
|
|
cacheWrite: 0,
|
|
premiumRequests: 0,
|
|
cost: 0,
|
|
tokensPerSecond: null,
|
|
},
|
|
contextPercent: 0,
|
|
contextTokens: 0,
|
|
contextWindow: 0,
|
|
autoCompactEnabled: false,
|
|
subagentCount: 0,
|
|
sessionStartTime: Date.now(),
|
|
activeRepo: null,
|
|
git: {
|
|
branch: null,
|
|
status: null,
|
|
pr: null,
|
|
},
|
|
usage: null,
|
|
};
|
|
}
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
setProjectDir(originalProjectDir);
|
|
});
|
|
|
|
function expectContentToContainPath(content: string, expected: string): void {
|
|
if (process.platform === "win32") {
|
|
expect(content.toLowerCase()).toContain(expected.toLowerCase());
|
|
return;
|
|
}
|
|
expect(content).toContain(expected);
|
|
}
|
|
|
|
function createFakeHome(): { home: string; projectsRoot: string } {
|
|
const homeRoot = path.join(originalProjectDir, ".wt");
|
|
fs.mkdirSync(homeRoot, { recursive: true });
|
|
const home = fs.mkdtempSync(path.join(homeRoot, "omp-status-line-home-"));
|
|
const projectsRoot = path.join(home, "Projects");
|
|
fs.mkdirSync(projectsRoot, { recursive: true });
|
|
vi.spyOn(os, "homedir").mockReturnValue(home);
|
|
return { home, projectsRoot };
|
|
}
|
|
|
|
describe("status line path segment", () => {
|
|
it("strips the Projects root for symlink-equivalent aliases", () => {
|
|
if (process.platform === "win32") return;
|
|
|
|
const { home, projectsRoot } = createFakeHome();
|
|
|
|
const realProjectDir = fs.mkdtempSync(path.join(projectsRoot, "omp-status-line-"));
|
|
const nestedDir = path.join(realProjectDir, "nested");
|
|
const aliasRoot = fs.mkdtempSync(path.join(os.tmpdir(), "omp-status-line-alias-"));
|
|
const homeAlias = path.join(aliasRoot, "home-link");
|
|
|
|
try {
|
|
fs.mkdirSync(nestedDir, { recursive: true });
|
|
fs.symlinkSync(home, homeAlias, "dir");
|
|
|
|
const aliasedDir = path.join(homeAlias, "Projects", path.basename(realProjectDir), "nested");
|
|
setProjectDir(aliasedDir);
|
|
|
|
const rendered = renderSegment("path", createPathContext());
|
|
const expectedRelative = `${path.basename(realProjectDir)}${path.sep}nested`;
|
|
|
|
expect(rendered.visible).toBe(true);
|
|
expect(rendered.content).toContain(expectedRelative);
|
|
expect(rendered.content).not.toContain("home-link");
|
|
expect(rendered.content).not.toContain(`${path.sep}Projects${path.sep}`);
|
|
} finally {
|
|
setProjectDir(originalProjectDir);
|
|
removeSyncWithRetries(aliasRoot);
|
|
removeSyncWithRetries(realProjectDir);
|
|
removeSyncWithRetries(home);
|
|
}
|
|
});
|
|
|
|
it("strips the scratch root and shows only the trailing folder inside the OS tmp dir", () => {
|
|
const scratchDir = fs.mkdtempSync(path.join(os.tmpdir(), "omp-status-line-scratch-"));
|
|
try {
|
|
setProjectDir(scratchDir);
|
|
|
|
const rendered = renderSegment("path", createPathContext());
|
|
expect(rendered.visible).toBe(true);
|
|
expect(rendered.content).toContain(theme.icon.scratchFolder);
|
|
expect(rendered.content).not.toContain(theme.icon.folder);
|
|
// Display is just the scratch-relative tail — no leading tmpdir, no ancestor segments.
|
|
expectContentToContainPath(rendered.content, path.basename(getProjectDir()));
|
|
expect(rendered.content).not.toContain(os.tmpdir());
|
|
} finally {
|
|
setProjectDir(originalProjectDir);
|
|
removeSyncWithRetries(scratchDir);
|
|
}
|
|
});
|
|
|
|
it("keeps nested subpaths visible under a scratch root", () => {
|
|
const scratchDir = fs.mkdtempSync(path.join(os.tmpdir(), "omp-status-line-scratch-nest-"));
|
|
const nested = path.join(scratchDir, "sub", "deep");
|
|
fs.mkdirSync(nested, { recursive: true });
|
|
try {
|
|
setProjectDir(nested);
|
|
|
|
const rendered = renderSegment("path", createPathContext());
|
|
const tail = `${path.basename(path.dirname(path.dirname(getProjectDir())))}${path.sep}sub${path.sep}deep`;
|
|
expect(rendered.content).toContain(theme.icon.scratchFolder);
|
|
expectContentToContainPath(rendered.content, tail);
|
|
expect(rendered.content).not.toContain(os.tmpdir());
|
|
} finally {
|
|
setProjectDir(originalProjectDir);
|
|
removeSyncWithRetries(scratchDir);
|
|
}
|
|
});
|
|
|
|
it("keeps the folder icon for scratch paths when stripWorkPrefix is disabled", () => {
|
|
const scratchDir = fs.mkdtempSync(path.join(os.tmpdir(), "omp-status-line-scratch-noprefix-"));
|
|
try {
|
|
setProjectDir(scratchDir);
|
|
|
|
const ctx = createPathContext();
|
|
ctx.options.path = { ...ctx.options.path, stripWorkPrefix: false };
|
|
const rendered = renderSegment("path", ctx);
|
|
expect(rendered.visible).toBe(true);
|
|
expect(rendered.content).toContain(theme.icon.folder);
|
|
expect(rendered.content).not.toContain(theme.icon.scratchFolder);
|
|
} finally {
|
|
setProjectDir(originalProjectDir);
|
|
removeSyncWithRetries(scratchDir);
|
|
}
|
|
});
|
|
|
|
it("keeps the folder icon for paths outside any scratch root", () => {
|
|
const { home, projectsRoot } = createFakeHome();
|
|
const realProjectDir = fs.mkdtempSync(path.join(projectsRoot, "omp-status-line-real-"));
|
|
try {
|
|
setProjectDir(realProjectDir);
|
|
|
|
const rendered = renderSegment("path", createPathContext());
|
|
expect(rendered.visible).toBe(true);
|
|
expect(rendered.content).toContain(theme.icon.folder);
|
|
expect(rendered.content).not.toContain(theme.icon.scratchFolder);
|
|
} finally {
|
|
setProjectDir(originalProjectDir);
|
|
removeSyncWithRetries(realProjectDir);
|
|
removeSyncWithRetries(home);
|
|
}
|
|
});
|
|
|
|
it("renders the active nested repo suffix after the parent cwd", () => {
|
|
const parentDir = fs.mkdtempSync(path.join(os.tmpdir(), "omp-status-line-parent-"));
|
|
const repoDir = path.join(parentDir, "pr-workspace");
|
|
fs.mkdirSync(repoDir);
|
|
try {
|
|
setProjectDir(parentDir);
|
|
const ctx = createPathContext();
|
|
ctx.activeRepo = {
|
|
cwd: parentDir,
|
|
repoRoot: repoDir,
|
|
relativeRepoRoot: "pr-workspace",
|
|
source: "single-direct-child-repo",
|
|
};
|
|
|
|
const rendered = renderSegment("path", ctx);
|
|
const expected = `${path.basename(getProjectDir())} ↳ pr-workspace`;
|
|
expect(rendered.visible).toBe(true);
|
|
expectContentToContainPath(rendered.content, expected);
|
|
expect(rendered.content).not.toContain(os.tmpdir());
|
|
} finally {
|
|
setProjectDir(originalProjectDir);
|
|
removeSyncWithRetries(parentDir);
|
|
}
|
|
});
|
|
|
|
it("keeps the active nested repo suffix visible when the parent path is truncated", () => {
|
|
const parentDir = fs.mkdtempSync(path.join(os.tmpdir(), "omp-status-line-parent-"));
|
|
const repoDir = path.join(parentDir, "pr-workspace");
|
|
fs.mkdirSync(repoDir);
|
|
try {
|
|
setProjectDir(parentDir);
|
|
const ctx = createPathContext();
|
|
ctx.options.path = { abbreviate: false, maxLength: 4, stripWorkPrefix: true };
|
|
ctx.activeRepo = {
|
|
cwd: parentDir,
|
|
repoRoot: repoDir,
|
|
relativeRepoRoot: "pr-workspace",
|
|
source: "single-direct-child-repo",
|
|
};
|
|
|
|
const rendered = renderSegment("path", ctx);
|
|
expect(rendered.visible).toBe(true);
|
|
expect(rendered.content).toContain("↳ pr-workspace");
|
|
} finally {
|
|
setProjectDir(originalProjectDir);
|
|
removeSyncWithRetries(parentDir);
|
|
}
|
|
});
|
|
});
|