npm.test.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import fs from "fs/promises"
  2. import path from "path"
  3. import { describe, expect, test } from "bun:test"
  4. import { NodeFileSystem } from "@effect/platform-node"
  5. import { Effect, Layer, Option } from "effect"
  6. import { AppFileSystem } from "@opencode-ai/core/filesystem"
  7. import { Global } from "@opencode-ai/core/global"
  8. import { Npm } from "@opencode-ai/core/npm"
  9. import { EffectFlock } from "@opencode-ai/core/util/effect-flock"
  10. import { tmpdir } from "./fixture/tmpdir"
  11. const win = process.platform === "win32"
  12. const writePackage = (dir: string, pkg: Record<string, unknown>) =>
  13. Bun.write(
  14. path.join(dir, "package.json"),
  15. JSON.stringify({
  16. version: "1.0.0",
  17. ...pkg,
  18. }),
  19. )
  20. const npmLayer = (cache: string) =>
  21. Npm.layer.pipe(
  22. Layer.provide(EffectFlock.layer),
  23. Layer.provide(AppFileSystem.layer),
  24. Layer.provide(Global.layerWith({ cache, state: path.join(cache, "state") })),
  25. Layer.provide(NodeFileSystem.layer),
  26. )
  27. describe("Npm.sanitize", () => {
  28. test("keeps normal scoped package specs unchanged", () => {
  29. expect(Npm.sanitize("@opencode/acme")).toBe("@opencode/acme")
  30. expect(Npm.sanitize("@opencode/acme@1.0.0")).toBe("@opencode/acme@1.0.0")
  31. expect(Npm.sanitize("prettier")).toBe("prettier")
  32. })
  33. test("handles git https specs", () => {
  34. const spec = "acme@git+https://github.com/opencode/acme.git"
  35. const expected = win ? "acme@git+https_//github.com/opencode/acme.git" : spec
  36. expect(Npm.sanitize(spec)).toBe(expected)
  37. })
  38. })
  39. describe("Npm.add", () => {
  40. test("reifies when package cache directory exists without the package installed", async () => {
  41. await using tmp = await tmpdir()
  42. await fs.mkdir(path.join(tmp.path, "fixture-provider"))
  43. await writePackage(path.join(tmp.path, "fixture-provider"), {
  44. name: "fixture-provider",
  45. main: "index.js",
  46. })
  47. await Bun.write(path.join(tmp.path, "fixture-provider", "index.js"), "export const fixture = true\n")
  48. const spec = `fixture-provider@file:${path.join(tmp.path, "fixture-provider")}`
  49. await fs.mkdir(path.join(tmp.path, "cache", "packages", Npm.sanitize(spec)), { recursive: true })
  50. const entry = await Effect.gen(function* () {
  51. const npm = yield* Npm.Service
  52. return yield* npm.add(spec)
  53. }).pipe(Effect.scoped, Effect.provide(npmLayer(path.join(tmp.path, "cache"))), Effect.runPromise)
  54. expect(Option.isSome(entry.entrypoint)).toBe(true)
  55. })
  56. })
  57. describe("Npm.install", () => {
  58. test("respects omit from project .npmrc", async () => {
  59. await using tmp = await tmpdir()
  60. await writePackage(tmp.path, {
  61. name: "fixture",
  62. dependencies: {
  63. "prod-pkg": "file:./prod-pkg",
  64. },
  65. devDependencies: {
  66. "dev-pkg": "file:./dev-pkg",
  67. },
  68. })
  69. await Bun.write(path.join(tmp.path, ".npmrc"), "omit=dev\n")
  70. await fs.mkdir(path.join(tmp.path, "prod-pkg"))
  71. await fs.mkdir(path.join(tmp.path, "dev-pkg"))
  72. await writePackage(path.join(tmp.path, "prod-pkg"), { name: "prod-pkg" })
  73. await writePackage(path.join(tmp.path, "dev-pkg"), { name: "dev-pkg" })
  74. await Npm.install(tmp.path)
  75. await expect(fs.stat(path.join(tmp.path, "node_modules", "prod-pkg"))).resolves.toBeDefined()
  76. await expect(fs.stat(path.join(tmp.path, "node_modules", "dev-pkg"))).rejects.toThrow()
  77. })
  78. })