Files
life-echo/app-expo/tests/features/memoir/mappers.test.ts

135 lines
3.7 KiB
TypeScript
Raw Normal View History

import { toChapterViewModel } from '@/features/memoir/mappers';
import type { Chapter, ImageAsset } from '@/features/memoir/types';
function makeImage(overrides: Partial<ImageAsset> = {}): ImageAsset {
return {
placeholder: 'img_1',
description: 'A scene',
index: 0,
status: 'completed',
prompt: null,
url: 'https://example.com/img.jpg',
provider: null,
style: null,
size: null,
error: null,
retryable: null,
created_at: null,
updated_at: null,
...overrides,
};
}
function makeChapter(overrides: Partial<Chapter> = {}): Chapter {
return {
id: 'ch-1',
title: '童年',
content: '一些内容',
order_index: 0,
status: 'ready',
category: 'childhood',
images: [],
cover_image: null,
sections: [{ content: '段落1', image: null }],
updated_at: '2026-01-01T00:00:00Z',
is_new: false,
source_segments: [],
...overrides,
};
}
describe('toChapterViewModel', () => {
test('maps basic chapter fields', () => {
const vm = toChapterViewModel(makeChapter());
expect(vm.id).toBe('ch-1');
expect(vm.title).toBe('童年');
expect(vm.category).toBe('childhood');
expect(vm.orderIndex).toBe(0);
expect(vm.isEmpty).toBe(false);
expect(vm.isNew).toBe(false);
expect(vm.sections).toHaveLength(1);
重构回忆录为 story-first / markdown-first 架构并整合图片意图与前端 UI 修复 本次 squash merge 将 codex-story-first-image-intent 的整体改动合入 development,核心内容包括: 1. 后端数据与迁移:新增 stories、story_versions、story_image_intents、chapter_cover_intents、assets 等模型与 Alembic 迁移,建立 story-first、markdown-first、asset-first 的主数据链路。 2. 生成与任务链:引入 StoryBuilderOrchestrator、ChapterComposerOrchestrator、story_image_tasks、chapter_cover_tasks,图片生成从正文占位符改为结构化 intent -> asset -> markdown 回填。 3. 并发与一致性:为 story/chapter intent 增加 claim_token、claimed_at、attempt_count,采用数据库原子 claim 为主、Redis 锁为辅,避免重复生成、锁误删和 processing 卡死。 4. Memoir 读写路径:章节 canonical_markdown 成为正文真源,列表/详情接口补齐 markdown、cover_asset、word_count 等字段,PDF 与 asset 解析链路同步升级。 5. Memory / Retrieval:扩展 transcript ingest、chunking、evidence 检索与 story 聚合基础设施,为后续 story-first RAG 与多 agent 编排提供底座。 6. App 端体验:章节页继续走 MarkdownRenderer 阅读链,同时吸收 fix3-19 的跨平台 UI glitch 修复;更新对话页、首页、文案资源与章节列表映射逻辑。 7. 测试与文档:补充 asset resolver、story image task、章节封面派发、markdown 映射等回归测试,并加入图片占位符退役设计文档。
2026-03-20 10:30:07 +08:00
expect(vm.wordCount).toBe('段落1'.length);
});
test('uses word_count from API when sections empty', () => {
const vm = toChapterViewModel(
makeChapter({
sections: [],
word_count: 1200,
canonical_markdown: 'x'.repeat(1200),
}),
);
expect(vm.wordCount).toBe(1200);
});
test('cover_asset mirrors cover_image for list payload', () => {
const cover = makeImage({ url: 'https://example.com/from-asset.jpg' });
const vm = toChapterViewModel(
makeChapter({ cover_image: null, cover_asset: cover, images: [] }),
);
expect(vm.coverImageUrl).toBe('https://example.com/from-asset.jpg');
expect(vm.hasImages).toBe(true);
});
test('detects empty chapters', () => {
const vm = toChapterViewModel(
makeChapter({ status: 'empty', content: '' }),
);
expect(vm.isEmpty).toBe(true);
});
重构回忆录为 story-first / markdown-first 架构并整合图片意图与前端 UI 修复 本次 squash merge 将 codex-story-first-image-intent 的整体改动合入 development,核心内容包括: 1. 后端数据与迁移:新增 stories、story_versions、story_image_intents、chapter_cover_intents、assets 等模型与 Alembic 迁移,建立 story-first、markdown-first、asset-first 的主数据链路。 2. 生成与任务链:引入 StoryBuilderOrchestrator、ChapterComposerOrchestrator、story_image_tasks、chapter_cover_tasks,图片生成从正文占位符改为结构化 intent -> asset -> markdown 回填。 3. 并发与一致性:为 story/chapter intent 增加 claim_token、claimed_at、attempt_count,采用数据库原子 claim 为主、Redis 锁为辅,避免重复生成、锁误删和 processing 卡死。 4. Memoir 读写路径:章节 canonical_markdown 成为正文真源,列表/详情接口补齐 markdown、cover_asset、word_count 等字段,PDF 与 asset 解析链路同步升级。 5. Memory / Retrieval:扩展 transcript ingest、chunking、evidence 检索与 story 聚合基础设施,为后续 story-first RAG 与多 agent 编排提供底座。 6. App 端体验:章节页继续走 MarkdownRenderer 阅读链,同时吸收 fix3-19 的跨平台 UI glitch 修复;更新对话页、首页、文案资源与章节列表映射逻辑。 7. 测试与文档:补充 asset resolver、story image task、章节封面派发、markdown 映射等回归测试,并加入图片占位符退役设计文档。
2026-03-20 10:30:07 +08:00
test('uses canonical_markdown for isEmpty when present', () => {
const vm = toChapterViewModel(
makeChapter({
status: 'ready',
content: '',
sections: [],
canonical_markdown: '# 童年\n\n一段回忆。',
}),
);
expect(vm.isEmpty).toBe(false);
});
test('derives image status counts correctly', () => {
const images = [
makeImage({ status: 'completed' }),
makeImage({ status: 'pending' }),
makeImage({ status: 'processing' }),
makeImage({ status: 'failed' }),
];
const vm = toChapterViewModel(makeChapter({ images }));
expect(vm.hasImages).toBe(true);
expect(vm.allImagesReady).toBe(false);
expect(vm.pendingImageCount).toBe(2);
expect(vm.failedImageCount).toBe(1);
});
test('allImagesReady is true when all completed', () => {
const images = [
makeImage({ status: 'completed' }),
makeImage({ status: 'completed' }),
];
const vm = toChapterViewModel(makeChapter({ images }));
expect(vm.allImagesReady).toBe(true);
expect(vm.pendingImageCount).toBe(0);
expect(vm.failedImageCount).toBe(0);
});
test('extracts cover image URL', () => {
const cover = makeImage({ url: 'https://example.com/cover.jpg' });
const vm = toChapterViewModel(makeChapter({ cover_image: cover }));
expect(vm.coverImageUrl).toBe('https://example.com/cover.jpg');
});
test('cover image URL is null when no cover', () => {
const vm = toChapterViewModel(makeChapter({ cover_image: null }));
expect(vm.coverImageUrl).toBeNull();
});
});