import-boundaries.test.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { expect, test } from "bun:test"
  2. import { mkdtemp, rm } from "node:fs/promises"
  3. import { join, resolve, sep } from "node:path"
  4. const directory = resolve(import.meta.dir, "..")
  5. const client = resolve(import.meta.dir, "../../client")
  6. const core = resolve(import.meta.dir, "../../core")
  7. const server = resolve(import.meta.dir, "../../server")
  8. test("bundles the client and in-memory host", async () => {
  9. const inputs = await bundleInputs()
  10. expect(within(inputs, client).length).toBeGreaterThan(0)
  11. expect(within(inputs, core).length).toBeGreaterThan(0)
  12. expect(within(inputs, server).length).toBeGreaterThan(0)
  13. })
  14. async function bundleInputs() {
  15. const temporary = await mkdtemp(join(import.meta.dir, ".import-boundary-"))
  16. const entrypoint = join(temporary, "index.ts")
  17. const metafile = join(temporary, "meta.json")
  18. try {
  19. await Bun.write(entrypoint, 'export * from "@opencode-ai/sdk-next"')
  20. const child = Bun.spawn(
  21. [
  22. process.execPath,
  23. "build",
  24. entrypoint,
  25. "--target=bun",
  26. "--format=esm",
  27. "--packages=bundle",
  28. `--metafile=${metafile}`,
  29. `--outdir=${join(temporary, "out")}`,
  30. ],
  31. { cwd: directory, stdout: "pipe", stderr: "pipe" },
  32. )
  33. const [exitCode, stdout, stderr] = await Promise.all([
  34. child.exited,
  35. new Response(child.stdout).text(),
  36. new Response(child.stderr).text(),
  37. ])
  38. if (exitCode !== 0) throw new Error(stdout + stderr)
  39. const metadata = await Bun.file(metafile).json()
  40. return Object.keys(metadata.inputs).map((input) => resolve(directory, input))
  41. } finally {
  42. await rm(temporary, { recursive: true, force: true })
  43. }
  44. }
  45. function within(inputs: ReadonlyArray<string>, directory: string) {
  46. const prefix = directory.endsWith(sep) ? directory : directory + sep
  47. return inputs.filter((input) => input === directory || input.startsWith(prefix))
  48. }