import { describe, expect, it } from "vitest";
import { VectordriveManager, textToVector } from "../manager.js";

describe("VectordriveManager", () => {
	it("should return singleton instance", () => {
		const a = VectordriveManager.getInstance();
		const b = VectordriveManager.getInstance();
		expect(a).toBe(b);
	});

	it("should degrade gracefully when vectordrive is not installed", async () => {
		const manager = VectordriveManager.getInstance();
		await manager.close();

		const status = await manager.getStatus();
		expect(status.initialized).toBe(true);
		expect(status.backend).toBe("none");
		expect(status.error).toBeTruthy();
	});
});

describe("textToVector", () => {
	it("should produce normalized vectors", () => {
		const v = textToVector("hello world", 384);
		expect(v).toHaveLength(384);
		const mag = Math.sqrt(v.reduce((s, x) => s + x * x, 0));
		expect(mag).toBeCloseTo(1, 5);
	});

	it("should produce different vectors for different texts", () => {
		const a = textToVector("authentication middleware for express", 384);
		const b = textToVector("database migration helper in python", 384);
		const similarity = a.reduce((s, x, i) => s + x * b[i], 0);
		expect(similarity).toBeLessThan(0.95);
	});
});
