From 09abc88d23f8adc84ecfe73049c1cbceb6c8b17b Mon Sep 17 00:00:00 2001 From: Sebastian Herrlinger Date: Mon, 30 Mar 2026 22:21:05 +0200 Subject: [PATCH] ensure pinned --- packages/opencode/test/bun.test.ts | 51 +++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/packages/opencode/test/bun.test.ts b/packages/opencode/test/bun.test.ts index d607ae4782..6e613d8009 100644 --- a/packages/opencode/test/bun.test.ts +++ b/packages/opencode/test/bun.test.ts @@ -1,6 +1,10 @@ -import { describe, expect, test } from "bun:test" +import { describe, expect, spyOn, test } from "bun:test" import fs from "fs/promises" import path from "path" +import { BunProc } from "../src/bun" +import { PackageRegistry } from "../src/bun/registry" +import { Global } from "../src/global" +import { Process } from "../src/util/process" describe("BunProc registry configuration", () => { test("should not contain hardcoded registry parameters", async () => { @@ -51,3 +55,48 @@ describe("BunProc registry configuration", () => { } }) }) + +describe("BunProc install pinning", () => { + test("uses pinned cache without touching registry", async () => { + const pkg = `pin-test-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}` + const ver = "1.2.3" + const mod = path.join(Global.Path.cache, "node_modules", pkg) + const data = path.join(Global.Path.cache, "package.json") + + await fs.mkdir(mod, { recursive: true }) + await Bun.write(path.join(mod, "package.json"), JSON.stringify({ name: pkg, version: ver }, null, 2)) + + const src = await fs.readFile(data, "utf8").catch(() => "") + const json = src ? ((JSON.parse(src) as { dependencies?: Record }) ?? {}) : {} + const deps = json.dependencies ?? {} + deps[pkg] = ver + await Bun.write(data, JSON.stringify({ ...json, dependencies: deps }, null, 2)) + + const stale = spyOn(PackageRegistry, "isOutdated").mockImplementation(async () => { + throw new Error("unexpected registry check") + }) + const run = spyOn(Process, "run").mockImplementation(async () => { + throw new Error("unexpected process.run") + }) + + try { + const out = await BunProc.install(pkg, ver) + expect(out).toBe(mod) + expect(stale).not.toHaveBeenCalled() + expect(run).not.toHaveBeenCalled() + } finally { + stale.mockRestore() + run.mockRestore() + + await fs.rm(mod, { recursive: true, force: true }) + const end = await fs + .readFile(data, "utf8") + .then((item) => JSON.parse(item) as { dependencies?: Record }) + .catch(() => undefined) + if (end?.dependencies) { + delete end.dependencies[pkg] + await Bun.write(data, JSON.stringify(end, null, 2)) + } + } + }) +})