fix: address ui review follow-ups

This commit is contained in:
Yaojia Wang
2026-06-29 12:06:04 +02:00
parent 90a66437d7
commit dd80b6815d
7 changed files with 244 additions and 3 deletions

View File

@@ -0,0 +1,45 @@
import { describe, expect, it } from "vitest";
import { reviewChapterHref, reviewChapterOptions } from "./chapterNav";
import type { ChapterEntry } from "@/lib/workbench/chapter";
describe("reviewChapterHref", () => {
it("builds the review route for a chapter number", () => {
expect(reviewChapterHref("proj-1", 3)).toBe(
"/projects/proj-1/review?chapter=3",
);
});
it("always targets the review page (not write)", () => {
expect(reviewChapterHref("p", 1)).toContain("/review?chapter=1");
});
});
describe("reviewChapterOptions", () => {
const chapters: ChapterEntry[] = [
{ no: 1, title: "开篇" },
{ no: 2 },
{ no: 3, title: "转折" },
];
it("merges outline chapters with the current chapter, sorted ascending", () => {
const opts = reviewChapterOptions(chapters, 2);
expect(opts.map((o) => o.no)).toEqual([1, 2, 3]);
});
it("includes the current chapter even when it is outside the outline", () => {
const opts = reviewChapterOptions(chapters, 9);
expect(opts.map((o) => o.no)).toEqual([1, 2, 3, 9]);
});
it("labels each option with chapter number and optional title", () => {
const opts = reviewChapterOptions(chapters, 1);
expect(opts[0]).toEqual({ no: 1, label: "第 1 章 · 开篇" });
expect(opts[1]).toEqual({ no: 2, label: "第 2 章" });
});
it("returns just the current chapter when there is no outline", () => {
const opts = reviewChapterOptions([], 4);
expect(opts).toEqual([{ no: 4, label: "第 4 章" }]);
});
});

View File

@@ -0,0 +1,23 @@
import type { ChapterEntry } from "@/lib/workbench/chapter";
import { buildChapterEntries } from "@/lib/workbench/chapter";
export interface ReviewChapterOption {
no: number;
label: string;
}
export function reviewChapterHref(projectId: string, chapterNo: number): string {
return `/projects/${projectId}/review?chapter=${chapterNo}`;
}
export function reviewChapterOptions(
chapters: readonly ChapterEntry[],
currentChapterNo: number,
): ReviewChapterOption[] {
return buildChapterEntries(chapters, currentChapterNo).map((chapter) => ({
no: chapter.no,
label: chapter.title
? `${chapter.no} 章 · ${chapter.title}`
: `${chapter.no}`,
}));
}