import-boundaries.test.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { describe, expect, test } from "bun:test"
  2. import { realpathSync } from "node:fs"
  3. import { mkdtemp, rm } from "node:fs/promises"
  4. import { join, resolve, sep } from "node:path"
  5. const directory = resolve(import.meta.dir, "..")
  6. const effect = realpathSync(resolve(import.meta.dir, "../node_modules/effect"))
  7. const schema = resolve(import.meta.dir, "../../schema")
  8. const protocol = resolve(import.meta.dir, "../../protocol")
  9. const core = resolve(import.meta.dir, "../../core")
  10. const server = resolve(import.meta.dir, "../../server")
  11. describe("public import boundaries", () => {
  12. test("isolates each public entrypoint", async () => {
  13. const root = await bundleInputs("@opencode-ai/client", "browser")
  14. expect(within(root, effect)).toEqual([])
  15. expect(within(root, schema)).toEqual([])
  16. expect(within(root, protocol)).toEqual([])
  17. expect(within(root, core)).toEqual([])
  18. expect(within(root, server)).toEqual([])
  19. // The effect entry includes local service lifecycle (node spawn/fs), so it
  20. // bundles for bun; the boundary assertions below are what matter.
  21. const network = await bundleInputs("@opencode-ai/client/effect", "bun")
  22. expect(within(network, effect).length).toBeGreaterThan(0)
  23. expect(within(network, schema).length).toBeGreaterThan(0)
  24. expect(within(network, protocol).length).toBeGreaterThan(0)
  25. expect(within(network, core)).toEqual([])
  26. expect(within(network, server)).toEqual([])
  27. })
  28. })
  29. async function bundleInputs(specifier: string, target: "browser" | "bun") {
  30. const temporary = await mkdtemp(join(import.meta.dir, ".import-boundary-"))
  31. const entrypoint = join(temporary, "index.ts")
  32. const metafile = join(temporary, "meta.json")
  33. try {
  34. await Bun.write(entrypoint, `export * from ${JSON.stringify(specifier)}`)
  35. const child = Bun.spawn(
  36. [
  37. process.execPath,
  38. "build",
  39. entrypoint,
  40. `--target=${target}`,
  41. "--format=esm",
  42. "--packages=bundle",
  43. `--metafile=${metafile}`,
  44. `--outdir=${join(temporary, "out")}`,
  45. ],
  46. { cwd: directory, stdout: "pipe", stderr: "pipe" },
  47. )
  48. const [exitCode, stdout, stderr] = await Promise.all([
  49. child.exited,
  50. new Response(child.stdout).text(),
  51. new Response(child.stderr).text(),
  52. ])
  53. if (exitCode !== 0) throw new Error(stdout + stderr)
  54. const metadata = await Bun.file(metafile).json()
  55. return Object.keys(metadata.inputs).map((input) => resolve(directory, input))
  56. } finally {
  57. await rm(temporary, { recursive: true, force: true })
  58. }
  59. }
  60. function within(inputs: ReadonlyArray<string>, directory: string) {
  61. const prefix = directory.endsWith(sep) ? directory : directory + sep
  62. return inputs.filter((input) => input === directory || input.startsWith(prefix))
  63. }