After BUG-2100 merged the supplementary timing chapters, one chapter holds up to ~505k characters and a question read only its first 20k. A chapter over 12k characters is now read as its lead, a list of its ### parts by id (chapter_<n>_<k>), then parts from the top; a part can be read by its id, and a passage selected in a long chapter is read from the part that holds it. Cut material ends with a mark the model is told about. The TOC timing pattern gains 年运 and Timing so 「年运附表」 and "Additional timing calculations" match their other-language twins. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8
127 lines
7.5 KiB
TypeScript
127 lines
7.5 KiB
TypeScript
/**
|
||
* BUG-2103: after BUG-2100 merged the supplementary timing chapters, one data chapter holds up to
|
||
* ~505,000 characters (zh) and one `###` part up to ~145,000. A question read the chapter's first
|
||
* 20,000 and never reached the systems listed later (Kalachakra, Niryaana Shoola…). A long chapter
|
||
* is now read by its `###` parts. Synthetic report text only.
|
||
*/
|
||
import assert from "node:assert/strict";
|
||
import test from "node:test";
|
||
import { readFileSync } from "node:fs";
|
||
|
||
import { personalChapterParts, personalDataChapters, personalSubsectionAskId, personalSubsectionIndex } from "../src/lib/personal-question-ids.ts";
|
||
import {
|
||
fitPersonalMaterial, personalAskSectionIds, personalAskSectionTitle, personalQuoteSubsection, personalReadableSection,
|
||
personalSectionMaterial, PERSONAL_CHAPTER_WHOLE_LIMIT, PERSONAL_MATERIAL_CUT_MARK,
|
||
} from "../src/lib/personal-question-material.ts";
|
||
import { isTimingChapter } from "../src/lib/report-toc-groups.ts";
|
||
import { PERSONAL_CHAT_INSTRUCTIONS } from "../src/mastra/personal-report-chat.ts";
|
||
|
||
const rows = (label: string, count: number) =>
|
||
Array.from({ length: count }, (_, at) => `| 次小运 | ${label} | 第 ${at + 1} 段 | 2026-01-01 | 2026-02-01 |`).join("\n");
|
||
|
||
const LONG = [
|
||
"## 辅助大运次小运数据",
|
||
"",
|
||
"研究口径:依赖所选方法。",
|
||
"",
|
||
"### Sthira",
|
||
"",
|
||
"次小运从子运星座起顺行。",
|
||
"",
|
||
"| 层级 | 主运 | 次序 | 起始 | 结束 |",
|
||
"| --- | --- | --- | --- | --- |",
|
||
rows("Sthira", 400),
|
||
"",
|
||
"### Kalachakra",
|
||
"",
|
||
"Kalachakra 的子运按星宿四分之一划分。",
|
||
"",
|
||
"| 层级 | 主运 | 次序 | 起始 | 结束 |",
|
||
"| --- | --- | --- | --- | --- |",
|
||
rows("Kalachakra", 400),
|
||
].join("\n");
|
||
|
||
const MARKDOWN = ["# 报告", "", "## 本命盘原始位置", "", "- 土星在第 1 宫。", "", LONG].join("\n");
|
||
const SOURCE = { reading: null, markdown: MARKDOWN, factTables: [], facts: {} };
|
||
|
||
test("a chapter splits into its lead and its ### parts; a part has its own id", () => {
|
||
const chapters = personalDataChapters(MARKDOWN);
|
||
assert.equal(chapters.length, 2);
|
||
const { head, parts } = personalChapterParts(chapters[1].markdown);
|
||
assert.match(head, /研究口径/u);
|
||
assert.deepEqual(parts.map((part) => part.title), ["Sthira", "Kalachakra"]);
|
||
assert.equal(personalSubsectionAskId(1, 2), "chapter_1_2");
|
||
assert.deepEqual(personalSubsectionIndex("chapter_1_2"), { chapter: 1, part: 2 });
|
||
assert.equal(personalSubsectionIndex("chapter_1"), null);
|
||
// Parts are read, not asked from: the page's entries and counts stay per chapter.
|
||
assert.deepEqual(personalAskSectionIds(SOURCE), ["chapter_0", "chapter_1"]);
|
||
assert.equal(personalAskSectionTitle(SOURCE, "chapter_1_2"), "辅助大运次小运数据 / Kalachakra");
|
||
});
|
||
|
||
test("a short chapter is read whole; a long one gives its lead, its parts by id, then parts from the top", () => {
|
||
assert.ok(LONG.length > PERSONAL_CHAPTER_WHOLE_LIMIT);
|
||
assert.equal(personalSectionMaterial(SOURCE, "chapter_0").text, personalDataChapters(MARKDOWN)[0].markdown);
|
||
const whole = personalSectionMaterial(SOURCE, "chapter_1");
|
||
assert.match(whole.text, /研究口径/u);
|
||
assert.match(whole.text, /- chapter_1_1:Sthira(约 \d+ 千字)\n- chapter_1_2:Kalachakra(约 \d+ 千字)/u);
|
||
assert.ok(whole.text.indexOf("chapter_1_2:Kalachakra") < whole.text.indexOf("### Sthira"));
|
||
// Cut to a turn's budget, the list of parts survives and the cut is marked.
|
||
const fitted = fitPersonalMaterial(whole, 6_000);
|
||
assert.ok(JSON.stringify(fitted).length <= 6_000);
|
||
assert.equal(fitted.truncated, true);
|
||
assert.match(fitted.text, /chapter_1_2:Kalachakra/u);
|
||
assert.ok(fitted.text.endsWith(PERSONAL_MATERIAL_CUT_MARK));
|
||
assert.doesNotMatch(fitted.text, /\| Kalachakra \|/u);
|
||
});
|
||
|
||
test("a part reads the chapter's lead and that part only", () => {
|
||
const part = personalSectionMaterial(SOURCE, "chapter_1_2");
|
||
assert.equal(part.title, "辅助大运次小运数据 / Kalachakra");
|
||
assert.match(part.text, /研究口径/u);
|
||
assert.match(part.text, /\| Kalachakra \| 第 400 段 \|/u);
|
||
assert.doesNotMatch(part.text, /\| Sthira \|/u);
|
||
assert.equal(personalSectionMaterial(SOURCE, "chapter_1_9").missing, true);
|
||
assert.equal(personalSectionMaterial(SOURCE, "chapter_7_1").missing, true);
|
||
});
|
||
|
||
test("only parts of this report are readable", () => {
|
||
for (const id of ["chapter_0", "chapter_1", "chapter_1_1", "chapter_1_2"]) assert.equal(personalReadableSection(SOURCE, id), true, id);
|
||
for (const id of ["chapter_2", "chapter_1_0", "chapter_1_3", "chapter_0_1", "career", "table_x"]) assert.equal(personalReadableSection(SOURCE, id), false, id);
|
||
});
|
||
|
||
test("a passage selected in a long chapter is read from the part that holds it", () => {
|
||
assert.equal(personalQuoteSubsection(SOURCE, "chapter_1", "Kalachakra 的子运按星宿四分之一划分。"), "chapter_1_2");
|
||
assert.equal(personalQuoteSubsection(SOURCE, "chapter_1", "次小运从子运星座起顺行。"), "chapter_1_1");
|
||
// The lead, a short chapter, or text the chapter does not have: the chapter itself is read.
|
||
assert.equal(personalQuoteSubsection(SOURCE, "chapter_1", "研究口径:依赖所选方法。"), null);
|
||
assert.equal(personalQuoteSubsection(SOURCE, "chapter_0", "土星在第 1 宫。"), null);
|
||
assert.equal(personalQuoteSubsection(SOURCE, "chapter_1", "不在这一章的话"), null);
|
||
});
|
||
|
||
test("the handler reads the passage's part, keeps the chapter on the record, and checks every read", () => {
|
||
const handler = readFileSync(new URL("../src/lib/personal-report-consult.ts", import.meta.url), "utf8");
|
||
assert.match(handler, /const readFrom = sectionId && quote \? \(personalQuoteSubsection\(source, sectionId, quote\) \?\? sectionId\) : sectionId;/u);
|
||
assert.match(handler, /anchor = sectionId \? \(quote \? \{ sectionId, quote \} : \{ sectionId \}\)/u);
|
||
assert.match(handler, /section: readFrom \? \{ id: readFrom, title: personalAskSectionTitle\(source, readFrom\) \} : null/u);
|
||
assert.match(handler, /personalReadableSection\(source, request\.section\) \? request\.section : \(readFrom \?\? request\.section\)/u);
|
||
});
|
||
|
||
test("the model is told how to read a long chapter and what the cut mark means", () => {
|
||
assert.match(PERSONAL_CHAT_INSTRUCTIONS, /很长的数据章节读整章时只给开头和各段的编号(例如 chapter_61_6);要看哪一段,就用那一段的编号再读一次。/u);
|
||
assert.match(PERSONAL_CHAT_INSTRUCTIONS, /材料末尾写着「材料到这里截断」时,后面的内容没有读到:没读到的不写,也不猜。/u);
|
||
assert.ok(PERSONAL_MATERIAL_CUT_MARK.includes("材料到这里截断"));
|
||
const chat = readFileSync(new URL("../src/mastra/personal-report-chat.ts", import.meta.url), "utf8");
|
||
assert.match(chat, /或很长的数据章节里的一段,例如 chapter_61_6/u);
|
||
});
|
||
|
||
test("the timing group takes 年运 and Timing titles in both languages", () => {
|
||
// 原值: 「年运附表」 and "Additional timing calculations" fell in 本命数据; 新值: 推运, like their
|
||
// other-language twins (Annual Appendix, 补充推运计算数据); 原因: BUG-2103.
|
||
for (const title of ["年运附表", "Annual Appendix", "补充推运计算数据", "Additional timing calculations", "辅助大运次小运数据", "Auxiliary dasha subperiods"]) {
|
||
assert.equal(isTimingChapter(title), true, title);
|
||
}
|
||
for (const title of ["本命 Tithi Ashtottari", "Natal Tithi Ashtottari", "Varnada", "Buddhi Gati", "计算口径", "Calculation Conventions", "Rectification and Prediction Contract"]) {
|
||
assert.equal(isTimingChapter(title), false, title);
|
||
}
|
||
});
|