Files
OmniRoute/tests/e2e/memory-settings.spec.ts
diegosouzapw fceb9d4145 fix(core): resolve runtime edge cases and TypeScript regressions
Tighten request and provider typing across the SSE pipeline to fix
nullability and inference issues in Claude compatibility, wildcard
routing, usage tracking, proxy fetch, and response sanitization.

Address runtime edge cases by normalizing Bailian hosts, guarding TLS
session creation, preserving custom provider base URLs, and using safer
OAuth form param construction during token refresh flows.

Update dashboard data path exports and usage stats typing, and align
E2E/unit tests with paginated API responses, internal model sync auth,
and current response payload shapes.
2026-04-17 20:02:45 -03:00

212 lines
6.9 KiB
TypeScript

import { expect, test, type Page, type Route } from "@playwright/test";
const NAVIGATION_TIMEOUT_MS = 300_000;
type MemoryConfig = {
enabled: boolean;
maxTokens: number;
retentionDays: number;
strategy: "recent" | "semantic" | "hybrid";
skillsEnabled: boolean;
};
type MemoryEntry = {
id: string;
apiKeyId: string;
sessionId: string | null;
type: "factual" | "episodic" | "procedural" | "semantic";
key: string;
content: string;
metadata: Record<string, unknown>;
createdAt: string;
updatedAt: string;
expiresAt: string | null;
};
async function fulfillJson(route: Route, body: unknown, status = 200) {
await route.fulfill({
status,
contentType: "application/json",
body: JSON.stringify(body),
});
}
async function gotoOrSkip(page: Page, url: string) {
let lastError: unknown;
for (let attempt = 0; attempt < 2; attempt += 1) {
try {
await page.goto(url, { waitUntil: "commit", timeout: NAVIGATION_TIMEOUT_MS });
} catch (error) {
lastError = error;
}
try {
await page.waitForURL(/\/(login|dashboard)(\/.*)?$/, { timeout: NAVIGATION_TIMEOUT_MS });
await page.locator("body").waitFor({ state: "visible", timeout: NAVIGATION_TIMEOUT_MS });
lastError = null;
break;
} catch (error) {
lastError = error;
}
await page.waitForTimeout(1000);
}
if (lastError) throw lastError;
const redirectedToLogin = page.url().includes("/login");
test.skip(redirectedToLogin, "Authentication enabled without a login fixture.");
}
async function setRangeValue(page: Page, testId: string, value: number) {
await page.getByTestId(testId).evaluate((element, nextValue) => {
const input = element as HTMLInputElement;
const valueSetter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")?.set;
valueSetter?.call(input, String(nextValue));
input.dispatchEvent(new Event("input", { bubbles: true }));
input.dispatchEvent(new Event("change", { bubbles: true }));
}, value);
}
test.describe("Memory settings", () => {
test.setTimeout(600_000);
test("updates memory config in settings and deletes stored memory entries", async ({ page }) => {
const state: {
config: MemoryConfig;
settings: { skillsmpApiKey: string };
memories: MemoryEntry[];
updateCalls: number;
deleteCalls: number;
} = {
config: {
enabled: false,
maxTokens: 2000,
retentionDays: 30,
strategy: "hybrid",
skillsEnabled: false,
},
settings: {
skillsmpApiKey: "",
},
memories: [
{
id: "mem-1",
apiKeyId: "key-1",
sessionId: "session-a",
type: "factual",
key: "preferred_language",
content: "The user prefers answers in Portuguese.",
metadata: {},
createdAt: new Date("2026-04-05T20:00:00.000Z").toISOString(),
updatedAt: new Date("2026-04-05T20:00:00.000Z").toISOString(),
expiresAt: null,
},
],
updateCalls: 0,
deleteCalls: 0,
};
await page.route("**/api/settings", async (route) => {
const method = route.request().method();
if (method === "GET") {
await fulfillJson(route, state.settings);
return;
}
if (method === "PATCH") {
const payload = (route.request().postDataJSON() as Record<string, unknown>) || {};
state.settings = {
...state.settings,
...(typeof payload.skillsmpApiKey === "string"
? { skillsmpApiKey: payload.skillsmpApiKey }
: {}),
};
await fulfillJson(route, state.settings);
return;
}
await fulfillJson(route, { error: "Method not allowed in settings stub" }, 405);
});
await page.route("**/api/settings/memory", async (route) => {
const method = route.request().method();
if (method === "GET") {
await fulfillJson(route, state.config);
return;
}
if (method === "PUT") {
state.updateCalls += 1;
const payload = (route.request().postDataJSON() as Partial<MemoryConfig>) || {};
state.config = {
...state.config,
...payload,
};
await fulfillJson(route, state.config);
return;
}
await fulfillJson(route, { error: "Method not allowed in memory settings stub" }, 405);
});
await page.route(/\/api\/memory(?:\?.*)?$/, async (route) => {
await fulfillJson(route, {
data: state.memories,
total: state.memories.length,
totalPages: 1,
stats: {
total: state.memories.length,
tokensUsed: state.memories.length * 24,
hitRate: state.memories.length > 0 ? 0.75 : 0,
},
});
});
await page.route(/\/api\/memory\/[^/]+$/, async (route) => {
state.deleteCalls += 1;
const memoryId = route.request().url().split("/").pop() || "";
state.memories = state.memories.filter((memory) => memory.id !== memoryId);
await fulfillJson(route, { success: true });
});
await gotoOrSkip(page, "/dashboard/settings?tab=ai");
let settingsHydrationRetries = 0;
await expect(async () => {
if (settingsHydrationRetries++ > 0) {
await page.reload({ waitUntil: "commit" }).catch(() => {});
}
await expect(page.getByTestId("memory-enabled-switch")).toBeVisible({ timeout: 15000 });
}).toPass({ timeout: 45_000, intervals: [1000, 2500, 5000] });
await expect(page.getByTestId("memory-enabled-switch")).toHaveAttribute(
"aria-checked",
"false"
);
await page.getByTestId("memory-enabled-switch").click();
await expect(page.getByTestId("memory-enabled-switch")).toHaveAttribute("aria-checked", "true");
await expect.poll(() => state.config.enabled).toBe(true);
await setRangeValue(page, "memory-retention-slider", 45);
await expect.poll(() => state.config.retentionDays).toBe(45);
await page.getByTestId("memory-strategy-recent").click();
await expect.poll(() => state.config.strategy).toBe("recent");
await expect.poll(() => state.updateCalls).toBeGreaterThanOrEqual(3);
await page.getByTestId("memory-enabled-switch").click();
await expect(page.getByTestId("memory-enabled-switch")).toHaveAttribute(
"aria-checked",
"false"
);
await expect.poll(() => state.config.enabled).toBe(false);
await gotoOrSkip(page, "/dashboard/memory");
let memoryHydrationRetries = 0;
await expect(async () => {
if (memoryHydrationRetries++ > 0) {
await page.reload({ waitUntil: "commit" }).catch(() => {});
}
await expect(page.getByText("preferred_language")).toBeVisible({ timeout: 15000 });
}).toPass({ timeout: 45_000, intervals: [1000, 2500, 5000] });
await page.getByRole("button", { name: /delete/i }).click();
await expect.poll(() => state.deleteCalls).toBe(1);
await expect(page.getByText("preferred_language")).toHaveCount(0);
});
});