/** Unit tests for security guard and response envelope utilities in TS. */ import { describe, it, expect } from "vitest"; import { makeEnvelope, errorEnvelope } from "../../src/utils/response.js"; describe("makeEnvelope", () => { it("creates a healthy envelope with data", () => { const envelope = makeEnvelope( "healthy", ["a", "b"], null, { query_time_ms: 42 } ); expect(envelope.status).toBe("healthy"); expect(envelope.data).toEqual(["a", "b"]); expect(envelope.error).toBeNull(); expect(envelope.meta.query_time_ms).toBe(42); }); it("creates a degraded envelope without data", () => { const envelope = makeEnvelope("degraded", null, null, { chunks_scanned: 0 }); expect(envelope.status).toBe("degraded"); expect(envelope.data).toBeNull(); }); it("defaults meta fields", () => { const envelope = makeEnvelope("healthy", [], null, {}); expect(envelope.meta.index_version).toBe("0.1.0"); expect(envelope.meta.vault_mtime).toBeDefined(); }); }); describe("errorEnvelope", () => { it("creates an unavailable error envelope", () => { const envelope = errorEnvelope( "INDEX_NOT_FOUND", "Vector index not found at expected path", false, "Run 'obsidian-rag index' to create the index" ); expect(envelope.status).toBe("unavailable"); expect(envelope.data).toBeNull(); expect(envelope.error).toEqual({ code: "INDEX_NOT_FOUND", message: "Vector index not found at expected path", recoverable: false, suggestion: "Run 'obsidian-rag index' to create the index", }); }); });