Repository navigation
fix: point the warehouse_list no-connections hint at the real config locations
#1559
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
587f79c
fix: point the `warehouse_list` no-connections hint at the real confi…
anandgupta42 a3c7274
fix: reload connection config edited after the first load and lead th…
anandgupta42 f27bdc6
fix: keep a refreshed connection config authoritative over stale in-m…
anandgupta42 55bf62b
fix: reload connection config only while the registry is empty
anandgupta42 434a319
fix: do not load another project's connections file on an empty-regis…
anandgupta42 10ba54c
fix: clarify `warehouse_list` hint and clear the test-injection flag …
anandgupta42 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
176 changes: 176 additions & 0 deletions
176
packages/opencode/test/altimate/warehouse-list-hint.test.ts
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 |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| import { describe, test, expect, beforeEach, afterEach } from "bun:test" | ||
| import * as fs from "fs" | ||
| import * as os from "os" | ||
| import * as path from "path" | ||
| import * as Registry from "../../src/altimate/native/connections/registry" | ||
| import { WarehouseListTool, noConnectionsMessage } from "../../src/altimate/tools/warehouse-list" | ||
| import { registerAll } from "../../src/altimate/native/connections/register" | ||
| import { SessionID, MessageID } from "../../src/session/schema" | ||
| import { initTool } from "./tool-fixture" | ||
|
|
||
| function toolCtx() { | ||
| return { | ||
| sessionID: SessionID.make("ses_test"), | ||
| messageID: MessageID.make("msg_test"), | ||
| callID: "call_test", | ||
| agent: "build", | ||
| abort: AbortSignal.any([]), | ||
| messages: [], | ||
| metadata: () => {}, | ||
| ask: async () => {}, | ||
| } as any | ||
| } | ||
|
|
||
| let project: string | ||
| let prevCwd: string | ||
| let ambientConnEnv: Record<string, string> | ||
|
|
||
| beforeEach(() => { | ||
| // Ambient connection env vars would make the empty-registry assertions depend on the caller's shell. | ||
| ambientConnEnv = {} | ||
| for (const [key, value] of Object.entries(process.env)) { | ||
| if (!key.startsWith(Registry.CONN_ENV_PREFIX) || value === undefined) continue | ||
| ambientConnEnv[key] = value | ||
| delete process.env[key] | ||
| } | ||
| prevCwd = process.cwd() | ||
| project = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "whl-hint-"))) | ||
| process.chdir(project) | ||
| Registry.reset() | ||
| registerAll() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| process.chdir(prevCwd) | ||
| Registry.reset() | ||
| fs.rmSync(project, { recursive: true, force: true }) | ||
| Object.assign(process.env, ambientConnEnv) | ||
| }) | ||
|
|
||
| describe("warehouse_list no-connections hint", () => { | ||
| test("names the paths and env prefix the registry reads, not .opencode", () => { | ||
| const msg = noConnectionsMessage() | ||
| expect(msg).toContain(Registry.globalConfigPath()) | ||
| expect(msg).toContain(Registry.localConfigPath()) | ||
| expect(msg).toContain(Registry.CONN_ENV_PREFIX) | ||
| expect(msg).toContain("warehouse_add") | ||
| expect(msg).toContain('"type": "postgres"') | ||
| expect(msg).not.toContain(".opencode/connections.json") | ||
| expect(Registry.localConfigPath()).toBe(path.join(project, ".altimate-code", "connections.json")) | ||
| }) | ||
|
|
||
| test("a connections.json written to the project path named by the hint is loaded", () => { | ||
| const named = Registry.localConfigPath() | ||
| expect(noConnectionsMessage()).toContain(named) | ||
| fs.mkdirSync(path.dirname(named), { recursive: true }) | ||
| fs.writeFileSync(named, JSON.stringify({ "hint-db": { type: "postgres", host: "localhost", database: "d" } })) | ||
| Registry.load() | ||
| expect(Registry.names()).toContain("hint-db") | ||
| }) | ||
|
|
||
| test("a connections.json at the legacy .opencode location is not loaded", () => { | ||
| fs.mkdirSync(path.join(project, ".opencode")) | ||
| fs.writeFileSync( | ||
| path.join(project, ".opencode", "connections.json"), | ||
| JSON.stringify({ "wrong-db": { type: "postgres", host: "localhost" } }), | ||
| ) | ||
| Registry.load() | ||
| expect(Registry.names()).not.toContain("wrong-db") | ||
| }) | ||
|
|
||
| test("the tool lists a connection created after an empty answer, in the same process", async () => { | ||
| const tool = await initTool(WarehouseListTool) | ||
| const ctx = toolCtx() | ||
| const first = await tool.execute({}, ctx) | ||
|
anandgupta42 marked this conversation as resolved.
|
||
| expect(first.output).toContain(Registry.localConfigPath()) | ||
|
|
||
| const named = Registry.localConfigPath() | ||
| fs.mkdirSync(path.dirname(named), { recursive: true }) | ||
| fs.writeFileSync(named, JSON.stringify({ "late-db": { type: "postgres", host: "localhost", database: "d" } })) | ||
|
|
||
| const second = await tool.execute({}, ctx) | ||
| expect(second.output).toContain("late-db") | ||
| // the lookup sql tools use resolves it too | ||
| expect(Registry.getConfig("late-db")?.type).toBe("postgres") | ||
| }) | ||
|
|
||
| test("an env var set after an empty answer is picked up", async () => { | ||
| const tool = await initTool(WarehouseListTool) | ||
| const ctx = toolCtx() | ||
| await tool.execute({}, ctx) | ||
| const previous = process.env.ALTIMATE_CODE_CONN_ENVLATE | ||
| process.env.ALTIMATE_CODE_CONN_ENVLATE = JSON.stringify({ type: "postgres", host: "h" }) | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| try { | ||
| expect((await tool.execute({}, ctx)).output).toContain("envlate") | ||
| } finally { | ||
| if (previous === undefined) delete process.env.ALTIMATE_CODE_CONN_ENVLATE | ||
| else process.env.ALTIMATE_CODE_CONN_ENVLATE = previous | ||
| } | ||
| }) | ||
|
|
||
| test("with a connection loaded, editing the file does not replace the configs", () => { | ||
| const named = Registry.localConfigPath() | ||
| fs.mkdirSync(path.dirname(named), { recursive: true }) | ||
| fs.writeFileSync(named, JSON.stringify({ "db-a": { type: "postgres", host: "old" } })) | ||
| expect(Registry.getConfig("db-a")?.host).toBe("old") | ||
| fs.writeFileSync(named, JSON.stringify({ "db-a": { type: "postgres", host: "new" }, "db-b": { type: "postgres" } })) | ||
| expect(Registry.getConfig("db-a")?.host).toBe("old") | ||
| expect(Registry.names()).toEqual(["db-a"]) | ||
| }) | ||
|
|
||
| test("a populated registry is not clobbered when the working project changes", () => { | ||
| const named = Registry.localConfigPath() | ||
| fs.mkdirSync(path.dirname(named), { recursive: true }) | ||
| fs.writeFileSync(named, JSON.stringify({ "proj-a": { type: "postgres" } })) | ||
| expect(Registry.names()).toEqual(["proj-a"]) | ||
|
|
||
| const other = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "whl-hint-b-"))) | ||
| try { | ||
| fs.mkdirSync(path.join(other, ".altimate-code")) | ||
| fs.writeFileSync( | ||
| path.join(other, ".altimate-code", "connections.json"), | ||
| JSON.stringify({ "proj-b": { type: "postgres" } }), | ||
| ) | ||
| process.chdir(other) | ||
| expect(Registry.names()).toEqual(["proj-a"]) | ||
| } finally { | ||
| process.chdir(project) | ||
| fs.rmSync(other, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
|
|
||
| test("an empty registry first loaded for one project does not pick up another project's file", () => { | ||
| expect(Registry.names()).toEqual([]) | ||
| const dirA = project | ||
| const other = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "whl-hint-b-"))) | ||
| try { | ||
| fs.mkdirSync(path.join(other, ".altimate-code")) | ||
| fs.writeFileSync( | ||
| path.join(other, ".altimate-code", "connections.json"), | ||
| JSON.stringify({ "proj-b": { type: "postgres" } }), | ||
| ) | ||
| process.chdir(other) | ||
| expect(Registry.names()).toEqual([]) | ||
| expect(Registry.projectConfigApplies()).toBe(false) | ||
| expect(noConnectionsMessage()).not.toContain(Registry.localConfigPath()) | ||
| expect(noConnectionsMessage()).toContain(Registry.globalConfigPath()) | ||
| process.chdir(dirA) | ||
| expect(Registry.projectConfigApplies()).toBe(true) | ||
| // the same project still picks up its own file after an empty answer | ||
| fs.mkdirSync(path.join(dirA, ".altimate-code")) | ||
| fs.writeFileSync( | ||
| path.join(dirA, ".altimate-code", "connections.json"), | ||
| JSON.stringify({ "proj-a": { type: "postgres" } }), | ||
| ) | ||
| expect(Registry.names()).toEqual(["proj-a"]) | ||
| } finally { | ||
| process.chdir(dirA) | ||
| fs.rmSync(other, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
|
|
||
| test("setConfigs-injected connections are kept", () => { | ||
| Registry.setConfigs({ injected: { type: "postgres" } }) | ||
| expect(Registry.names()).toEqual(["injected"]) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
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.