Skip to content

Commit 77aad83

Browse files
anandgupta42claude
andauthored
fix: brew formula stuck at v0.3.1, version normalization in publish pipeline (#286)
* fix: brew formula stuck at v0.3.1 — strip `v` prefix from `Script.version`, fix `latest()` fallback Root cause: `Script.version` passed `OPENCODE_VERSION` (git tag `v0.4.9`) verbatim, causing double-v URLs (`vv0.4.9`) in brew formula and invalid version fields. - Strip `v` prefix from `OPENCODE_VERSION` in `packages/script/src/index.ts` - Replace dead `formulae.brew.sh` fallback (404s — not in core homebrew) with `brew info --json=v2` then GitHub releases API - Add 64 new tests: version normalization, brew formula template, upgrade method detection - Extend existing publish-package tests with version/URL validation Closes #285 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]> * fix: brew formula stuck at v0.3.1 — strip `v` prefix from `Script.version`, fix `latest()` fallback Root cause: `Script.version` passed `OPENCODE_VERSION` (git tag `v0.4.9`) verbatim, causing double-v URLs (`vv0.4.9`) in brew formula and invalid version fields. - Strip `v` prefix from `OPENCODE_VERSION` in `packages/script/src/index.ts` - Replace dead `formulae.brew.sh` fallback (404s — not in core homebrew) with `brew info --json=v2` then GitHub releases API - Add 64 new tests: version normalization, brew formula template, upgrade method detection - Extend existing publish-package tests with version/URL validation Closes #285 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
1 parent f27dc39 commit 77aad83

6 files changed

Lines changed: 501 additions & 7 deletions

File tree

‎packages/opencode/src/installation/index.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,22 @@ export namespace Installation {
288288
if (!version) throw new Error(`Could not detect version for tap formula: ${formula}`)
289289
return version
290290
}
291-
// altimate_change start — brew formula URL
292-
return fetch("https://formulae.brew.sh/api/formula/altimate-code.json")
293-
// altimate_change end
291+
// altimate_change start — brew: use GitHub releases API as source of truth
292+
// altimate-code is NOT in core homebrew, so formulae.brew.sh will 404.
293+
// `brew info --json=v2` returns the LOCAL cached version which can be stale
294+
// if the tap hasn't been updated — using it would cause `latest()` to return
295+
// the already-installed version, making the upgrade command skip silently.
296+
// GitHub releases API is the authoritative source for the actual latest version.
297+
return fetch("https://api.github.com/repos/AltimateAI/altimate-code/releases/latest")
294298
.then((res) => {
295-
if (!res.ok) throw new Error(res.statusText)
299+
if (!res.ok) throw new Error(`GitHub releases API: ${res.status} ${res.statusText}`)
296300
return res.json()
297301
})
298-
.then((data: any) => data.versions.stable)
302+
.then((data: any) => {
303+
if (!data.tag_name) throw new Error("Missing tag_name in GitHub releases response")
304+
return data.tag_name.replace(/^v/, "")
305+
})
306+
// altimate_change end
299307
}
300308

301309
if (detectedMethod === "npm" || detectedMethod === "bun" || detectedMethod === "pnpm") {
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
/**
2+
* Homebrew formula generation tests.
3+
*
4+
* Validates that the brew formula template in publish.ts generates
5+
* correct Ruby syntax, URLs, version fields, and platform coverage.
6+
*/
7+
import { describe, test, expect } from "bun:test"
8+
import fs from "fs"
9+
import path from "path"
10+
11+
const PUBLISH_SCRIPT = fs.readFileSync(
12+
path.resolve(import.meta.dir, "../../script/publish.ts"),
13+
"utf-8",
14+
)
15+
16+
/**
17+
* Generate a brew formula using the same template logic as publish.ts,
18+
* but with test values substituted.
19+
*/
20+
function generateBrewFormula(opts: {
21+
version: string
22+
macX64Sha: string
23+
macArm64Sha: string
24+
x64Sha: string
25+
arm64Sha: string
26+
}): string {
27+
return [
28+
"# typed: false",
29+
"# frozen_string_literal: true",
30+
"",
31+
"class AltimateCode < Formula",
32+
` desc "The AI coding agent built for the terminal."`,
33+
` homepage "https://github.com/AltimateAI/altimate-code"`,
34+
` version "${opts.version.split("-")[0]}"`,
35+
"",
36+
` depends_on "ripgrep"`,
37+
"",
38+
" on_macos do",
39+
" if Hardware::CPU.intel?",
40+
` url "https://github.com/AltimateAI/altimate-code/releases/download/v${opts.version}/altimate-code-darwin-x64.zip"`,
41+
` sha256 "${opts.macX64Sha}"`,
42+
"",
43+
" def install",
44+
' bin.install "altimate"',
45+
' bin.install_symlink "altimate" => "altimate-code"',
46+
" end",
47+
" end",
48+
" if Hardware::CPU.arm?",
49+
` url "https://github.com/AltimateAI/altimate-code/releases/download/v${opts.version}/altimate-code-darwin-arm64.zip"`,
50+
` sha256 "${opts.macArm64Sha}"`,
51+
"",
52+
" def install",
53+
' bin.install "altimate"',
54+
' bin.install_symlink "altimate" => "altimate-code"',
55+
" end",
56+
" end",
57+
" end",
58+
"",
59+
" on_linux do",
60+
" if Hardware::CPU.intel? and Hardware::CPU.is_64_bit?",
61+
` url "https://github.com/AltimateAI/altimate-code/releases/download/v${opts.version}/altimate-code-linux-x64.tar.gz"`,
62+
` sha256 "${opts.x64Sha}"`,
63+
" def install",
64+
' bin.install "altimate"',
65+
' bin.install_symlink "altimate" => "altimate-code"',
66+
" end",
67+
" end",
68+
" if Hardware::CPU.arm? and Hardware::CPU.is_64_bit?",
69+
` url "https://github.com/AltimateAI/altimate-code/releases/download/v${opts.version}/altimate-code-linux-arm64.tar.gz"`,
70+
` sha256 "${opts.arm64Sha}"`,
71+
" def install",
72+
' bin.install "altimate"',
73+
' bin.install_symlink "altimate" => "altimate-code"',
74+
" end",
75+
" end",
76+
" end",
77+
"end",
78+
"",
79+
"",
80+
].join("\n")
81+
}
82+
83+
describe("brew formula template", () => {
84+
const formula = generateBrewFormula({
85+
version: "0.4.9",
86+
macX64Sha: "abc123",
87+
macArm64Sha: "def456",
88+
x64Sha: "ghi789",
89+
arm64Sha: "jkl012",
90+
})
91+
92+
test("version field is clean semver without v prefix", () => {
93+
expect(formula).toContain('version "0.4.9"')
94+
expect(formula).not.toContain('version "v0.4.9"')
95+
})
96+
97+
test("URLs have exactly one v prefix", () => {
98+
const urls = formula.match(/url "([^"]+)"/g) || []
99+
expect(urls.length).toBe(4)
100+
for (const url of urls) {
101+
expect(url).toContain("/download/v0.4.9/")
102+
expect(url).not.toContain("/download/vv0.4.9/")
103+
}
104+
})
105+
106+
test("all 4 platform variants are present", () => {
107+
expect(formula).toContain("altimate-code-darwin-x64.zip")
108+
expect(formula).toContain("altimate-code-darwin-arm64.zip")
109+
expect(formula).toContain("altimate-code-linux-x64.tar.gz")
110+
expect(formula).toContain("altimate-code-linux-arm64.tar.gz")
111+
})
112+
113+
test("macOS uses .zip format", () => {
114+
const macUrls = formula.match(/darwin-[^"]+/g) || []
115+
for (const url of macUrls) {
116+
expect(url).toMatch(/\.zip$/)
117+
}
118+
})
119+
120+
test("Linux uses .tar.gz format", () => {
121+
const linuxUrls = formula.match(/linux-[^"]+/g) || []
122+
for (const url of linuxUrls) {
123+
expect(url).toMatch(/\.tar\.gz$/)
124+
}
125+
})
126+
127+
test("SHA256 hashes are present for all platforms", () => {
128+
const shas = formula.match(/sha256 "[^"]+"/g) || []
129+
expect(shas.length).toBe(4)
130+
})
131+
132+
test("depends on ripgrep", () => {
133+
expect(formula).toContain('depends_on "ripgrep"')
134+
})
135+
136+
test("installs altimate binary and creates altimate-code symlink", () => {
137+
const installLines = formula.match(/bin\.install "altimate"/g) || []
138+
expect(installLines.length).toBe(4) // once per platform variant
139+
140+
const symlinkLines = formula.match(/bin\.install_symlink "altimate" => "altimate-code"/g) || []
141+
expect(symlinkLines.length).toBe(4)
142+
})
143+
144+
test("class name is AltimateCode", () => {
145+
expect(formula).toContain("class AltimateCode < Formula")
146+
})
147+
148+
test("has correct homepage", () => {
149+
expect(formula).toContain('homepage "https://github.com/AltimateAI/altimate-code"')
150+
})
151+
})
152+
153+
describe("brew formula with pre-release version", () => {
154+
const formula = generateBrewFormula({
155+
version: "1.0.0-beta.1",
156+
macX64Sha: "aaa",
157+
macArm64Sha: "bbb",
158+
x64Sha: "ccc",
159+
arm64Sha: "ddd",
160+
})
161+
162+
test("version field strips pre-release suffix", () => {
163+
expect(formula).toContain('version "1.0.0"')
164+
expect(formula).not.toContain('version "1.0.0-beta.1"')
165+
})
166+
167+
test("URLs include full pre-release version", () => {
168+
expect(formula).toContain("/download/v1.0.0-beta.1/")
169+
})
170+
})
171+
172+
describe("publish.ts brew formula template matches test template", () => {
173+
test("publish.ts generates formula with same structure", () => {
174+
// Verify the template in publish.ts uses the same patterns we test
175+
expect(PUBLISH_SCRIPT).toContain("class AltimateCode < Formula")
176+
expect(PUBLISH_SCRIPT).toContain('depends_on "ripgrep"')
177+
expect(PUBLISH_SCRIPT).toContain('bin.install "altimate"')
178+
expect(PUBLISH_SCRIPT).toContain('bin.install_symlink "altimate" => "altimate-code"')
179+
expect(PUBLISH_SCRIPT).toContain("on_macos do")
180+
expect(PUBLISH_SCRIPT).toContain("on_linux do")
181+
expect(PUBLISH_SCRIPT).toContain("Hardware::CPU.intel?")
182+
expect(PUBLISH_SCRIPT).toContain("Hardware::CPU.arm?")
183+
})
184+
185+
test("publish.ts uses Script.version in URL construction", () => {
186+
// URLs must use v${Script.version} — not vv or raw tag
187+
expect(PUBLISH_SCRIPT).toContain("v${Script.version}/altimate-code-darwin-x64.zip")
188+
expect(PUBLISH_SCRIPT).toContain("v${Script.version}/altimate-code-darwin-arm64.zip")
189+
expect(PUBLISH_SCRIPT).toContain("v${Script.version}/altimate-code-linux-x64.tar.gz")
190+
expect(PUBLISH_SCRIPT).toContain("v${Script.version}/altimate-code-linux-arm64.tar.gz")
191+
})
192+
})

‎packages/opencode/test/install/publish-package.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,28 @@ describe("publish package validation", () => {
7575
})
7676
})
7777

78+
describe("version normalization in publish pipeline", () => {
79+
test("publish.ts never produces double-v (vv) in any URL", () => {
80+
const publishScript = fs.readFileSync(path.join(REPO_PKG_DIR, "script/publish.ts"), "utf-8")
81+
// All URLs use v${Script.version} or v${...} patterns
82+
// Script.version is already clean (v stripped), so vv should never appear
83+
expect(publishScript).not.toContain('"vv')
84+
expect(publishScript).not.toContain("/vv")
85+
})
86+
87+
test("Script source strips v prefix from OPENCODE_VERSION", () => {
88+
const scriptSrc = fs.readFileSync(path.resolve(REPO_PKG_DIR, "../../packages/script/src/index.ts"), "utf-8")
89+
expect(scriptSrc).toContain('env.OPENCODE_VERSION.replace(/^v/, "")')
90+
})
91+
92+
test("optionalDependencies version comes from build artifacts (clean semver)", () => {
93+
const publishScript = fs.readFileSync(path.join(REPO_PKG_DIR, "script/publish.ts"), "utf-8")
94+
// The version is extracted from binary package.json files in dist/
95+
// which get their version from Script.version (now v-stripped)
96+
expect(publishScript).toContain("const version = Object.values(binaries)[0]")
97+
})
98+
})
99+
78100
describe("unscoped package (altimate-code)", () => {
79101
const publishScript = fs.readFileSync(path.join(REPO_PKG_DIR, "script/publish.ts"), "utf-8")
80102

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* Upgrade method detection and brew latest() tests.
3+
*
4+
* Validates the installation method detection logic and the
5+
* brew version resolution paths in installation/index.ts.
6+
*/
7+
import { describe, test, expect } from "bun:test"
8+
import fs from "fs"
9+
import path from "path"
10+
11+
const INSTALLATION_SRC = fs.readFileSync(
12+
path.resolve(import.meta.dir, "../../src/installation/index.ts"),
13+
"utf-8",
14+
)
15+
16+
describe("installation method detection", () => {
17+
test("checks brew with correct formula name", () => {
18+
// method() must check for "altimate-code" not "opencode"
19+
expect(INSTALLATION_SRC).toContain('"altimate-code"')
20+
})
21+
22+
test("npm detection uses scoped package name", () => {
23+
expect(INSTALLATION_SRC).toContain("@altimateai/altimate-code")
24+
})
25+
26+
test("brew check command uses brew list --formula", () => {
27+
expect(INSTALLATION_SRC).toContain('"brew", "list", "--formula"')
28+
})
29+
30+
test("method detection prioritizes matching exec path", () => {
31+
// checks.sort puts the manager matching process.execPath first
32+
expect(INSTALLATION_SRC).toContain("exec.includes(a.name)")
33+
})
34+
})
35+
36+
describe("brew formula resolution", () => {
37+
test("getBrewFormula checks tap formula first", () => {
38+
expect(INSTALLATION_SRC).toContain("AltimateAI/tap/altimate-code")
39+
})
40+
41+
test("getBrewFormula returns tap formula as default", () => {
42+
expect(INSTALLATION_SRC).toContain('return "AltimateAI/tap/altimate-code"')
43+
})
44+
})
45+
46+
describe("brew latest() version resolution", () => {
47+
test("tap formula uses brew info --json=v2", () => {
48+
expect(INSTALLATION_SRC).toContain('"brew", "info", "--json=v2"')
49+
})
50+
51+
test("non-tap formula does NOT use formulae.brew.sh", () => {
52+
// altimate-code is NOT in core homebrew — formulae.brew.sh would 404
53+
expect(INSTALLATION_SRC).not.toContain("formulae.brew.sh/api/formula/altimate-code.json")
54+
})
55+
56+
test("non-tap brew uses GitHub releases API as source of truth", () => {
57+
// brew info --json=v2 returns LOCAL cached version which can be stale.
58+
// GitHub releases API is authoritative for the actual latest version.
59+
expect(INSTALLATION_SRC).toContain("api.github.com/repos/AltimateAI/altimate-code/releases/latest")
60+
})
61+
62+
test("GitHub releases fallback strips v prefix from tag_name", () => {
63+
expect(INSTALLATION_SRC).toContain('tag_name.replace(/^v/, "")')
64+
})
65+
66+
test("GitHub releases fallback validates tag_name exists", () => {
67+
expect(INSTALLATION_SRC).toContain("Missing tag_name")
68+
})
69+
})
70+
71+
describe("upgrade execution", () => {
72+
test("npm upgrade uses scoped package name", () => {
73+
expect(INSTALLATION_SRC).toContain("@altimateai/altimate-code@${target}")
74+
})
75+
76+
test("brew upgrade taps AltimateAI/tap", () => {
77+
expect(INSTALLATION_SRC).toContain('"brew", "tap", "AltimateAI/tap"')
78+
})
79+
80+
test("brew upgrade pulls latest formula before upgrading", () => {
81+
expect(INSTALLATION_SRC).toContain('"git", "pull", "--ff-only"')
82+
})
83+
84+
test("brew upgrade disables auto-update", () => {
85+
expect(INSTALLATION_SRC).toContain("HOMEBREW_NO_AUTO_UPDATE")
86+
})
87+
88+
test("curl upgrade uses altimate.ai/install endpoint", () => {
89+
expect(INSTALLATION_SRC).toContain("https://altimate.ai/install")
90+
})
91+
92+
test("VERSION normalization strips v prefix", () => {
93+
expect(INSTALLATION_SRC).toContain('OPENCODE_VERSION.trim().replace(/^v/, "")')
94+
})
95+
})
96+
97+
describe("version comparison in upgrade command", () => {
98+
/**
99+
* Simulate the version comparison logic from cmd/upgrade.ts.
100+
* Both sides must be normalized for comparison to work.
101+
*/
102+
function wouldSkipUpgrade(currentVersion: string, target: string): boolean {
103+
return currentVersion === target
104+
}
105+
106+
test("matching versions skip upgrade", () => {
107+
expect(wouldSkipUpgrade("0.4.9", "0.4.9")).toBe(true)
108+
})
109+
110+
test("different versions proceed with upgrade", () => {
111+
expect(wouldSkipUpgrade("0.4.8", "0.4.9")).toBe(false)
112+
})
113+
114+
test("v-prefixed current would NOT match clean target (documents the fix)", () => {
115+
// Before the fix, VERSION could be "v0.4.9" and target "0.4.9"
116+
// This would incorrectly proceed with upgrade even when versions match
117+
expect(wouldSkipUpgrade("v0.4.9", "0.4.9")).toBe(false)
118+
// After the fix, both are clean — comparison works correctly
119+
const normalized = "v0.4.9".replace(/^v/, "")
120+
expect(wouldSkipUpgrade(normalized, "0.4.9")).toBe(true)
121+
})
122+
})

0 commit comments

Comments
 (0)