Files
Jyotisha/frontend/tests/chart-library-session.test.ts
T
jesse-ux cc1a89802e
Independent Staging Quality Gate / validate (push) Failing after 6m27s
Independent Staging Quality Gate / publish (push) Skipped
fix(web): 无用户行钉顶、闲置不上留白,并修会话列表 tsc 两错
2026-09-17 22:25:53 +08:00

113 lines
4.4 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import {
chartLibraryFromCloudOthers,
chartLibraryOnCloudFailure,
chartLibrarySessionBranch,
} from "../src/lib/chart-library-session.ts";
import { sessionSidebarSubtitle, sessionSidebarTitle, toSidebarSessionRow } from "../src/lib/session-sidebar-row.ts";
import { emptyProfile, type ChartLibraryRecord, type ChatSession } from "../src/lib/home-types.ts";
type RecordShape = { id: string; role: "self" | "other" };
function upsertSelf(library: RecordShape[], profile: unknown): RecordShape[] {
const others = library.filter((record) => record.role !== "self");
if (!profile) return others;
return [{ id: "self", role: "self" }, ...others];
}
test("clearing the account empties the chart library session", () => {
assert.equal(chartLibrarySessionBranch(null, "acct-1"), "clear");
assert.equal(chartLibrarySessionBranch("", "acct-1"), "clear");
});
test("switching accounts hydrates local then cloud instead of writing the previous library", () => {
assert.equal(chartLibrarySessionBranch("acct-2", "acct-1"), "hydrate-then-persist");
});
test("a later profile change on the same account still upserts self", () => {
assert.equal(chartLibrarySessionBranch("acct-1", "acct-1"), "persist-self");
const current: RecordShape[] = [{ id: "other-1", role: "other" }];
assert.deepEqual(upsertSelf(current, { name: "self" }), [
{ id: "self", role: "self" },
{ id: "other-1", role: "other" },
]);
});
test("a successful cloud read keeps only non-self records before upserting self", () => {
const cloud: RecordShape[] = [
{ id: "stale-self", role: "self" },
{ id: "other-1", role: "other" },
];
assert.deepEqual(chartLibraryFromCloudOthers(cloud, { name: "self" }, upsertSelf), [
{ id: "self", role: "self" },
{ id: "other-1", role: "other" },
]);
});
test("a failed cloud read keeps only the profile-derived self chart", () => {
// Former value: keepLocalChartLibraryOnCloudFailure(local) === local.
// Local other-charts are no longer a fallback when the cloud read fails.
const local: RecordShape[] = [{ id: "self", role: "self" }, { id: "other-1", role: "other" }];
assert.notDeepEqual(chartLibraryOnCloudFailure({ name: "self" }, upsertSelf), local);
assert.deepEqual(chartLibraryOnCloudFailure({ name: "self" }, upsertSelf), [
{ id: "self", role: "self" },
]);
});
test("sidebar titles drop the chart-name prefix and only subtitle others", () => {
// 原值:本人盘副标题 null;他人盘只有资料名
// 新值:一律「M月D日 HH:MM」,他人盘再加「 · 盘主」
// 原因:任务书 T4 副标题规则
const createdAt = Date.parse("2026-09-14T04:14:00.000Z");
const selfRecord: ChartLibraryRecord = {
id: "self",
role: "self",
relationship: "self",
updatedAt: 1,
profile: { ...emptyProfile, name: "本人" },
};
const library: ChartLibraryRecord[] = [selfRecord];
const base: ChatSession = {
id: "11111111-1111-4111-8111-111111111111",
title: "半年内换工作时机",
theme: "career",
modelId: "m",
messages: [],
createdAt,
updatedAt: createdAt,
sessionType: "consultation",
rectificationCaseId: null,
chartProfileId: "self",
chartProfileName: "本人",
chartProfileRole: "self",
pinned: false,
archivedAt: null,
messagesHydrated: true,
};
// 原值: sessionSidebarTitle(base, library)
// 新值: sessionSidebarTitle(base)
// 原因: sessionSidebarTitle 只吃 session;第二参是 e4e73f56 的 TS 错,挡住 tsc / next build
assert.equal(sessionSidebarTitle(base), "半年内换工作时机");
assert.equal(sessionSidebarSubtitle(base, library), "9月14日 12:14");
const other = { ...base, chartProfileId: "other-1", chartProfileName: "对方", chartProfileRole: "other" as const };
assert.equal(sessionSidebarSubtitle(other, library), "9月14日 12:14 · 资料已删除 · 对方");
const liveOtherLibrary: ChartLibraryRecord[] = [{
...selfRecord,
id: "other-1",
role: "other",
relationship: "partner",
profile: { ...emptyProfile, name: "对方" },
}];
assert.equal(sessionSidebarSubtitle(other, liveOtherLibrary), "9月14日 12:14 · 对方");
assert.deepEqual(toSidebarSessionRow(other, liveOtherLibrary), {
id: other.id,
title: "半年内换工作时机",
subtitle: "9月14日 12:14 · 对方",
pinned: false,
archived: false,
updatedAt: createdAt,
});
});