106 lines
3.0 KiB
TypeScript
106 lines
3.0 KiB
TypeScript
import { afterEach, describe, expect, it } from "vitest";
|
|
|
|
import type { AgentResponse } from "@agent/types";
|
|
import type { DerivedState } from "./onboarding-state.js";
|
|
import { UserMemoryStore } from "./user-memory.js";
|
|
|
|
class MemoryGarnet {
|
|
readonly records = new Map<string, unknown>();
|
|
readonly ttls = new Map<string, number | undefined>();
|
|
|
|
getJson<T>(key: string): Promise<T | null> {
|
|
return Promise.resolve((this.records.get(key) as T | undefined) ?? null);
|
|
}
|
|
|
|
setJson(
|
|
key: string,
|
|
value: unknown,
|
|
ttlSeconds?: number,
|
|
): Promise<void> {
|
|
this.records.set(key, value);
|
|
this.ttls.set(key, ttlSeconds);
|
|
return Promise.resolve();
|
|
}
|
|
}
|
|
|
|
function derived(overrides?: Partial<DerivedState>): DerivedState {
|
|
return {
|
|
allowShortResponse: false,
|
|
intent: { kind: "nickname-question" },
|
|
phase: { kind: "nickname-rejected", current: "bad", issues: [] },
|
|
responseMode: "llm-with-tools",
|
|
shouldRespond: true,
|
|
suggestedMaxTokens: 256,
|
|
tier: 1,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function response(text: string): AgentResponse {
|
|
return {
|
|
confidence: 0.9,
|
|
requestId: "req-1",
|
|
source: "llm",
|
|
text,
|
|
};
|
|
}
|
|
|
|
describe("UserMemoryStore", () => {
|
|
afterEach(() => {
|
|
delete process.env["AGENT_USER_MEMORY_TTL_DAYS"];
|
|
});
|
|
|
|
it("stores anonymized user ids and redacted issue strings", async () => {
|
|
process.env["AGENT_USER_MEMORY_TTL_DAYS"] = "7";
|
|
const garnet = new MemoryGarnet();
|
|
const store = new UserMemoryStore(garnet);
|
|
|
|
const profile = await store.observeTurn(
|
|
"raw-user-id",
|
|
derived(),
|
|
response("닉네임은 이름/95/여/D 형식으로 바꿔주시면 돼요!"),
|
|
{
|
|
flaggedIssues: [
|
|
"phone 010-1234-5678, email test@example.com should not persist",
|
|
],
|
|
now: "2026-05-01T00:00:00.000Z",
|
|
},
|
|
);
|
|
|
|
expect(profile.userId).toMatch(/^user_[a-f0-9]{8}$/u);
|
|
expect(profile.userId).not.toContain("raw-user-id");
|
|
expect(profile.flaggedIssues[0]).toContain("[REDACTED:phone]");
|
|
expect(profile.flaggedIssues[0]).toContain("[REDACTED:email]");
|
|
expect(profile.preferredStyle).toBe("warm");
|
|
expect([...garnet.ttls.values()][0]).toBe(7 * 24 * 60 * 60);
|
|
});
|
|
|
|
it("accumulates intent counts and completion time", async () => {
|
|
const garnet = new MemoryGarnet();
|
|
const store = new UserMemoryStore(garnet);
|
|
|
|
await store.observeTurn(
|
|
"user-1",
|
|
derived({ intent: { kind: "photo-question" } }),
|
|
response("하트 인증만 해주시면 돼요."),
|
|
{ now: "2026-05-01T00:00:00.000Z" },
|
|
);
|
|
const profile = await store.observeTurn(
|
|
"user-1",
|
|
derived({
|
|
intent: { kind: "photo-question" },
|
|
phase: { kind: "completed" },
|
|
}),
|
|
response("완료됐어요."),
|
|
{ now: "2026-05-02T00:00:00.000Z" },
|
|
);
|
|
|
|
expect(profile.totalTurns).toBe(2);
|
|
expect(profile.pastIntents).toEqual([
|
|
{ count: 2, kind: "photo-question" },
|
|
]);
|
|
expect(profile.completedOnboardingAt).toBe("2026-05-02T00:00:00.000Z");
|
|
expect(profile.preferredStyle).toBe("concise");
|
|
});
|
|
});
|