|
| 1 | +import test from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { projectRootIfInsideOmvState, resolveProjectRoot } from "../paths.js"; |
| 7 | +import { extractProjectRootOption } from "../commands/index.js"; |
| 8 | + |
| 9 | +test("projectRootIfInsideOmvState maps checkouts back to the owner project", () => { |
| 10 | + const root = "/tmp/research-root"; |
| 11 | + assert.equal( |
| 12 | + projectRootIfInsideOmvState(join(root, ".omv", "checkouts", "pkg")), |
| 13 | + root, |
| 14 | + ); |
| 15 | + assert.equal( |
| 16 | + projectRootIfInsideOmvState(join(root, ".omv", "findings")), |
| 17 | + root, |
| 18 | + ); |
| 19 | + assert.equal(projectRootIfInsideOmvState(root), undefined); |
| 20 | + assert.equal(projectRootIfInsideOmvState(join(root, "src")), undefined); |
| 21 | +}); |
| 22 | + |
| 23 | +test("resolveProjectRoot prefers OMV_PROJECT_ROOT over walk-up", async () => { |
| 24 | + const outer = await mkdtemp(join(tmpdir(), "omv-root-outer-")); |
| 25 | + const inner = await mkdtemp(join(tmpdir(), "omv-root-inner-")); |
| 26 | + try { |
| 27 | + await mkdir(join(outer, ".omv"), { recursive: true }); |
| 28 | + await mkdir(join(inner, ".omv"), { recursive: true }); |
| 29 | + const previous = process.env.OMV_PROJECT_ROOT; |
| 30 | + process.env.OMV_PROJECT_ROOT = inner; |
| 31 | + try { |
| 32 | + assert.equal(resolveProjectRoot(outer), inner); |
| 33 | + } finally { |
| 34 | + if (previous === undefined) { |
| 35 | + delete process.env.OMV_PROJECT_ROOT; |
| 36 | + } else { |
| 37 | + process.env.OMV_PROJECT_ROOT = previous; |
| 38 | + } |
| 39 | + } |
| 40 | + } finally { |
| 41 | + await rm(outer, { recursive: true, force: true }); |
| 42 | + await rm(inner, { recursive: true, force: true }); |
| 43 | + } |
| 44 | +}); |
| 45 | + |
| 46 | +test("resolveProjectRoot walks up to nearest .omv owner from a subdirectory", async () => { |
| 47 | + const project = await mkdtemp(join(tmpdir(), "omv-root-walk-")); |
| 48 | + try { |
| 49 | + await mkdir(join(project, ".omv", "findings"), { recursive: true }); |
| 50 | + await writeFile(join(project, ".omv", "index.json"), "{\"version\":1,\"findings\":[]}\n", "utf-8"); |
| 51 | + const nested = join(project, "src", "cli"); |
| 52 | + await mkdir(nested, { recursive: true }); |
| 53 | + const previous = process.env.OMV_PROJECT_ROOT; |
| 54 | + delete process.env.OMV_PROJECT_ROOT; |
| 55 | + delete process.env.OMV_ROOT; |
| 56 | + try { |
| 57 | + assert.equal(resolveProjectRoot(nested), project); |
| 58 | + } finally { |
| 59 | + if (previous !== undefined) { |
| 60 | + process.env.OMV_PROJECT_ROOT = previous; |
| 61 | + } |
| 62 | + } |
| 63 | + } finally { |
| 64 | + await rm(project, { recursive: true, force: true }); |
| 65 | + } |
| 66 | +}); |
| 67 | + |
| 68 | +test("resolveProjectRoot does not create nested workspaces under .omv/checkouts", async () => { |
| 69 | + const project = await mkdtemp(join(tmpdir(), "omv-root-checkout-")); |
| 70 | + try { |
| 71 | + const checkout = join(project, ".omv", "checkouts", "sanitize-url"); |
| 72 | + await mkdir(checkout, { recursive: true }); |
| 73 | + await mkdir(join(project, ".omv", "findings"), { recursive: true }); |
| 74 | + // Accidental nested workspace under the checkout must not win. |
| 75 | + await mkdir(join(checkout, ".omv", "findings"), { recursive: true }); |
| 76 | + const previous = process.env.OMV_PROJECT_ROOT; |
| 77 | + delete process.env.OMV_PROJECT_ROOT; |
| 78 | + delete process.env.OMV_ROOT; |
| 79 | + try { |
| 80 | + assert.equal(resolveProjectRoot(checkout), project); |
| 81 | + } finally { |
| 82 | + if (previous !== undefined) { |
| 83 | + process.env.OMV_PROJECT_ROOT = previous; |
| 84 | + } |
| 85 | + } |
| 86 | + } finally { |
| 87 | + await rm(project, { recursive: true, force: true }); |
| 88 | + } |
| 89 | +}); |
| 90 | + |
| 91 | +test("extractProjectRootOption strips --root forms", () => { |
| 92 | + assert.deepEqual(extractProjectRootOption(["--root", "/tmp/ws", "dashboard", "--json"]), { |
| 93 | + args: ["dashboard", "--json"], |
| 94 | + root: "/tmp/ws", |
| 95 | + }); |
| 96 | + assert.deepEqual(extractProjectRootOption(["findings", "list", "--root=/tmp/ws"]), { |
| 97 | + args: ["findings", "list"], |
| 98 | + root: "/tmp/ws", |
| 99 | + }); |
| 100 | + assert.equal(extractProjectRootOption(["--root"]).error, "--root requires a directory path"); |
| 101 | +}); |
0 commit comments