-
Notifications
You must be signed in to change notification settings - Fork 8.2k
feat(bun): track provider packages for automatic cleanup #10275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jerome-benoit
wants to merge
17
commits into
anomalyco:dev
Choose a base branch
from
jerome-benoit:feat/provider-package-tracking
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+262
−68
Open
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
beb523a
feat(bun): track provider packages for automatic cleanup
jerome-benoit 624dea8
Merge branch 'dev' into feat/provider-package-tracking
jerome-benoit 2a89e1b
fix(bun): address review feedback for provider tracking
jerome-benoit 43fc90e
fix: install new package before removing old to prevent broken state
jerome-benoit 25a764c
fix: always reinstall when version is latest
jerome-benoit b422998
test: add edge cases for package.json states
jerome-benoit 3891358
chore: clean up comments
jerome-benoit 817fa1d
Merge branch 'dev' into feat/provider-package-tracking
jerome-benoit 0607311
test(bun): verify exact semver version after install
jerome-benoit 7d198b4
test(bun): harmonize version checks across all tests
jerome-benoit ff24cbe
Merge branch 'dev' into feat/provider-package-tracking
jerome-benoit 12b3dfa
Merge branch 'dev' into feat/provider-package-tracking
jerome-benoit ca70279
Merge branch 'dev' into feat/provider-package-tracking
jerome-benoit d463c96
Merge branch 'dev' into feat/provider-package-tracking
jerome-benoit 0d2572d
Merge branch 'dev' into feat/provider-package-tracking
jerome-benoit 56c719e
Merge branch 'dev' into feat/provider-package-tracking
jerome-benoit 74d2cc6
Merge branch 'dev' into feat/provider-package-tracking
jerome-benoit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,169 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { describe, expect, test, beforeEach, afterEach } from "bun:test" | ||
| import fs from "fs/promises" | ||
| import path from "path" | ||
| import os from "os" | ||
|
|
||
| describe("BunProc registry configuration", () => { | ||
| test("should not contain hardcoded registry parameters", async () => { | ||
| // Read the bun/index.ts file | ||
| const bunIndexPath = path.join(__dirname, "../src/bun/index.ts") | ||
| const content = await fs.readFile(bunIndexPath, "utf-8") | ||
|
|
||
| // Verify that no hardcoded registry is present | ||
| expect(content).not.toContain("--registry=") | ||
| expect(content).not.toContain("hasNpmRcConfig") | ||
| expect(content).not.toContain("NpmRc") | ||
| }) | ||
|
|
||
| test("should use Bun's default registry resolution", async () => { | ||
| // Read the bun/index.ts file | ||
| test("should have correct bun add command structure", async () => { | ||
| const bunIndexPath = path.join(__dirname, "../src/bun/index.ts") | ||
| const content = await fs.readFile(bunIndexPath, "utf-8") | ||
|
|
||
| // Verify that it uses Bun's default resolution | ||
| expect(content).toContain("Bun's default registry resolution") | ||
| expect(content).toContain("Bun will use them automatically") | ||
| expect(content).toContain("No need to pass --registry flag") | ||
| }) | ||
|
|
||
| test("should have correct command structure without registry", async () => { | ||
| // Read the bun/index.ts file | ||
| const bunIndexPath = path.join(__dirname, "../src/bun/index.ts") | ||
| const content = await fs.readFile(bunIndexPath, "utf-8") | ||
|
|
||
| // Extract the install function | ||
| const installFunctionMatch = content.match(/export async function install[\s\S]*?^ }/m) | ||
| expect(installFunctionMatch).toBeTruthy() | ||
|
|
||
| if (installFunctionMatch) { | ||
| const installFunction = installFunctionMatch[0] | ||
|
|
||
| // Verify expected arguments are present | ||
| expect(installFunction).toContain('"add"') | ||
| expect(installFunction).toContain('"--force"') | ||
| expect(installFunction).toContain('"--exact"') | ||
| expect(installFunction).toContain('"--cwd"') | ||
| expect(installFunction).toContain("Global.Path.cache") | ||
| expect(installFunction).toContain('pkg + "@" + version') | ||
|
|
||
| // Verify no registry argument is added | ||
| expect(installFunction).not.toContain('"--registry"') | ||
| expect(installFunction).not.toContain('args.push("--registry') | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| describe("BunProc.install provider tracking", () => { | ||
| let tempDir: string | ||
| let originalCache: string | undefined | ||
|
|
||
| beforeEach(async () => { | ||
| tempDir = path.join(os.tmpdir(), "opencode-bun-test-" + Math.random().toString(36).slice(2)) | ||
| await fs.mkdir(tempDir, { recursive: true }) | ||
| originalCache = process.env.OPENCODE_TEST_CACHE | ||
| process.env.OPENCODE_TEST_CACHE = tempDir | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| if (originalCache === undefined) { | ||
| delete process.env.OPENCODE_TEST_CACHE | ||
| } else { | ||
| process.env.OPENCODE_TEST_CACHE = originalCache | ||
| } | ||
| await fs.rm(tempDir, { recursive: true, force: true }).catch(() => {}) | ||
| }) | ||
|
|
||
| async function readPkgJson() { | ||
| return JSON.parse(await fs.readFile(path.join(tempDir, "package.json"), "utf-8")) | ||
| } | ||
|
|
||
| async function pkgExists(pkg: string) { | ||
| return fs | ||
| .stat(path.join(tempDir, "node_modules", pkg)) | ||
| .then(() => true) | ||
| .catch(() => false) | ||
| } | ||
|
|
||
| test("should track provider in opencode.providers section", async () => { | ||
| const { BunProc } = await import("../src/bun") | ||
|
|
||
| await BunProc.install("zod", "latest", "anthropic") | ||
|
|
||
| const pkgJson = await readPkgJson() | ||
| expect(pkgJson.opencode?.providers?.anthropic).toBe("zod") | ||
| }) | ||
|
|
||
| test("should install package in node_modules", async () => { | ||
| const { BunProc } = await import("../src/bun") | ||
|
|
||
| await BunProc.install("zod", "latest", "anthropic") | ||
|
|
||
| expect(await pkgExists("zod")).toBe(true) | ||
| }) | ||
|
|
||
| test("should update tracking when provider switches packages", async () => { | ||
| const { BunProc } = await import("../src/bun") | ||
|
|
||
| await BunProc.install("zod", "latest", "anthropic") | ||
| let pkgJson = await readPkgJson() | ||
| expect(pkgJson.opencode?.providers?.anthropic).toBe("zod") | ||
|
|
||
| await BunProc.install("superstruct", "latest", "anthropic") | ||
| pkgJson = await readPkgJson() | ||
| expect(pkgJson.opencode?.providers?.anthropic).toBe("superstruct") | ||
| }) | ||
|
|
||
| test("should remove old package when provider switches", async () => { | ||
| const { BunProc } = await import("../src/bun") | ||
|
|
||
| await BunProc.install("zod", "latest", "anthropic") | ||
| expect(await pkgExists("zod")).toBe(true) | ||
|
|
||
| await BunProc.install("superstruct", "latest", "anthropic") | ||
|
|
||
| // Old package should be removed | ||
| expect(await pkgExists("zod")).toBe(false) | ||
| // New package should be installed | ||
| expect(await pkgExists("superstruct")).toBe(true) | ||
| }) | ||
|
|
||
| test("should remove old package even when new package is already cached", async () => { | ||
| const { BunProc } = await import("../src/bun") | ||
|
|
||
| // Install zod for provider-a | ||
| await BunProc.install("zod", "latest", "provider-a") | ||
| expect(await pkgExists("zod")).toBe(true) | ||
|
|
||
| // Install superstruct for provider-b (now superstruct is cached) | ||
| await BunProc.install("superstruct", "latest", "provider-b") | ||
| expect(await pkgExists("superstruct")).toBe(true) | ||
|
|
||
| // Switch provider-a from zod to superstruct (superstruct already cached) | ||
| await BunProc.install("superstruct", "latest", "provider-a") | ||
|
|
||
| // zod should be removed since provider-a no longer uses it | ||
| expect(await pkgExists("zod")).toBe(false) | ||
|
|
||
| // Tracking should be updated | ||
| const pkgJson = await readPkgJson() | ||
| expect(pkgJson.opencode?.providers?.["provider-a"]).toBe("superstruct") | ||
| expect(pkgJson.opencode?.providers?.["provider-b"]).toBe("superstruct") | ||
| }) | ||
|
|
||
| test("should not remove package if provider is not switching", async () => { | ||
| const { BunProc } = await import("../src/bun") | ||
|
|
||
| // Install zod for anthropic | ||
| await BunProc.install("zod", "latest", "anthropic") | ||
|
|
||
| // Install same package again (e.g., version check) | ||
| await BunProc.install("zod", "latest", "anthropic") | ||
|
|
||
| // Package should still exist | ||
| expect(await pkgExists("zod")).toBe(true) | ||
| }) | ||
|
|
||
| test("should work without providerID (backward compatible)", async () => { | ||
| const { BunProc } = await import("../src/bun") | ||
|
|
||
| await BunProc.install("zod", "latest") | ||
|
|
||
| expect(await pkgExists("zod")).toBe(true) | ||
| const pkgJson = await readPkgJson() | ||
| // No provider tracking when providerID not provided | ||
| expect(pkgJson.opencode?.providers).toBeUndefined() | ||
| }) | ||
|
|
||
| test("should track multiple providers independently", async () => { | ||
| const { BunProc } = await import("../src/bun") | ||
|
|
||
| await BunProc.install("zod", "latest", "anthropic") | ||
| await BunProc.install("superstruct", "latest", "openai") | ||
|
|
||
| const pkgJson = await readPkgJson() | ||
| expect(pkgJson.opencode?.providers?.anthropic).toBe("zod") | ||
| expect(pkgJson.opencode?.providers?.openai).toBe("superstruct") | ||
|
|
||
| expect(await pkgExists("zod")).toBe(true) | ||
| expect(await pkgExists("superstruct")).toBe(true) | ||
| }) | ||
| }) | ||
jerome-benoit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.