Skip to content

Persistent project knowledge graph for coding agents. MCP server with semantic search, in-process embeddings, and web explorer.

test: add coverage for embedding validation and Buffer deserialization fixes

0xk3vin23e91cd5d06e
diff --git a/package-lock.json b/package-lock.json
index d41c346..671c541 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "megamemory",
- "version": "1.2.0",
+ "version": "1.2.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "megamemory",
- "version": "1.2.0",
+ "version": "1.2.1",
"license": "MIT",
"dependencies": {
"@modelcontextprotocol/sdk": "^1.12.1",
diff --git a/package.json b/package.json
index a89fd1d..63ba432 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "megamemory",
- "version": "1.2.0",
+ "version": "1.2.1",
"description": "MCP server that gives coding agents persistent project understanding via a knowledge graph",
"type": "module",
"main": "dist/index.js",
diff --git a/src/__tests__/db.test.ts b/src/__tests__/db.test.ts
index a05dcc8..805a9ba 100644
--- a/src/__tests__/db.test.ts
+++ b/src/__tests__/db.test.ts
@@ -198,6 +198,79 @@ describe("KnowledgeDB", () => {
});
});
+ describe("getAllActiveNodesWithEmbeddings", () => {
+ function makeEmbeddingBuffer(values: number[]): Buffer {
+ const arr = new Float32Array(values);
+ return Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength);
+ }
+
+ it("returns embeddings as Buffer instances with correct float32 data", () => {
+ const original = makeEmbeddingBuffer([1.0, 2.0, 3.0, 4.0]);
+ db.insertNode({
+ id: "with-emb",
+ name: "With Embedding",
+ kind: "feature",
+ summary: "Has an embedding",
+ why: null,
+ file_refs: null,
+ parent_id: null,
+ created_by_task: null,
+ embedding: original,
+ });
+
+ const results = db.getAllActiveNodesWithEmbeddings();
+ expect(results).toHaveLength(1);
+ expect(results[0].id).toBe("with-emb");
+
+ const emb = results[0].embedding!;
+ expect(Buffer.isBuffer(emb)).toBe(true);
+
+ // Verify the float32 values round-trip correctly
+ const floats = new Float32Array(emb.buffer, emb.byteOffset, emb.byteLength / Float32Array.BYTES_PER_ELEMENT);
+ expect(floats[0]).toBeCloseTo(1.0);
+ expect(floats[1]).toBeCloseTo(2.0);
+ expect(floats[2]).toBeCloseTo(3.0);
+ expect(floats[3]).toBeCloseTo(4.0);
+ });
+
+ it("excludes nodes without embeddings", () => {
+ db.insertNode({
+ id: "no-emb",
+ name: "No Embedding",
+ kind: "feature",
+ summary: "Missing embedding",
+ why: null,
+ file_refs: null,
+ parent_id: null,
+ created_by_task: null,
+ embedding: null,
+ });
+
+ const results = db.getAllActiveNodesWithEmbeddings();
+ expect(results).toHaveLength(0);
+ });
+
+ it("excludes soft-deleted nodes", () => {
+ const emb = makeEmbeddingBuffer([1.0, 2.0]);
+ db.insertNode({
+ id: "deleted-emb",
+ name: "Deleted",
+ kind: "feature",
+ summary: "Will be deleted",
+ why: null,
+ file_refs: null,
+ parent_id: null,
+ created_by_task: null,
+ embedding: emb,
+ });
+
+ db.softDeleteNode("deleted-emb", "No longer needed");
+
+ const results = db.getAllActiveNodesWithEmbeddings();
+ expect(results).toHaveLength(0);
+ });
+ });
+
describe("edges", () => {
beforeEach(() => {
db.insertNode({
diff --git a/src/__tests__/embeddings.test.ts b/src/__tests__/embeddings.test.ts
index d7512bd..753cb61 100644
--- a/src/__tests__/embeddings.test.ts
+++ b/src/__tests__/embeddings.test.ts
@@ -1,5 +1,6 @@
import { describe, it, expect } from "vitest";
import {
+ embed,
embeddingText,
cosineSimilarity,
findTopK,
@@ -20,6 +21,20 @@ describe("EMBEDDING_DIM", () => {
});
});
+describe("embed", () => {
+ it("rejects empty string", async () => {
+ await expect(embed("")).rejects.toThrow("Cannot embed empty text");
+ });
+
+ it("rejects whitespace-only string", async () => {
+ await expect(embed(" ")).rejects.toThrow("Cannot embed empty text");
+ });
+
+ it("rejects newline/tab-only string", async () => {
+ await expect(embed("\n\t \n")).rejects.toThrow("Cannot embed empty text");
+ });
+});
+
describe("embeddingText", () => {
it("formats kind, name, and summary into a template string", () => {
const result = embeddingText("Auth Module", "module", "Handles JWT validation");
@@ -104,4 +119,17 @@ describe("findTopK", () => {
const results = findTopK(queryEmbedding, [], 5);
expect(results).toEqual([]);
});
+
+ it("filters out candidates with zero-length embedding buffers", () => {
+ const candidatesWithEmpty = [
+ { id: "valid", name: "Valid", kind: "feature", summary: "", embedding: mockEmbedding([1, 0, 0]) },
+ { id: "empty-buf", name: "Empty", kind: "feature", summary: "", embedding: Buffer.alloc(0) },
+ { id: "null-emb", name: "Null", kind: "feature", summary: "", embedding: null },
+ ];
+ const results = findTopK(queryEmbedding, candidatesWithEmpty, 10);
+ const ids = results.map((r) => r.id);
+ expect(ids).toEqual(["valid"]);
+ expect(ids).not.toContain("empty-buf");
+ expect(ids).not.toContain("null-emb");
+ });
});