commercial-model-policy.test.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { readFileSync } from "node:fs";
  2. import { describe, expect, test } from "vitest";
  3. import * as embeddingApi from "../src/embedding/index.js";
  4. import * as sdk from "../src/index.js";
  5. import { LocalLlamaCppProvider } from "../src/embedding/local.js";
  6. import { LlamaCpp, pullModels } from "../src/llm.js";
  7. import {
  8. COMMERCIAL_API_HOLD_CODE,
  9. CommercialApiHoldError,
  10. } from "../src/model-policy.js";
  11. function expectTypedHold(error: unknown): void {
  12. expect(error).toBeInstanceOf(CommercialApiHoldError);
  13. expect(error).toMatchObject({
  14. code: COMMERCIAL_API_HOLD_CODE,
  15. disposition: "HOLD",
  16. });
  17. }
  18. describe("commercial-only package policy", () => {
  19. test("the public SDK does not export local or fallback providers", () => {
  20. for (const api of [sdk, embeddingApi]) {
  21. expect(api).not.toHaveProperty("LocalLlamaCppProvider");
  22. expect(api).not.toHaveProperty("AutoFallbackEmbeddingProvider");
  23. }
  24. });
  25. test("package manifests contain no local model runtime or model artifact dependency", () => {
  26. const manifest = JSON.parse(
  27. readFileSync(new URL("../package.json", import.meta.url), "utf8"),
  28. ) as Record<string, Record<string, string> | undefined>;
  29. const dependencyNames = [
  30. ...Object.keys(manifest.dependencies ?? {}),
  31. ...Object.keys(manifest.optionalDependencies ?? {}),
  32. ...Object.keys(manifest.devDependencies ?? {}),
  33. ];
  34. expect(dependencyNames).not.toContain("node-llama-cpp");
  35. for (const lockFile of ["../pnpm-lock.yaml", "../bun.lock"]) {
  36. const lock = readFileSync(new URL(lockFile, import.meta.url), "utf8");
  37. expect(lock).not.toMatch(/node-llama-cpp|\.gguf\b|huggingface\.co/i);
  38. }
  39. });
  40. test("historical local construction and model pulls return typed HOLD", async () => {
  41. try {
  42. new LocalLlamaCppProvider();
  43. throw new Error("local provider construction unexpectedly succeeded");
  44. } catch (error) {
  45. expectTypedHold(error);
  46. }
  47. try {
  48. await pullModels(["forbidden-model-artifact"]);
  49. throw new Error("model pull unexpectedly succeeded");
  50. } catch (error) {
  51. expectTypedHold(error);
  52. }
  53. });
  54. test("learned compatibility operations fail closed without a commercial adapter", async () => {
  55. const llm = new LlamaCpp();
  56. const operations = [
  57. () => llm.embed("text"),
  58. () => llm.embedBatch(["text"]),
  59. () => llm.generate("prompt"),
  60. () => llm.expandQuery("query"),
  61. () => llm.rerank("query", [{ file: "doc", text: "body" }]),
  62. ];
  63. for (const operation of operations) {
  64. try {
  65. await operation();
  66. throw new Error("learned operation unexpectedly succeeded");
  67. } catch (error) {
  68. expectTypedHold(error);
  69. }
  70. }
  71. });
  72. });