Files
oh-my-pi/packages/coding-agent/test/discovery/builtin-rules-md.test.ts
T
Ogrodev b99039ded2 fix(coding-agent): profile-scope native config discovery and load symlinked extension dirs
Native user-level config discovery (MCP, skills, rules, slash commands, prompts, instructions, hooks, tools, settings, extensions, and the top-level SYSTEM.md/RULES.md/AGENTS.md) now resolves the user scope through getAgentDir() in builtin.ts, omp-extension-roots.ts, and the discovery-layer getUserPath() helper. A named profile sees only its own ~/.omp/profiles/<name>/agent config instead of the default profile's ~/.omp/agent leaking into every profile, matching the /mcp config writer and getMCPConfigPath("user").

discoverExtensionModulePaths now detects top-level symlinked directories that the native glob skips (follow_links=false) and synthesizes their index/package.json entry-point matches, so an extension shared across profiles via a symlink loads like a real directory. Symlinked extension files were already handled.

cli: check --tiny-worker on the profile-flag-stripped resolvedArgv, matching the adjacent --smoke-test check and launch routing.
2026-06-04 15:07:55 -03:00

121 lines
4.8 KiB
TypeScript

/**
* Regression test for #1266:
* `RULES.md` (singular, top-level) MUST be loaded as a sticky always-apply rule
* from both `~/.omp/agent/RULES.md` (user) and the nearest `.omp/RULES.md`
* (project, walked up from cwd to repoRoot).
*
* Calls the native provider's `load` directly with the agent dir pointed at a
* tempdir (via setAgentDir) so the user scope can be staged in isolation.
*/
import { afterEach, beforeEach, expect, test } from "bun:test";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { getCapability } from "@oh-my-pi/pi-coding-agent/capability";
import { clearCache } from "@oh-my-pi/pi-coding-agent/capability/fs";
import { type Rule, ruleCapability } from "@oh-my-pi/pi-coding-agent/capability/rule";
import type { LoadContext } from "@oh-my-pi/pi-coding-agent/capability/types";
// Register all discovery providers as a side effect.
import "@oh-my-pi/pi-coding-agent/discovery";
import { getConfigRootDir, setAgentDir } from "@oh-my-pi/pi-utils";
let tempDir: string;
let home: string;
let project: string;
const originalAgentDirEnv = process.env.PI_CODING_AGENT_DIR;
const fallbackAgentDir = path.join(getConfigRootDir(), "agent");
function writeFile(filePath: string, content: string): void {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, content);
}
async function loadNativeRules(ctx: LoadContext): Promise<Rule[]> {
const cap = getCapability(ruleCapability.id);
if (!cap) throw new Error("rules capability missing");
const native = cap.providers.find(p => p.id === "native");
if (!native) throw new Error("native rules provider missing");
const result = await (native.load as (ctx: LoadContext) => Promise<{ items: Rule[] }>)(ctx);
return result.items;
}
beforeEach(() => {
clearCache();
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-rules-md-"));
home = path.join(tempDir, "home");
project = path.join(tempDir, "project");
fs.mkdirSync(home, { recursive: true });
fs.mkdirSync(project, { recursive: true });
fs.mkdirSync(path.join(project, ".git"), { recursive: true });
setAgentDir(path.join(home, ".omp", "agent"));
});
afterEach(() => {
clearCache();
if (originalAgentDirEnv) {
setAgentDir(originalAgentDirEnv);
} else {
setAgentDir(fallbackAgentDir);
delete process.env.PI_CODING_AGENT_DIR;
}
fs.rmSync(tempDir, { recursive: true, force: true });
});
test("user ~/.omp/agent/RULES.md becomes an alwaysApply rule", async () => {
writeFile(
path.join(home, ".omp", "agent", "RULES.md"),
"**CRITICAL**: You _MUST_ use beads task tracker for any project\n",
);
const rules = await loadNativeRules({ cwd: project, home, repoRoot: project });
const userRule = rules.find(r => r._source.level === "user" && r.name === "RULES");
expect(userRule).toBeDefined();
expect(userRule?.alwaysApply).toBe(true);
expect(userRule?.content).toContain("beads task tracker");
});
test("project .omp/RULES.md becomes an alwaysApply rule", async () => {
writeFile(path.join(project, ".omp", "RULES.md"), "# Project rule\nAlways say hi.\n");
const rules = await loadNativeRules({ cwd: project, home, repoRoot: project });
const projectRule = rules.find(r => r._source.level === "project" && r.name === "RULES");
expect(projectRule).toBeDefined();
expect(projectRule?.alwaysApply).toBe(true);
expect(projectRule?.content).toContain("Always say hi.");
});
test("project RULES.md is found walking up from a sub-package cwd", async () => {
const subPkg = path.join(project, "packages", "app");
fs.mkdirSync(subPkg, { recursive: true });
writeFile(path.join(project, ".omp", "RULES.md"), "# Repo-wide sticky rule\n");
const rules = await loadNativeRules({ cwd: subPkg, home, repoRoot: project });
const projectRule = rules.find(r => r._source.level === "project" && r.name === "RULES");
expect(projectRule).toBeDefined();
expect(projectRule?.alwaysApply).toBe(true);
expect(projectRule?.path).toBe(path.join(project, ".omp", "RULES.md"));
});
test("alwaysApply is forced even when frontmatter says false", async () => {
writeFile(path.join(home, ".omp", "agent", "RULES.md"), "---\nalwaysApply: false\n---\nStick around anyway.\n");
const rules = await loadNativeRules({ cwd: project, home, repoRoot: project });
const userRule = rules.find(r => r._source.level === "user" && r.name === "RULES");
expect(userRule?.alwaysApply).toBe(true);
expect(userRule?.content).toContain("Stick around anyway.");
});
test("absent RULES.md does not produce a rule", async () => {
// No RULES.md anywhere — only a sibling .omp/rules/ to make sure the directory exists.
writeFile(path.join(home, ".omp", "agent", "rules", "other.md"), "# Unrelated rule\n");
const rules = await loadNativeRules({ cwd: project, home, repoRoot: project });
expect(rules.find(r => r.name === "RULES")).toBeUndefined();
});