1
0

import-boundaries.test.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. const network = await bundleInputs("@opencode-ai/client/effect", "browser")
  20. expect(within(network, effect).length).toBeGreaterThan(0)
  21. expect(within(network, schema).length).toBeGreaterThan(0)
  22. expect(within(network, protocol).length).toBeGreaterThan(0)
  23. expect(within(network, core)).toEqual([])
  24. expect(within(network, server)).toEqual([])
  25. })
  26. })
  27. async function bundleInputs(specifier: string, target: "browser" | "bun") {
  28. const temporary = await mkdtemp(join(import.meta.dir, ".import-boundary-"))
  29. const entrypoint = join(temporary, "index.ts")
  30. const metafile = join(temporary, "meta.json")
  31. try {
  32. await Bun.write(entrypoint, `export * from ${JSON.stringify(specifier)}`)
  33. const child = Bun.spawn(
  34. [
  35. process.execPath,
  36. "build",
  37. entrypoint,
  38. `--target=${target}`,
  39. "--format=esm",
  40. "--packages=bundle",
  41. `--metafile=${metafile}`,
  42. `--outdir=${join(temporary, "out")}`,
  43. ],
  44. { cwd: directory, stdout: "pipe", stderr: "pipe" },
  45. )
  46. const [exitCode, stdout, stderr] = await Promise.all([
  47. child.exited,
  48. new Response(child.stdout).text(),
  49. new Response(child.stderr).text(),
  50. ])
  51. if (exitCode !== 0) throw new Error(stdout + stderr)
  52. const metadata = await Bun.file(metafile).json()
  53. return Object.keys(metadata.inputs).map((input) => resolve(directory, input))
  54. } finally {
  55. await rm(temporary, { recursive: true, force: true })
  56. }
  57. }
  58. function within(inputs: ReadonlyArray<string>, directory: string) {
  59. const prefix = directory.endsWith(sep) ? directory : directory + sep
  60. return inputs.filter((input) => input === directory || input.startsWith(prefix))
  61. }