|
1 | 1 | import { afterEach, describe, expect, spyOn, test } from "bun:test"; |
2 | 2 | import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
3 | 3 | import { tmpdir } from "node:os"; |
4 | | -import { join } from "node:path"; |
| 4 | +import { isAbsolute, join, posix, win32 } from "node:path"; |
5 | 5 | import * as serviceModule from "../src/service"; |
6 | 6 | import { saveConfig } from "../src/config"; |
7 | 7 | import { windowsEnvIndirectBatchValue } from "../src/lib/win-paths"; |
@@ -663,6 +663,61 @@ describe("launchd service plist", () => { |
663 | 663 | else process.env.OPENCODEX_API_AUTH_TOKEN = oldApiAuthToken; |
664 | 664 | } |
665 | 665 | }); |
| 666 | + |
| 667 | + // A POSIX unit must carry the literal POSIX path no matter which host writes it. The two |
| 668 | + // cases above are where this actually bites: on a Windows host `resolve("/tmp/x")` anchors |
| 669 | + // to the current drive and the generated file said `D:\tmp\codex-sqlite-home`, while |
| 670 | + // CODEX_HOME beside it kept `/tmp/codex-home`. The same file disagreed with itself about two |
| 671 | + // variables holding the same kind of value. This states the rule directly so the intent |
| 672 | + // survives; on a POSIX host `resolve()` is identity here, so only Windows can catch it. |
| 673 | + test("carries an absolute POSIX sqlite home into POSIX units without host anchoring", () => { |
| 674 | + const inherited = process.env.CODEX_SQLITE_HOME; |
| 675 | + try { |
| 676 | + process.env.CODEX_SQLITE_HOME = "/var/lib/opencodex/codex-sqlite"; |
| 677 | + |
| 678 | + expect(buildPlist()).toContain( |
| 679 | + "<key>CODEX_SQLITE_HOME</key><string>/var/lib/opencodex/codex-sqlite</string>", |
| 680 | + ); |
| 681 | + expect(buildUnit()).toContain( |
| 682 | + 'Environment="CODEX_SQLITE_HOME=/var/lib/opencodex/codex-sqlite"', |
| 683 | + ); |
| 684 | + } finally { |
| 685 | + if (inherited === undefined) delete process.env.CODEX_SQLITE_HOME; |
| 686 | + else process.env.CODEX_SQLITE_HOME = inherited; |
| 687 | + } |
| 688 | + }); |
| 689 | + |
| 690 | + // The relative case is why the resolve() is there at all: a service unit has no meaningful |
| 691 | + // working directory, so a relative home must still be made absolute. |
| 692 | + test("still absolutizes a relative sqlite home", () => { |
| 693 | + const inherited = process.env.CODEX_SQLITE_HOME; |
| 694 | + try { |
| 695 | + process.env.CODEX_SQLITE_HOME = "relative-sqlite-home"; |
| 696 | + const plist = buildPlist(); |
| 697 | + |
| 698 | + // Assert the emitted value is actually absolute. Rejecting only the raw string would |
| 699 | + // stay green for any other non-absolute transform, which is the whole thing this test |
| 700 | + // exists to catch. The two artifact formats differ, so each is extracted on its own |
| 701 | + // terms: launchd is XML, systemd is a quoted Environment= line. |
| 702 | + const plistValue = /<key>CODEX_SQLITE_HOME<\/key>\s*<string>([^<]*)<\/string>/.exec(plist)?.[1]; |
| 703 | + expect(plistValue).toBeDefined(); |
| 704 | + expect( |
| 705 | + isAbsolute(plistValue!) || posix.isAbsolute(plistValue!) || win32.isAbsolute(plistValue!), |
| 706 | + ).toBe(true); |
| 707 | + expect(plistValue!.endsWith("relative-sqlite-home")).toBe(true); |
| 708 | + |
| 709 | + const unit = buildUnit(); |
| 710 | + const unitValue = /Environment="CODEX_SQLITE_HOME=([^"]*)"/.exec(unit)?.[1]; |
| 711 | + expect(unitValue).toBeDefined(); |
| 712 | + expect( |
| 713 | + isAbsolute(unitValue!) || posix.isAbsolute(unitValue!) || win32.isAbsolute(unitValue!), |
| 714 | + ).toBe(true); |
| 715 | + expect(unitValue!.endsWith("relative-sqlite-home")).toBe(true); |
| 716 | + } finally { |
| 717 | + if (inherited === undefined) delete process.env.CODEX_SQLITE_HOME; |
| 718 | + else process.env.CODEX_SQLITE_HOME = inherited; |
| 719 | + } |
| 720 | + }); |
666 | 721 | }); |
667 | 722 |
|
668 | 723 | describe("service lifecycle cleanup ordering", () => { |
|
0 commit comments