117 lines
4.6 KiB
TypeScript
117 lines
4.6 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { correlateEventWithServiceSession, normalizeServiceId, resolveUniqueSessionUserId, serviceEventExternalIds } from "../src/events/record-grow-event.js";
|
|
import { canonicalGrowEventType, normalizeGrowEvent } from "../src/events/normalize.js";
|
|
import type { GrowEventEnvelope } from "../src/events/envelope.js";
|
|
|
|
{
|
|
const normalized = normalizeGrowEvent({
|
|
source: "interview-service",
|
|
type: "interview.session.completed",
|
|
correlation: { session_id: "session-1", taskId: "nested-task", keep: "yes" },
|
|
task_id: "top-level-task",
|
|
payload: { curator_task_id: "payload-task" },
|
|
});
|
|
assert.equal(normalized.correlation?.session_id, "session-1");
|
|
assert.equal(normalized.correlation?.keep, "yes");
|
|
assert.equal(normalized.correlation?.task_id, "nested-task");
|
|
assert.equal(normalized.correlation?.taskId, undefined);
|
|
assert.equal(normalized.correlation?.curatorTaskId, undefined);
|
|
}
|
|
{
|
|
const standalone = normalizeGrowEvent({ source: "social-service", type: "social.account.connected" });
|
|
assert.equal(standalone.correlation, undefined);
|
|
assert.equal(standalone.type, "brand.account.connected");
|
|
}
|
|
{
|
|
assert.equal(resolveUniqueSessionUserId([{ userId: "user-a" }]), "user-a");
|
|
assert.equal(resolveUniqueSessionUserId([{ userId: "user-a" }, { userId: "user-a" }]), "user-a");
|
|
assert.equal(resolveUniqueSessionUserId([{ userId: "user-a" }, { userId: "user-b" }]), undefined);
|
|
}
|
|
|
|
const event: GrowEventEnvelope = {
|
|
id: "event-1",
|
|
userId: "user-a",
|
|
source: "interview-service",
|
|
type: "interview.session.completed",
|
|
category: "service",
|
|
occurredAt: "2026-07-14T00:00:00.000Z",
|
|
correlation: { sessionId: "session-1" },
|
|
payload: { status: "completed" },
|
|
};
|
|
|
|
const link = {
|
|
userId: "user-a",
|
|
missionInstanceId: "sprint-1",
|
|
missionId: "curator-sprint",
|
|
stageId: "day-1:interview",
|
|
metadata: { task_id: "curator-task-1" },
|
|
};
|
|
|
|
{
|
|
const correlated = correlateEventWithServiceSession(event, "user-a", link);
|
|
assert.deepEqual(correlated.correlation, {
|
|
sessionId: "session-1",
|
|
task_id: "curator-task-1",
|
|
missionInstanceId: "sprint-1",
|
|
missionId: "curator-sprint",
|
|
stageId: "day-1:interview",
|
|
});
|
|
assert.deepEqual(correlated.mission, {
|
|
instanceId: "sprint-1",
|
|
missionId: "curator-sprint",
|
|
stageId: "day-1:interview",
|
|
});
|
|
}
|
|
|
|
{
|
|
const foreign = correlateEventWithServiceSession(event, "user-a", { ...link, userId: "user-b" });
|
|
assert.strictEqual(foreign, event, "a service session owned by another user must never enrich the event");
|
|
}
|
|
{
|
|
const sessionless = correlateEventWithServiceSession(event, "user-a", undefined);
|
|
assert.strictEqual(sessionless, event, "without an exact session link, historical events must not enrich correlation");
|
|
}
|
|
|
|
{
|
|
const unresolved = correlateEventWithServiceSession(event, undefined, link);
|
|
assert.strictEqual(unresolved, event, "an unresolved event must never inherit session context");
|
|
}
|
|
|
|
{
|
|
const explicit = correlateEventWithServiceSession({
|
|
...event,
|
|
mission: { missionId: "explicit-mission", stageId: "explicit-stage" },
|
|
correlation: { sessionId: "session-1", task_id: "explicit-task" },
|
|
}, "user-a", link);
|
|
assert.equal(explicit.correlation?.task_id, "explicit-task");
|
|
assert.deepEqual(explicit.mission, { missionId: "explicit-mission", stageId: "explicit-stage" });
|
|
}
|
|
|
|
{
|
|
assert.deepEqual(serviceEventExternalIds({
|
|
...event,
|
|
correlation: { courseId: "course-1", lesson_id: "lesson-1", externalId: "course-1" },
|
|
subject: { serviceId: "courses-service", externalId: "lesson-1" },
|
|
payload: { resume_id: "resume-1", assessmentId: "assessment-1" },
|
|
}), ["course-1", "lesson-1", "resume-1", "assessment-1"]);
|
|
}
|
|
|
|
{
|
|
assert.equal(normalizeServiceId("social-branding-service"), "brand");
|
|
assert.equal(normalizeServiceId("courses-service"), "course");
|
|
assert.equal(normalizeServiceId("cover-letter-service"), "cover_letter");
|
|
assert.equal(normalizeServiceId("matchmaking-v2"), "matchmaking");
|
|
}
|
|
|
|
{
|
|
assert.equal(canonicalGrowEventType("roleplay.session.completed"), "roleplay.scenario.completed");
|
|
assert.equal(canonicalGrowEventType("roleplay.review.completed"), "roleplay.feedback.generated");
|
|
assert.equal(canonicalGrowEventType("course.video.completed"), "course.completed");
|
|
assert.equal(canonicalGrowEventType("course.video.started"), "course.started");
|
|
assert.equal(canonicalGrowEventType("course.lesson.started"), "course.started");
|
|
assert.equal(canonicalGrowEventType("social.profile.synced"), "brand.profile.updated");
|
|
assert.equal(canonicalGrowEventType("resume.analysis.completed"), "resume.analysis.completed");
|
|
}
|
|
|
|
console.log("service session correlation tests passed");
|