lazy-module-load.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { spawnSync } from "node:child_process";
  2. import { dirname, resolve } from "node:path";
  3. import { fileURLToPath } from "node:url";
  4. import { describe, expect, it } from "vitest";
  5. const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
  6. const aiEntryUrl = new URL("../src/index.ts", import.meta.url).href;
  7. const SDK_SPECIFIERS = [
  8. "@anthropic-ai/sdk",
  9. "openai",
  10. "@google/genai",
  11. "@mistralai/mistralai",
  12. "@aws-sdk/client-bedrock-runtime",
  13. ] as const;
  14. type ProbeResult = {
  15. loadedSpecifiers: string[];
  16. };
  17. function runProbe(action: string): ProbeResult {
  18. const script = `
  19. import { registerHooks } from "node:module";
  20. const targets = new Set(${JSON.stringify(SDK_SPECIFIERS)});
  21. const loaded = [];
  22. registerHooks({
  23. resolve(specifier, context, nextResolve) {
  24. if (targets.has(specifier)) {
  25. loaded.push(specifier);
  26. }
  27. return nextResolve(specifier, context);
  28. },
  29. });
  30. const mod = await import(${JSON.stringify(aiEntryUrl)});
  31. ${action}
  32. console.log(JSON.stringify({ loadedSpecifiers: [...new Set(loaded)] }));
  33. `;
  34. const result = spawnSync(process.execPath, ["--input-type=module", "--eval", script], {
  35. cwd: packageRoot,
  36. encoding: "utf8",
  37. });
  38. if (result.status !== 0) {
  39. throw new Error(`Probe failed (exit ${result.status})\nSTDOUT:\n${result.stdout}\nSTDERR:\n${result.stderr}`);
  40. }
  41. const stdoutLines = result.stdout
  42. .split(/\r?\n/)
  43. .map((line) => line.trim())
  44. .filter((line) => line.length > 0);
  45. const lastLine = stdoutLines.at(-1);
  46. if (!lastLine) {
  47. throw new Error(`Probe produced no output\nSTDERR:\n${result.stderr}`);
  48. }
  49. return JSON.parse(lastLine) as ProbeResult;
  50. }
  51. describe("lazy provider module loading", () => {
  52. it("does not load provider SDKs when importing the root barrel", () => {
  53. const result = runProbe("");
  54. expect(result.loadedSpecifiers).toEqual([]);
  55. });
  56. it("loads only the Anthropic SDK when calling the root lazy wrapper", () => {
  57. const result = runProbe(`
  58. const model = {
  59. id: "claude-sonnet-4-6",
  60. name: "Claude Sonnet 4",
  61. api: "anthropic-messages",
  62. provider: "anthropic",
  63. baseUrl: "https://api.anthropic.com",
  64. reasoning: true,
  65. input: ["text"],
  66. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
  67. contextWindow: 200000,
  68. maxTokens: 8192,
  69. };
  70. const context = { messages: [{ role: "user", content: "hi" }] };
  71. await mod.streamSimpleAnthropic(model, context).result();
  72. `);
  73. expect(result.loadedSpecifiers).toEqual(["@anthropic-ai/sdk"]);
  74. });
  75. it("loads only the Anthropic SDK when dispatching through streamSimple", () => {
  76. const result = runProbe(`
  77. const model = mod.getModel("anthropic", "claude-sonnet-4-6");
  78. const context = { messages: [{ role: "user", content: "hi" }] };
  79. await mod.streamSimple(model, context).result();
  80. `);
  81. expect(result.loadedSpecifiers).toEqual(["@anthropic-ai/sdk"]);
  82. });
  83. });