From 388b61a099eacf842b2b4d2491202d23957d8f20 Mon Sep 17 00:00:00 2001 From: kartik-mem0 Date: Thu, 19 Mar 2026 15:50:43 +0530 Subject: [PATCH] test: add cleanup hooks and refactor integration test helpers --- mem0-ts/jest.integration.config.js | 6 +- .../src/client/tests/integration/crud.test.ts | 5 +- .../src/client/tests/integration/helpers.ts | 11 +- .../tests/integration/management.test.ts | 125 +++++++++--------- 4 files changed, 70 insertions(+), 77 deletions(-) diff --git a/mem0-ts/jest.integration.config.js b/mem0-ts/jest.integration.config.js index 8c5894749..50eb8e21e 100644 --- a/mem0-ts/jest.integration.config.js +++ b/mem0-ts/jest.integration.config.js @@ -2,8 +2,6 @@ module.exports = { ...require("./jest.config"), testMatch: ["**/integration/**/*.test.ts"], - globalSetup: - "/src/client/tests/integration/global-setup.ts", - globalTeardown: - "/src/client/tests/integration/global-teardown.ts", + globalSetup: "/src/client/tests/integration/global-setup.ts", + globalTeardown: "/src/client/tests/integration/global-teardown.ts", }; diff --git a/mem0-ts/src/client/tests/integration/crud.test.ts b/mem0-ts/src/client/tests/integration/crud.test.ts index 4c1b60663..8600d6571 100644 --- a/mem0-ts/src/client/tests/integration/crud.test.ts +++ b/mem0-ts/src/client/tests/integration/crud.test.ts @@ -200,7 +200,10 @@ describeIntegration("MemoryClient Integration — CRUD", () => { const result = await client.add( [ { role: "user" as const, content: "I prefer dark mode in all apps." }, - { role: "assistant" as const, content: "Noted, dark mode preference saved!" }, + { + role: "assistant" as const, + content: "Noted, dark mode preference saved!", + }, ], { user_id: TEST_USER_ID, diff --git a/mem0-ts/src/client/tests/integration/helpers.ts b/mem0-ts/src/client/tests/integration/helpers.ts index e1fcf9001..c48e4647b 100644 --- a/mem0-ts/src/client/tests/integration/helpers.ts +++ b/mem0-ts/src/client/tests/integration/helpers.ts @@ -60,9 +60,7 @@ export async function waitForMemories( ): Promise { const start = Date.now(); while (Date.now() - start < maxWaitMs) { - const memories = await withRetry(() => - client.getAll({ user_id: userId }), - ); + const memories = await withRetry(() => client.getAll({ user_id: userId })); if (Array.isArray(memories) && memories.length >= minCount) { return memories; } @@ -133,8 +131,7 @@ export async function seedTestMemories( [ { role: "user" as const, - content: - "Hi, I'm integration-test-user. My favorite color is blue.", + content: "Hi, I'm integration-test-user. My favorite color is blue.", }, { role: "assistant" as const, @@ -193,9 +190,7 @@ export async function cleanupTestUser( * Used as cleanup before and after integration test runs so tests * start from a clean slate and don't leave data behind. */ -export async function fullProjectCleanup( - client: MemoryClient, -): Promise { +export async function fullProjectCleanup(client: MemoryClient): Promise { // Delete all memories — all four filters set explicitly try { await client.deleteAll({ diff --git a/mem0-ts/src/client/tests/integration/management.test.ts b/mem0-ts/src/client/tests/integration/management.test.ts index c49a9c213..95df93b6f 100644 --- a/mem0-ts/src/client/tests/integration/management.test.ts +++ b/mem0-ts/src/client/tests/integration/management.test.ts @@ -23,79 +23,76 @@ jest.setTimeout(120_000); const TEST_USER_ID = `integration-mgmt-${randomUUID()}`; -describeIntegration( - "MemoryClient Integration — Users & Project", - () => { - let client: MemoryClient; - let cleanup: () => void; +describeIntegration("MemoryClient Integration — Users & Project", () => { + let client: MemoryClient; + let cleanup: () => void; - beforeAll(async () => { - cleanup = suppressTelemetryNoise(); - client = createTestClient(); - await seedTestMemories(client, TEST_USER_ID); + beforeAll(async () => { + cleanup = suppressTelemetryNoise(); + client = createTestClient(); + await seedTestMemories(client, TEST_USER_ID); + }); + + afterAll(async () => { + await cleanupTestUser(client, TEST_USER_ID); + cleanup(); + }); + + // ─── Users ──────────────────────────────────────────────── + describe("user management", () => { + test("lists users and finds test user", async () => { + const allUsers = await client.users(); + + expect(typeof allUsers.count).toBe("number"); + expect(Array.isArray(allUsers.results)).toBe(true); + + if (allUsers.results.length > 0) { + const user = allUsers.results[0]; + expect(typeof user.id).toBe("string"); + expect(typeof user.name).toBe("string"); + expect(typeof user.type).toBe("string"); + } + + const testUser = allUsers.results.find((u) => u.name === TEST_USER_ID); + expect(testUser).toBeDefined(); }); + }); - afterAll(async () => { - await cleanupTestUser(client, TEST_USER_ID); - cleanup(); - }); + // ─── Project ────────────────────────────────────────────── + describe("project management", () => { + let originalInstructions: string | undefined; - // ─── Users ──────────────────────────────────────────────── - describe("user management", () => { - test("lists users and finds test user", async () => { - const allUsers = await client.users(); - - expect(typeof allUsers.count).toBe("number"); - expect(Array.isArray(allUsers.results)).toBe(true); - - if (allUsers.results.length > 0) { - const user = allUsers.results[0]; - expect(typeof user.id).toBe("string"); - expect(typeof user.name).toBe("string"); - expect(typeof user.type).toBe("string"); - } - - const testUser = allUsers.results.find((u) => u.name === TEST_USER_ID); - expect(testUser).toBeDefined(); - }); - }); - - // ─── Project ────────────────────────────────────────────── - describe("project management", () => { - let originalInstructions: string | undefined; - - test("gets project with custom_instructions field", async () => { - const project = await client.getProject({ - fields: ["custom_instructions"], - }); - - expect(project).toBeDefined(); - expect(typeof project).toBe("object"); - expect("custom_instructions" in project).toBe(true); - - originalInstructions = project.custom_instructions; + test("gets project with custom_instructions field", async () => { + const project = await client.getProject({ + fields: ["custom_instructions"], }); - test("updates project custom_instructions via updateProject()", async () => { - const testInstruction = `integration-test-${randomUUID().slice(0, 8)}`; + expect(project).toBeDefined(); + expect(typeof project).toBe("object"); + expect("custom_instructions" in project).toBe(true); - const result = await client.updateProject({ - custom_instructions: testInstruction, - }); + originalInstructions = project.custom_instructions; + }); - expect(result).toBeDefined(); + test("updates project custom_instructions via updateProject()", async () => { + const testInstruction = `integration-test-${randomUUID().slice(0, 8)}`; - // Verify the update took effect - const project = await client.getProject({ - fields: ["custom_instructions"], - }); - expect(project.custom_instructions).toBe(testInstruction); + const result = await client.updateProject({ + custom_instructions: testInstruction, + }); - // Restore original - await client.updateProject({ - custom_instructions: originalInstructions || "", - }); + expect(result).toBeDefined(); + + // Verify the update took effect + const project = await client.getProject({ + fields: ["custom_instructions"], + }); + expect(project.custom_instructions).toBe(testInstruction); + + // Restore original + await client.updateProject({ + custom_instructions: originalInstructions || "", }); }); - }, -); + }); +});