|
1 | 1 | import { assertFalse, assertStrictEquals } from "jsr:@std/assert@^1.0.0"; |
2 | 2 | import { describe, it } from "jsr:@std/testing@^1.0.0/bdd"; |
| 3 | +import { stub } from "jsr:@std/testing@^1.0.0/mock"; |
| 4 | +import os from "node:os"; |
3 | 5 | import { join } from "node:path"; |
4 | 6 | import { loadConfig } from "./config.ts"; |
5 | 7 | import type { GraphitiConfig } from "./types/index.ts"; |
@@ -215,5 +217,106 @@ describe("config", () => { |
215 | 217 | }); |
216 | 218 | }); |
217 | 219 | }); |
| 220 | + |
| 221 | + // --- legacy home fallback tests --- |
| 222 | + // |
| 223 | + // README documents a legacy fallback at ~/.config/opencode/.graphitirc (step 3 in |
| 224 | + // lookup order). These tests are deterministic regression tests that verify this |
| 225 | + // exact path is read when no project-local or standard global config is found. |
| 226 | + // |
| 227 | + // The tests use an isolated fake home directory (via os.homedir() stub) so the |
| 228 | + // real home directory is never touched. |
| 229 | + |
| 230 | + it("loads ~/.config/opencode/.graphitirc as legacy fallback when no other config exists", async () => { |
| 231 | + // Create an isolated fake home dir so the real ~ is never touched. |
| 232 | + const fakeHome = await Deno.makeTempDir({ prefix: "graphiti_fakehome_" }); |
| 233 | + const legacyDir = join(fakeHome, ".config", "opencode"); |
| 234 | + |
| 235 | + try { |
| 236 | + await Deno.mkdir(legacyDir, { recursive: true }); |
| 237 | + await Deno.writeTextFile( |
| 238 | + join(legacyDir, ".graphitirc"), |
| 239 | + JSON.stringify({ |
| 240 | + endpoint: "http://legacy-opencode.local/mcp", |
| 241 | + driftThreshold: 0.8, |
| 242 | + factStaleDays: 42, |
| 243 | + }), |
| 244 | + ); |
| 245 | + |
| 246 | + // Redirect os.homedir() for the duration of this test only. |
| 247 | + // CWD is outside fakeHome so the upward walk never reaches it. |
| 248 | + using _homedirStub = stub(os, "homedir", () => fakeHome); |
| 249 | + |
| 250 | + await withTempDirAsCwd(() => { |
| 251 | + const config = loadConfig(); |
| 252 | + assertConfigValues(config, { |
| 253 | + endpoint: "http://legacy-opencode.local/mcp", |
| 254 | + groupIdPrefix: "opencode", // default, not in file |
| 255 | + driftThreshold: 0.8, |
| 256 | + factStaleDays: 42, |
| 257 | + }); |
| 258 | + }); |
| 259 | + } finally { |
| 260 | + await Deno.remove(fakeHome, { recursive: true }); |
| 261 | + } |
| 262 | + }); |
| 263 | + |
| 264 | + it("merges partial ~/.config/opencode/.graphitirc with defaults", async () => { |
| 265 | + const fakeHome = await Deno.makeTempDir({ prefix: "graphiti_fakehome_" }); |
| 266 | + const legacyDir = join(fakeHome, ".config", "opencode"); |
| 267 | + |
| 268 | + try { |
| 269 | + await Deno.mkdir(legacyDir, { recursive: true }); |
| 270 | + // Only override one field; remaining fields must come from DEFAULT_CONFIG. |
| 271 | + await Deno.writeTextFile( |
| 272 | + join(legacyDir, ".graphitirc"), |
| 273 | + JSON.stringify({ endpoint: "http://partial-legacy.local/mcp" }), |
| 274 | + ); |
| 275 | + |
| 276 | + using _homedirStub = stub(os, "homedir", () => fakeHome); |
| 277 | + |
| 278 | + await withTempDirAsCwd(() => { |
| 279 | + const config = loadConfig(); |
| 280 | + assertStrictEquals( |
| 281 | + config.endpoint, |
| 282 | + "http://partial-legacy.local/mcp", |
| 283 | + ); |
| 284 | + assertStrictEquals(config.groupIdPrefix, "opencode"); // default |
| 285 | + assertStrictEquals(config.driftThreshold, 0.5); // default |
| 286 | + assertStrictEquals(config.factStaleDays, 30); // default |
| 287 | + }); |
| 288 | + } finally { |
| 289 | + await Deno.remove(fakeHome, { recursive: true }); |
| 290 | + } |
| 291 | + }); |
| 292 | + |
| 293 | + it("project-local config takes precedence over ~/.config/opencode/.graphitirc", async () => { |
| 294 | + const fakeHome = await Deno.makeTempDir({ prefix: "graphiti_fakehome_" }); |
| 295 | + const legacyDir = join(fakeHome, ".config", "opencode"); |
| 296 | + |
| 297 | + try { |
| 298 | + await Deno.mkdir(legacyDir, { recursive: true }); |
| 299 | + await Deno.writeTextFile( |
| 300 | + join(legacyDir, ".graphitirc"), |
| 301 | + JSON.stringify({ |
| 302 | + endpoint: "http://legacy-should-be-ignored.local/mcp", |
| 303 | + }), |
| 304 | + ); |
| 305 | + |
| 306 | + using _homedirStub = stub(os, "homedir", () => fakeHome); |
| 307 | + |
| 308 | + await withTempDir(async (projectDir) => { |
| 309 | + await Deno.writeTextFile( |
| 310 | + join(projectDir, ".graphitirc"), |
| 311 | + JSON.stringify({ endpoint: "http://project-wins.local/mcp" }), |
| 312 | + ); |
| 313 | + |
| 314 | + const config = loadConfig(projectDir); |
| 315 | + assertStrictEquals(config.endpoint, "http://project-wins.local/mcp"); |
| 316 | + }); |
| 317 | + } finally { |
| 318 | + await Deno.remove(fakeHome, { recursive: true }); |
| 319 | + } |
| 320 | + }); |
218 | 321 | }); |
219 | 322 | }); |
0 commit comments