Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ea94ffa63b | ||
|
|
a3d1a1068b | ||
|
|
227c02da9f | ||
|
|
d64d7b525b |
@@ -2,8 +2,6 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
...require("./jest.config"),
|
...require("./jest.config"),
|
||||||
testMatch: ["**/integration/**/*.test.ts"],
|
testMatch: ["**/integration/**/*.test.ts"],
|
||||||
globalSetup: "<rootDir>/src/client/tests/integration/global-setup.ts",
|
|
||||||
globalTeardown: "<rootDir>/src/client/tests/integration/global-teardown.ts",
|
|
||||||
// Run integration tests serially to avoid rate limiting and race conditions
|
// Run integration tests serially to avoid rate limiting and race conditions
|
||||||
maxWorkers: 1,
|
maxWorkers: 1,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,40 +0,0 @@
|
|||||||
/**
|
|
||||||
* Jest global setup for integration tests.
|
|
||||||
*
|
|
||||||
* Runs a full project cleanup before any integration test starts,
|
|
||||||
* then waits 10 seconds for the async cleanup to propagate.
|
|
||||||
*/
|
|
||||||
import { MemoryClient } from "../../mem0";
|
|
||||||
|
|
||||||
export default async function globalSetup() {
|
|
||||||
const apiKey = process.env.MEM0_API_KEY;
|
|
||||||
if (!apiKey) return; // skip if no key — tests will be skipped too
|
|
||||||
|
|
||||||
const client = new MemoryClient({ apiKey });
|
|
||||||
await client.ping();
|
|
||||||
|
|
||||||
console.log("[integration] Running pre-test cleanup...");
|
|
||||||
|
|
||||||
// Full project wipe — all four filters set explicitly
|
|
||||||
try {
|
|
||||||
await client.deleteAll({
|
|
||||||
userId: "*",
|
|
||||||
agentId: "*",
|
|
||||||
appId: "*",
|
|
||||||
runId: "*",
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
// ignore — may 404 if no data exists
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
await client.deleteUsers();
|
|
||||||
} catch {
|
|
||||||
// ignore — may throw "No entities to delete"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait 10 seconds for async cleanup to propagate
|
|
||||||
console.log("[integration] Waiting 10s for cleanup to propagate...");
|
|
||||||
await new Promise((r) => setTimeout(r, 10_000));
|
|
||||||
console.log("[integration] Pre-test cleanup done.");
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
/**
|
|
||||||
* Jest global teardown for integration tests.
|
|
||||||
*
|
|
||||||
* Runs a full project cleanup after all integration tests complete
|
|
||||||
* so no test data is left behind.
|
|
||||||
*/
|
|
||||||
import { MemoryClient } from "../../mem0";
|
|
||||||
|
|
||||||
export default async function globalTeardown() {
|
|
||||||
const apiKey = process.env.MEM0_API_KEY;
|
|
||||||
if (!apiKey) return;
|
|
||||||
|
|
||||||
const client = new MemoryClient({ apiKey });
|
|
||||||
await client.ping();
|
|
||||||
|
|
||||||
console.log("[integration] Running post-test cleanup...");
|
|
||||||
|
|
||||||
try {
|
|
||||||
await client.deleteAll({
|
|
||||||
userId: "*",
|
|
||||||
agentId: "*",
|
|
||||||
appId: "*",
|
|
||||||
runId: "*",
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
await client.deleteUsers();
|
|
||||||
} catch {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("[integration] Post-test cleanup done.");
|
|
||||||
}
|
|
||||||
@@ -52,13 +52,17 @@ export async function withRetry<T>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Poll getAll until memories appear for a user.
|
* Poll getAll until a user's memories reach minCount and the ID set
|
||||||
* The Mem0 API processes memories asynchronously — after add()
|
* stops changing between polls.
|
||||||
* we need to wait for them to be available.
|
*
|
||||||
|
* The Mem0 API processes memories asynchronously, and consolidating a
|
||||||
|
* new fact replaces a memory (delete + add) instead of editing it in
|
||||||
|
* place. Returning on the first non-empty read hands callers IDs the
|
||||||
|
* pipeline then invalidates, so compare IDs rather than length: a
|
||||||
|
* replacement leaves the count unchanged.
|
||||||
*
|
*
|
||||||
* Polls every 15 seconds with a maximum of 4 retries to avoid
|
* Polls every 15 seconds with a maximum of 4 retries to avoid
|
||||||
* hitting rate limits. Throws if results aren't available after
|
* hitting rate limits. Throws if minCount is never reached.
|
||||||
* all retries.
|
|
||||||
*/
|
*/
|
||||||
export async function waitForMemories(
|
export async function waitForMemories(
|
||||||
client: MemoryClient,
|
client: MemoryClient,
|
||||||
@@ -66,18 +70,32 @@ export async function waitForMemories(
|
|||||||
minCount: number,
|
minCount: number,
|
||||||
maxRetries = 4,
|
maxRetries = 4,
|
||||||
): Promise<Memory[]> {
|
): Promise<Memory[]> {
|
||||||
|
let memories: Memory[] = [];
|
||||||
|
let previousIds = "";
|
||||||
|
|
||||||
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
||||||
const response = await withRetry(() =>
|
const response = await withRetry(() =>
|
||||||
client.getAll({ filters: { user_id: userId } }),
|
client.getAll({ filters: { user_id: userId } }),
|
||||||
);
|
);
|
||||||
const memories = response.results ?? [];
|
memories = response.results ?? [];
|
||||||
if (memories.length >= minCount) {
|
|
||||||
|
const ids = memories
|
||||||
|
.map((m) => m.id)
|
||||||
|
.sort()
|
||||||
|
.join(",");
|
||||||
|
if (memories.length >= minCount && ids === previousIds) {
|
||||||
return memories;
|
return memories;
|
||||||
}
|
}
|
||||||
|
previousIds = ids;
|
||||||
|
|
||||||
if (attempt < maxRetries) {
|
if (attempt < maxRetries) {
|
||||||
await new Promise((r) => setTimeout(r, 15_000));
|
await new Promise((r) => setTimeout(r, 15_000));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (memories.length >= minCount) {
|
||||||
|
return memories;
|
||||||
|
}
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`waitForMemories: expected at least ${minCount} memories for user "${userId}" but did not get them after ${maxRetries} attempts`,
|
`waitForMemories: expected at least ${minCount} memories for user "${userId}" but did not get them after ${maxRetries} attempts`,
|
||||||
);
|
);
|
||||||
@@ -202,32 +220,3 @@ export async function cleanupTestUser(
|
|||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Full project wipe — deletes all memories and all entities.
|
|
||||||
* Equivalent to Python SDK's:
|
|
||||||
* client.delete_all(user_id="*", agent_id="*", app_id="*", run_id="*")
|
|
||||||
*
|
|
||||||
* 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<void> {
|
|
||||||
// Delete all memories — all four filters set explicitly
|
|
||||||
try {
|
|
||||||
await client.deleteAll({
|
|
||||||
userId: "*",
|
|
||||||
agentId: "*",
|
|
||||||
appId: "*",
|
|
||||||
runId: "*",
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
// ignore — may 404 if no data exists
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete all entities (users, agents, apps, runs)
|
|
||||||
try {
|
|
||||||
await client.deleteUsers();
|
|
||||||
} catch {
|
|
||||||
// ignore — may throw "No entities to delete"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -100,6 +100,7 @@ describeIntegration("MemoryClient Integration — Users & Project", () => {
|
|||||||
const hookName = `test-hook-${randomUUID().slice(0, 8)}`;
|
const hookName = `test-hook-${randomUUID().slice(0, 8)}`;
|
||||||
const hookUrl = `https://example.com/webhook/${randomUUID().slice(0, 8)}`;
|
const hookUrl = `https://example.com/webhook/${randomUUID().slice(0, 8)}`;
|
||||||
const updatedName = `updated-hook-${randomUUID().slice(0, 8)}`;
|
const updatedName = `updated-hook-${randomUUID().slice(0, 8)}`;
|
||||||
|
const updatedUrl = `https://example.com/updated/${randomUUID().slice(0, 8)}`;
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
if (createdWebhookId) {
|
if (createdWebhookId) {
|
||||||
@@ -168,7 +169,7 @@ describeIntegration("MemoryClient Integration — Users & Project", () => {
|
|||||||
client.updateWebhook({
|
client.updateWebhook({
|
||||||
webhookId: createdWebhookId,
|
webhookId: createdWebhookId,
|
||||||
name: updatedName,
|
name: updatedName,
|
||||||
url: "https://example.com/updated",
|
url: updatedUrl,
|
||||||
eventTypes: [WebhookEvent.MEMORY_DELETED],
|
eventTypes: [WebhookEvent.MEMORY_DELETED],
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
import { waitForMemories } from "./integration/helpers";
|
||||||
|
|
||||||
|
const clientReturning = (polls: Array<Array<{ id: string }>>) => {
|
||||||
|
let call = 0;
|
||||||
|
return {
|
||||||
|
getAll: jest.fn(async () => ({
|
||||||
|
results: polls[Math.min(call++, polls.length - 1)],
|
||||||
|
})),
|
||||||
|
} as any;
|
||||||
|
};
|
||||||
|
|
||||||
|
const settle = async <T>(pending: Promise<T>): Promise<T> => {
|
||||||
|
pending.catch(() => {});
|
||||||
|
for (let i = 0; i < 4; i++) {
|
||||||
|
await jest.advanceTimersByTimeAsync(15_000);
|
||||||
|
}
|
||||||
|
return pending;
|
||||||
|
};
|
||||||
|
|
||||||
|
describe("waitForMemories", () => {
|
||||||
|
beforeEach(() => jest.useFakeTimers());
|
||||||
|
afterEach(() => jest.useRealTimers());
|
||||||
|
|
||||||
|
test("waits for the id set to stop changing", async () => {
|
||||||
|
const client = clientReturning([
|
||||||
|
[{ id: "a" }],
|
||||||
|
[{ id: "a" }, { id: "b" }],
|
||||||
|
[{ id: "a" }, { id: "b" }],
|
||||||
|
]);
|
||||||
|
|
||||||
|
const memories = await settle(waitForMemories(client, "user", 1));
|
||||||
|
|
||||||
|
expect(memories.map((m) => m.id)).toEqual(["a", "b"]);
|
||||||
|
expect(client.getAll).toHaveBeenCalledTimes(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("keeps polling when consolidation replaces a memory at a constant count", async () => {
|
||||||
|
const client = clientReturning([
|
||||||
|
[{ id: "a" }, { id: "b" }],
|
||||||
|
[{ id: "a" }, { id: "c" }],
|
||||||
|
[{ id: "a" }, { id: "c" }],
|
||||||
|
]);
|
||||||
|
|
||||||
|
const memories = await settle(waitForMemories(client, "user", 1));
|
||||||
|
|
||||||
|
expect(memories.map((m) => m.id)).toEqual(["a", "c"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns the last read when the set never settles but minCount is met", async () => {
|
||||||
|
const client = clientReturning([
|
||||||
|
[{ id: "a" }],
|
||||||
|
[{ id: "b" }],
|
||||||
|
[{ id: "c" }],
|
||||||
|
[{ id: "d" }],
|
||||||
|
]);
|
||||||
|
|
||||||
|
const memories = await settle(waitForMemories(client, "user", 1));
|
||||||
|
|
||||||
|
expect(memories.map((m) => m.id)).toEqual(["d"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("throws when minCount is never reached", async () => {
|
||||||
|
const client = clientReturning([[]]);
|
||||||
|
|
||||||
|
await expect(settle(waitForMemories(client, "user", 1))).rejects.toThrow(
|
||||||
|
/expected at least 1 memories/,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user