This repository has been archived on 2026-09-13. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
BPOS-TMP-REPO/apps/api/src/app/__tests__/file-compose.test.ts
T
xcsandClaude Opus 5 0aa2fc11b0 资料文件真产出来了;E04/I01 补到验收线,顺手填了几个「墙拦住却不给路」的坑
**资料文件全链**(用户:审核资料这一步要真产出 word/excel/md/html,前端能渲、点资料名能开)
接手时这一步一个文件都产不出来 —— S3 剧本白纸黑字写着「file_ref 本 MVP 阶段可留空」。
文件是台账那一行的投影,不另存一份:
· 声明 `file:{titleColumn,bodyColumn,formatColumn}`,它同时是授权面(没声明就点不开)
· 现造真字节 —— docx/xlsx 是自己打的 OOXML 包(zip-write:store+CRC32,零依赖)
· 读回来渲 —— 解的就是造出来那份字节,渲得出来才证明文件是真的
· 件上 `data-file-of` → 工作区二级界面(Word 纸面/Excel 网格/渲好的 md/沙箱 html)+ 下载原件
放行落地前把每一份冻进 `sys.task_asset_file`(迁移 0029):归档件点开的必须是签字那一刻
那一份,而资料落在主数据上、试跑单不落正式表 —— 不存这一份,收工后一个都点不开。
工作成果里「产出的资料」自成一摞。Markdown 富文本渲染顺带销账(仍零依赖)。

**Harness**
· 画面按「这一拍办完之后」在办的那一步挑 —— 从前拿拍首的局面决定拍尾的事:
  收掉 S4、发出 S5 的卡,而屏上还摆着 S4 的只读件,人无处可填(墙要配路第四次现形)
· 验收「重做」真能退回重做,且打回后台账没动就不许再提交(软硬两段,不锁死单子)
· 立监听的授权面是清单声明;发卡的授权面是调人预算,不是计划项
· 读工具同参重调有兜底;空转写身后还有行动就不废串;finish 的 doneWhen 按写完后的草稿算

**界面**
· 翻件与翻页各归各位(从前二选一,多卡多页时页翻不动);没得翻写「—」不写 0/0
· 工作成果详情能翻份;表宽超出能横滑(E04/I01 抄版式时把 overflow-x 抄丢了)
· 查看面灰底滚过一屏变白 —— 两条独立的路都治了(沙箱内不同色 / 弹性子项不长个)
· 测试台「取不到」不再当成「还活着」

**活动**
E04 关系覆盖、I01 品牌内容各自补到验收线:判据全读基座数、判据指向的东西摆上卡面、
样例满载且名字 ≤14 字;提交前要裁的判据搬进说明书(模型会压拍,压过之后只有说明书还在)。

验收:e2e i01 4/4、e04 3/3 全部 released,资料在办中渲得出、放行后存了档点得开;
门禁 typecheck/lint/depcruise/1628 测全绿。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EEJX8Z7CHejvwmDQyPQuBX
2026-08-24 10:26:45 +08:00

211 lines
8.3 KiB
TypeScript

/**
* 资料文件现造 + 读回来 —— 两头合起来自证:**造出来的是真文件**。
*
* 断言的重点不是「函数返回了字节」,而是:
* · docx / xlsx 是合法 ZIP,里面那几份 XML 一份不缺(Word / Excel 认的就是它们);
* · 读回来的内容与写进去的一模一样 —— 渲得出来,才谈得上「前端能渲染」;
* · 模型写的字全程转义 —— 正文里带 `<script>` 也只是几个字,不是标签。
*/
import { describe, expect, it } from 'vitest';
import {
columnName,
composeFile,
inlineHtml,
markdownBody,
markdownSheetRows,
parseMarkdown,
safeFileName,
} from '../file-compose.ts';
import { docxBlocks, toFileDoc, xlsxRows } from '../file-doc.ts';
import { readZip } from '../upload-extract.ts';
const MD = [
'# 车规级 MCU 供货能力说明',
'',
'本文面向 **Itelma** 采购与供应链团队,说明我方在车规 MCU 上的供货保障。',
'',
'## 一、产能与库存',
'',
'- 常备安全库存:12 周',
'- 二供已导入,切换周期 4 周',
'',
'## 二、价格与交期',
'',
'| 型号 | 交期(周) | 年用量 |',
'| --- | --- | --- |',
'| NE555P | 8 | 30000 |',
'| DAG2400 | 12 | 25000 |',
'',
'---',
'',
'结论:可承诺 `98%` 的按期交付率。',
].join('\n');
describe('parseMarkdown —— 认得出这几样,认不出的不吞字', () => {
it('标题 / 段落 / 列表 / 表格 / 分隔线各归各位', () => {
const blocks = parseMarkdown(MD);
expect(blocks.map((b) => b.t)).toEqual(['h1', 'p', 'h2', 'ul', 'h2', 'table', 'hr', 'p']);
});
it('表格的分隔行只说对齐,不算一行数据', () => {
const table = parseMarkdown(MD).find((b) => b.t === 'table');
expect(table).toBeDefined();
if (table?.t !== 'table') throw new Error('不是表格块');
expect(table.rows).toEqual([
['型号', '交期(周)', '年用量'],
['NE555P', '8', '30000'],
['DAG2400', '12', '25000'],
]);
});
it('认不出的行当段落原样留着 —— 宁可朴素也不吞内容', () => {
const blocks = parseMarkdown('> 引用还不认\n\n普通段落');
expect(blocks).toEqual([
{ t: 'p', text: '> 引用还不认' },
{ t: 'p', text: '普通段落' },
]);
});
});
describe('转义 —— 模型写的字进不了标签位', () => {
it('正文里的 <script> 只是几个字', () => {
const html = markdownBody('这里有 <script>alert(1)</script> 一段');
expect(html).toContain('&lt;script&gt;');
expect(html).not.toContain('<script>');
});
it('先转义再上行内标记 —— 顺序反了就是把字当标签执行', () => {
expect(inlineHtml('**粗** 与 `码` 与 <b>假标签</b>')).toBe(
'<strong>粗</strong> 与 <code>码</code> 与 &lt;b&gt;假标签&lt;/b&gt;',
);
});
});
describe('md —— 原文即文件', () => {
it('正文没有标题行就补一条:文件得自报家门', () => {
const f = composeFile({ title: '供货能力说明', markdown: '正文直接开始', format: 'md' });
expect(new TextDecoder().decode(f.bytes)).toBe('# 供货能力说明\n\n正文直接开始');
expect(f.fileName).toBe('供货能力说明.md');
});
it('正文自带标题就不再补', () => {
const f = composeFile({ title: 'T', markdown: MD, format: 'md' });
expect(new TextDecoder().decode(f.bytes).startsWith('# 车规级 MCU')).toBe(true);
});
});
describe('html —— 整份文件,离线打开也是这个样子', () => {
const f = composeFile({ title: '供货能力说明', markdown: MD, format: 'html' });
const text = new TextDecoder().decode(f.bytes);
it('是一份完整文档:doctype + 自带样式 + 纸面容器', () => {
expect(text.startsWith('<!doctype html>')).toBe(true);
expect(text).toContain('<style>');
expect(text).toContain('class="doc-paper"');
});
it('表格是真表格,不是一段文字', () => {
expect(text).toContain('<th>型号</th>');
expect(text).toContain('<td>NE555P</td>');
});
});
describe('docx —— 真 OOXML 包', () => {
const f = composeFile({ title: '供货能力说明', markdown: MD, format: 'docx' });
const zip = readZip(f.bytes);
it('该有的几份 XML 一份不缺(少一份 Word 就报文件损坏)', () => {
expect([...zip.keys()].sort()).toEqual(
['[Content_Types].xml', '_rels/.rels', 'word/_rels/document.xml.rels', 'word/document.xml', 'word/styles.xml'].sort(),
);
});
it('MIME 与扩展名是 Word 那一套', () => {
expect(f.fileName).toBe('供货能力说明.docx');
expect(f.mime).toContain('wordprocessingml.document');
});
it('读回来:标题、正文、列表、表格都在,层级没丢', () => {
const xml = new TextDecoder().decode(zip.get('word/document.xml') ?? new Uint8Array());
const blocks = docxBlocks(xml);
expect(blocks[0]).toEqual({ t: 'h1', text: '供货能力说明' });
expect(blocks.some((b) => b.t === 'h2' && b.text === '一、产能与库存')).toBe(true);
const list = blocks.find((b) => b.t === 'ul');
expect(list?.t === 'ul' ? list.items : []).toEqual(['常备安全库存:12 周', '二供已导入,切换周期 4 周']);
// 表格按行摊成 p:列用「 | 」连
expect(blocks.some((b) => b.t === 'p' && b.text === 'NE555P | 8 | 30000')).toBe(true);
});
it('行内标记不带进 Word —— 留着 ** 反而像乱码', () => {
const xml = new TextDecoder().decode(zip.get('word/document.xml') ?? new Uint8Array());
expect(xml).toContain('Itelma');
expect(xml).not.toContain('**Itelma**');
});
});
describe('xlsx —— 真 OOXML 包,表格原样成行', () => {
const f = composeFile({ title: '交期对照表', markdown: MD, format: 'xlsx' });
it('该有的几份 XML 一份不缺', () => {
expect([...readZip(f.bytes).keys()].sort()).toEqual(
['[Content_Types].xml', '_rels/.rels', 'xl/_rels/workbook.xml.rels', 'xl/workbook.xml', 'xl/worksheets/sheet1.xml'].sort(),
);
});
it('读回来:表格三行在,首行是表头', () => {
const sheet = xlsxRows(f.bytes);
expect(sheet?.sheet).toBe('交期对照表');
const rows = sheet?.rows ?? [];
expect(rows[0]).toEqual(['交期对照表']);
expect(rows).toContainEqual(['型号', '交期(周)', '年用量']);
expect(rows).toContainEqual(['NE555P', '8', '30000']);
});
it('正文不丢:列表与段落各占一行', () => {
const rows = markdownSheetRows('T', MD);
expect(rows.some((r) => r[0] === '• 常备安全库存:12 周')).toBe(true);
expect(rows.some((r) => (r[0] ?? '').startsWith('结论:可承诺'))).toBe(true);
});
it('列号:0→A、25→Z、26→AA', () => {
expect([columnName(0), columnName(25), columnName(26)]).toEqual(['A', 'Z', 'AA']);
});
});
describe('toFileDoc —— 前端拿到的就是这份文件读回来的样子', () => {
const one = (format: 'md' | 'html' | 'docx' | 'xlsx') => {
const f = composeFile({ title: '供货能力说明', markdown: MD, format });
return toFileDoc({ name: f.fileName, title: '供货能力说明', format, bytes: f.bytes });
};
it('四种格式各出各的查看形态', () => {
expect(one('md')?.kind).toBe('markdown');
expect(one('html')?.kind).toBe('html');
expect(one('docx')?.kind).toBe('word');
expect(one('xlsx')?.kind).toBe('excel');
});
it('markdown 同时给原文与渲好的那一份(下载拿原文,屏上摆渲染版)', () => {
const doc = one('md');
if (doc?.kind !== 'markdown') throw new Error('不是 markdown');
expect(doc.md).toContain('| NE555P | 8 | 30000 |');
expect(doc.html).toContain('<td>NE555P</td>');
});
it('**读不出来就返回 undefined** —— 不摆一个空壳假装渲染过', () => {
const junk = new TextEncoder().encode('这不是 ZIP');
expect(toFileDoc({ name: 'x.docx', title: 'x', format: 'docx', bytes: junk })).toBeUndefined();
expect(toFileDoc({ name: 'x.xlsx', title: 'x', format: 'xlsx', bytes: junk })).toBeUndefined();
});
});
describe('文件名', () => {
it('路径分隔符这类字符换成下划线 —— 别让标题决定文件落在哪', () => {
expect(safeFileName('a/b:c*d?"e<f>g|h', 'md')).toBe('a_b_c_d__e_f_g_h.md');
});
it('标题整个是空白就退一个兜底名,不出「.docx」这种没名字的文件', () => {
expect(safeFileName(' ', 'docx')).toBe('资料.docx');
});
});