File size: 6,436 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { expect, test, type Page, type Route } from "@playwright/test";
import { gotoDashboardRoute } from "./helpers/dashboardAuth";

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 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 gotoDashboardRoute(page, "/dashboard/settings/ai", {
      timeoutMs: NAVIGATION_TIMEOUT_MS,
    });

    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 gotoDashboardRoute(page, "/dashboard/memory", {
      timeoutMs: NAVIGATION_TIMEOUT_MS,
    });

    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);
  });
});