installation.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { describe, expect } from "bun:test"
  2. import { Effect, Layer, Stream } from "effect"
  3. import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http"
  4. import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
  5. import { Installation } from "../../src/installation"
  6. import { InstallationChannel } from "@opencode-ai/core/installation/version"
  7. import { testEffect } from "../lib/effect"
  8. const encoder = new TextEncoder()
  9. function mockHttpClient(handler: (request: HttpClientRequest.HttpClientRequest) => Response) {
  10. const client = HttpClient.make((request) => Effect.succeed(HttpClientResponse.fromWeb(request, handler(request))))
  11. return Layer.succeed(HttpClient.HttpClient, client)
  12. }
  13. function mockSpawner(handler: (cmd: string, args: readonly string[]) => string = () => "") {
  14. const spawner = ChildProcessSpawner.make((command) => {
  15. const std = ChildProcess.isStandardCommand(command) ? command : undefined
  16. const output = handler(std?.command ?? "", std?.args ?? [])
  17. return Effect.succeed(
  18. ChildProcessSpawner.makeHandle({
  19. pid: ChildProcessSpawner.ProcessId(0),
  20. exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
  21. isRunning: Effect.succeed(false),
  22. kill: () => Effect.void,
  23. stdin: { [Symbol.for("effect/Sink/TypeId")]: Symbol.for("effect/Sink/TypeId") } as any,
  24. stdout: output ? Stream.make(encoder.encode(output)) : Stream.empty,
  25. stderr: Stream.empty,
  26. all: Stream.empty,
  27. getInputFd: () => ({ [Symbol.for("effect/Sink/TypeId")]: Symbol.for("effect/Sink/TypeId") }) as any,
  28. getOutputFd: () => Stream.empty,
  29. unref: Effect.succeed(Effect.void),
  30. }),
  31. )
  32. })
  33. return Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner)
  34. }
  35. function jsonResponse(body: unknown) {
  36. return new Response(JSON.stringify(body), {
  37. status: 200,
  38. headers: { "content-type": "application/json" },
  39. })
  40. }
  41. function testLayer(
  42. httpHandler: (request: HttpClientRequest.HttpClientRequest) => Response,
  43. spawnHandler?: (cmd: string, args: readonly string[]) => string,
  44. ) {
  45. return Installation.layer.pipe(Layer.provide(mockHttpClient(httpHandler)), Layer.provide(mockSpawner(spawnHandler)))
  46. }
  47. describe("installation", () => {
  48. describe("latest", () => {
  49. testEffect(testLayer(() => jsonResponse({ tag_name: "v1.2.3" }))).effect(
  50. "reads release version from GitHub releases",
  51. () =>
  52. Effect.gen(function* () {
  53. const result = yield* Installation.Service.use((svc) => svc.latest("unknown"))
  54. expect(result).toBe("1.2.3")
  55. }),
  56. )
  57. testEffect(testLayer(() => jsonResponse({ tag_name: "v4.0.0-beta.1" }))).effect(
  58. "strips v prefix from GitHub release tag",
  59. () =>
  60. Effect.gen(function* () {
  61. const result = yield* Installation.Service.use((svc) => svc.latest("curl"))
  62. expect(result).toBe("4.0.0-beta.1")
  63. }),
  64. )
  65. const npmCalls: string[] = []
  66. testEffect(
  67. testLayer((request) => {
  68. npmCalls.push(request.url)
  69. return jsonResponse({ version: "1.5.0" })
  70. }),
  71. ).effect("reads npm versions via registry", () =>
  72. Effect.gen(function* () {
  73. const result = yield* Installation.Service.use((svc) => svc.latest("npm"))
  74. expect(result).toBe("1.5.0")
  75. expect(npmCalls).toContain(`https://registry.npmjs.org/opencode-ai/${InstallationChannel}`)
  76. }),
  77. )
  78. const bunCalls: string[] = []
  79. testEffect(
  80. testLayer((request) => {
  81. bunCalls.push(request.url)
  82. return jsonResponse({ version: "1.6.0" })
  83. }),
  84. ).effect("reads bun versions via registry", () =>
  85. Effect.gen(function* () {
  86. const result = yield* Installation.Service.use((svc) => svc.latest("bun"))
  87. expect(result).toBe("1.6.0")
  88. expect(bunCalls).toContain(`https://registry.npmjs.org/opencode-ai/${InstallationChannel}`)
  89. }),
  90. )
  91. const pnpmCalls: string[] = []
  92. testEffect(
  93. testLayer((request) => {
  94. pnpmCalls.push(request.url)
  95. return jsonResponse({ version: "1.7.0" })
  96. }),
  97. ).effect("reads pnpm versions via registry", () =>
  98. Effect.gen(function* () {
  99. const result = yield* Installation.Service.use((svc) => svc.latest("pnpm"))
  100. expect(result).toBe("1.7.0")
  101. expect(pnpmCalls).toContain(`https://registry.npmjs.org/opencode-ai/${InstallationChannel}`)
  102. }),
  103. )
  104. testEffect(testLayer(() => jsonResponse({ version: "2.3.4" }))).effect("reads scoop manifest versions", () =>
  105. Effect.gen(function* () {
  106. const result = yield* Installation.Service.use((svc) => svc.latest("scoop"))
  107. expect(result).toBe("2.3.4")
  108. }),
  109. )
  110. testEffect(testLayer(() => jsonResponse({ d: { results: [{ Version: "3.4.5" }] } }))).effect(
  111. "reads chocolatey feed versions",
  112. () =>
  113. Effect.gen(function* () {
  114. const result = yield* Installation.Service.use((svc) => svc.latest("choco"))
  115. expect(result).toBe("3.4.5")
  116. }),
  117. )
  118. testEffect(
  119. testLayer(
  120. () => jsonResponse({ versions: { stable: "2.0.0" } }),
  121. (cmd, args) => {
  122. // getBrewFormula: return core formula (no tap)
  123. if (cmd === "brew" && args.includes("--formula") && args.includes("anomalyco/tap/opencode")) return ""
  124. if (cmd === "brew" && args.includes("--formula") && args.includes("opencode")) return "opencode"
  125. return ""
  126. },
  127. ),
  128. ).effect("reads brew formulae API versions", () =>
  129. Effect.gen(function* () {
  130. const result = yield* Installation.Service.use((svc) => svc.latest("brew"))
  131. expect(result).toBe("2.0.0")
  132. }),
  133. )
  134. const brewInfoJson = JSON.stringify({
  135. formulae: [{ versions: { stable: "2.1.0" } }],
  136. })
  137. testEffect(
  138. testLayer(
  139. () => jsonResponse({}), // HTTP not used for tap formula
  140. (cmd, args) => {
  141. if (cmd === "brew" && args.includes("anomalyco/tap/opencode") && args.includes("--formula")) return "opencode"
  142. if (cmd === "brew" && args.includes("--json=v2")) return brewInfoJson
  143. return ""
  144. },
  145. ),
  146. ).effect("reads brew tap info JSON via CLI", () =>
  147. Effect.gen(function* () {
  148. const result = yield* Installation.Service.use((svc) => svc.latest("brew"))
  149. expect(result).toBe("2.1.0")
  150. }),
  151. )
  152. })
  153. })