npm-config.test.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import path from "path"
  2. import { describe, expect, test } from "bun:test"
  3. import { Effect } from "effect"
  4. import { NpmConfig } from "@opencode-ai/util/npm-config"
  5. import { tmpdir } from "./fixture/tmpdir"
  6. describe("NpmConfig.load", () => {
  7. test("reads registry from project .npmrc", async () => {
  8. await using tmp = await tmpdir()
  9. await Bun.write(path.join(tmp.path, ".npmrc"), "registry=https://registry.example.test/\n")
  10. const config = await Effect.runPromise(NpmConfig.load(tmp.path))
  11. expect(config.registry).toBe("https://registry.example.test/")
  12. })
  13. test("reads scoped registries from project .npmrc", async () => {
  14. await using tmp = await tmpdir()
  15. await Bun.write(path.join(tmp.path, ".npmrc"), "@acme:registry=https://npm.acme.test/\n")
  16. const config = await Effect.runPromise(NpmConfig.load(tmp.path))
  17. expect(config["@acme:registry"]).toBe("https://npm.acme.test/")
  18. })
  19. test("flattens boolean and list options", async () => {
  20. await using tmp = await tmpdir()
  21. await Bun.write(path.join(tmp.path, ".npmrc"), "ignore-scripts=true\nomit[]=dev\nomit[]=optional\n")
  22. const config = await Effect.runPromise(NpmConfig.load(tmp.path))
  23. expect(config.ignoreScripts).toBe(true)
  24. expect(config.omit).toEqual(["dev", "optional"])
  25. })
  26. })
  27. describe("NpmConfig.registry", () => {
  28. test("normalizes configured registry without trailing slash", async () => {
  29. await using tmp = await tmpdir()
  30. await Bun.write(path.join(tmp.path, ".npmrc"), "registry=https://registry.example.test/\n")
  31. await expect(Effect.runPromise(NpmConfig.registry(tmp.path))).resolves.toBe("https://registry.example.test")
  32. })
  33. test("leaves configured registry without trailing slash unchanged", async () => {
  34. await using tmp = await tmpdir()
  35. await Bun.write(path.join(tmp.path, ".npmrc"), "registry=https://registry.example.test\n")
  36. await expect(Effect.runPromise(NpmConfig.registry(tmp.path))).resolves.toBe("https://registry.example.test")
  37. })
  38. })