/* ===================================================================== uc-session-continuity-guard.spec.ts — 같은 내담자 활성 회기 보호 목적: 종료된 회기가 아닌 같은 내담자의 진행 중 회기가 있을 때 새 회기를 중복 생성하지 않고, 서버가 돌려준 기존 회기로 학습자를 이동시킨다. 모든 응답은 route fixture이며 실제 AI 엔진/DB를 쓰지 않는다. ===================================================================== */ import { expect, test, type Page } from "@playwright/test"; const ACTIVE_SESSION_ID = "77777777-7777-4777-8777-777777777777"; function jsonRoute(body: unknown, status = 200) { return { status, contentType: "application/json", body: JSON.stringify(body), }; } async function installFixture(page: Page) { let startAttempts = 0; await page.route("**/api/**", (route) => route.fulfill(jsonRoute({ detail: "not part of this focused fixture" }, 404)), ); await page.route("**/api/auth/me", (route) => route.fulfill( jsonRoute({ user_id: "00000000-0000-0000-0000-00000uccg301", email: "continuity.guard@hs.ac.kr", display_name: "연속성 검증 학습자", role: "learner", admin_access: false, super_admin: false, account_status: "approved", approval_required: false, cohort_ids: [], consent_at: Math.floor(Date.now() / 1000), onboarding_completed_at: Math.floor(Date.now() / 1000), nickname: "연속성 검증 학습자", self_introduction: "", avatar_url: "", }), ), ); await page.route("**/api/personas", (route) => route.fulfill( jsonRoute([ { code: "P1", display_name: "민서(청소년 우울)", difficulty: "hard", theory_target: ["humanistic"], demographics: { age_band: "10대" }, presenting_summary: "자퇴와 무기력감을 둘러싼 상담 연습", voice_preset: "soft-young-fem", source: "database", degraded: false, }, ]), ), ); await page.route("**/api/users/me/prepost-measures**", (route) => route.fulfill( jsonRoute({ pilot_id: "phase3-pilot-draft", instrument_version: "test", measures: [], complete_pre_count: 0, complete_post_count: 0, updated_at: null, }), ), ); await page.route("**/api/sessions", async (route) => { if (route.request().method() !== "POST") { await route.fallback(); return; } startAttempts += 1; await route.fulfill( jsonRoute( { detail: { code: "active_session_exists", session_id: ACTIVE_SESSION_ID, }, }, 409, ), ); }); await page.route(`**/api/sessions/${ACTIVE_SESSION_ID}`, (route) => route.fulfill( jsonRoute({ session_id: ACTIVE_SESSION_ID, case_id: "uc-continuity-case-001", persona_code: "P1", persona_name: "민서", session_no: 1, status: "active", stage: "라포", theory_mode: "humanistic", effective_openness: 0.21, started_at: new Date().toISOString(), ended_at: null, review_ready: false, turns: [], goal_stages: ["라포"], progress: null, duration_limit_seconds: 3600, warning_before_end_seconds: 600, }), ), ); return { startAttempts: () => startAttempts }; } test.describe("uc session continuity guard", () => { test("같은 내담자의 활성 회기 충돌은 기존 회기로 이어간다", async ({ page }) => { const fixture = await installFixture(page); await page.goto("/learn/session/P1"); await expect(page.locator(".sx-page--prestart")).toBeVisible(); await page.getByRole("button", { name: "회기 시작" }).click(); await expect(page).toHaveURL( new RegExp(`/learn/session/${ACTIVE_SESSION_ID}$`), ); await expect(page.getByText("민서").first()).toBeVisible(); expect(fixture.startAttempts()).toBe(1); }); });