npm.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import fs from "fs/promises"
  2. import path from "path"
  3. import { describe, expect, test } from "bun:test"
  4. import { Effect } from "effect"
  5. import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
  6. import { Global } from "@opencode-ai/util/global"
  7. import { Npm } from "@opencode-ai/util/npm"
  8. import { tmpdir } from "./fixture/tmpdir"
  9. const win = process.platform === "win32"
  10. const writePackage = (dir: string, pkg: Record<string, unknown>) =>
  11. Bun.write(
  12. path.join(dir, "package.json"),
  13. JSON.stringify({
  14. version: "1.0.0",
  15. ...pkg,
  16. }),
  17. )
  18. const npmLayer = (cache: string) =>
  19. AppNodeBuilder.build(Npm.node, [[Global.node, Global.layerWith({ cache, state: path.join(cache, "state") })]])
  20. describe("Npm.sanitize", () => {
  21. test("keeps normal scoped package specs unchanged", () => {
  22. expect(Npm.sanitize("@opencode/acme")).toBe("@opencode/acme")
  23. expect(Npm.sanitize("@opencode/acme@1.0.0")).toBe("@opencode/acme@1.0.0")
  24. expect(Npm.sanitize("prettier")).toBe("prettier")
  25. })
  26. test("handles git https specs", () => {
  27. const spec = "acme@git+https://github.com/opencode/acme.git"
  28. const expected = win ? "acme@git+https_//github.com/opencode/acme.git" : spec
  29. expect(Npm.sanitize(spec)).toBe(expected)
  30. })
  31. })
  32. describe("Npm.add", () => {
  33. test("resolves cached scoped package specs without reifying", async () => {
  34. await using tmp = await tmpdir()
  35. const spec = "@fixture/provider@1.0.0"
  36. const directory = path.join(
  37. tmp.path,
  38. "cache",
  39. "packages",
  40. Npm.sanitize(spec),
  41. "node_modules",
  42. "@fixture",
  43. "provider",
  44. )
  45. await fs.mkdir(directory, { recursive: true })
  46. await writePackage(directory, { name: "@fixture/provider", exports: "./index.js" })
  47. await Bun.write(path.join(directory, "index.js"), "export const fixture = true\n")
  48. const entry = await Effect.gen(function* () {
  49. const npm = yield* Npm.Service
  50. return yield* npm.add(spec)
  51. }).pipe(Effect.scoped, Effect.provide(npmLayer(path.join(tmp.path, "cache"))), Effect.runPromise)
  52. expect(entry.directory).toBe(directory)
  53. expect(entry.entrypoint).toEndWith("/index.js")
  54. })
  55. test("falls back to the original spec when parsing fails", async () => {
  56. await using tmp = await tmpdir()
  57. const spec = "fixture provider"
  58. const directory = path.join(tmp.path, "cache", "packages", Npm.sanitize(spec), "node_modules", spec)
  59. await fs.mkdir(directory, { recursive: true })
  60. await writePackage(directory, { name: spec, exports: "./index.js" })
  61. await Bun.write(path.join(directory, "index.js"), "export const fixture = true\n")
  62. const entry = await Effect.gen(function* () {
  63. const npm = yield* Npm.Service
  64. return yield* npm.add(spec)
  65. }).pipe(Effect.scoped, Effect.provide(npmLayer(path.join(tmp.path, "cache"))), Effect.runPromise)
  66. expect(entry.directory).toBe(directory)
  67. expect(entry.entrypoint).toEndWith("/index.js")
  68. })
  69. test("reifies when package cache directory exists without the package installed", async () => {
  70. await using tmp = await tmpdir()
  71. await fs.mkdir(path.join(tmp.path, "fixture-provider"))
  72. await writePackage(path.join(tmp.path, "fixture-provider"), {
  73. name: "fixture-provider",
  74. exports: {
  75. ".": "./index.js",
  76. "./tui": "./tui.js",
  77. },
  78. })
  79. await Bun.write(path.join(tmp.path, "fixture-provider", "index.js"), "export const fixture = true\n")
  80. await Bun.write(path.join(tmp.path, "fixture-provider", "tui.js"), "export const tui = true\n")
  81. const spec = `fixture-provider@file:${path.join(tmp.path, "fixture-provider")}`
  82. await fs.mkdir(path.join(tmp.path, "cache", "packages", Npm.sanitize(spec)), { recursive: true })
  83. const entries = await Effect.gen(function* () {
  84. const npm = yield* Npm.Service
  85. return {
  86. tui: yield* npm.add(spec, { subpaths: ["tui", ""] }),
  87. fallback: yield* npm.add(spec, { subpaths: ["missing", ""] }),
  88. }
  89. }).pipe(Effect.scoped, Effect.provide(npmLayer(path.join(tmp.path, "cache"))), Effect.runPromise)
  90. expect(entries.tui.entrypoint).toEndWith("/tui.js")
  91. expect(entries.fallback.entrypoint).toEndWith("/index.js")
  92. })
  93. })