import assert from "node:assert/strict";
import test from "node:test";
import { cosine, embedLocal, VECTOR_DIM } from "@/lib/rag/embedLocal";

test("embedLocal is 384-d and unit-ish", () => {
  const v = embedLocal("mortgage protection loan");
  assert.equal(v.length, VECTOR_DIM);
  const n = Math.sqrt(v.reduce((s, x) => s + x * x, 0));
  assert.ok(Math.abs(n - 1) < 1e-6);
});

test("embedLocal empty tokens still returns vector", () => {
  const v = embedLocal("???");
  assert.equal(v.length, VECTOR_DIM);
});

test("cosine identical vectors is 1", () => {
  const a = embedLocal("hello world");
  assert.ok(cosine(a, a) > 0.99);
  assert.ok(cosine(a, embedLocal("zzzz unrelated qqq")) < cosine(a, embedLocal("hello")));
});
