81 lines
2.4 KiB
TypeScript
81 lines
2.4 KiB
TypeScript
import { describe, it, expect } from "bun:test";
|
|
import { parseTasks, validateTaskFormat, TaskFormatError } from "../../src/core/task-parser.ts";
|
|
|
|
describe("parseTasks", () => {
|
|
it("解析标准 checkbox 格式", () => {
|
|
const content = `- [ ] 任务一
|
|
- [ ] 任务二
|
|
- [x] 任务三`;
|
|
const tasks = parseTasks(content);
|
|
expect(tasks).toHaveLength(3);
|
|
expect(tasks[0]).toEqual({ checked: false, text: "任务一" });
|
|
expect(tasks[1]).toEqual({ checked: false, text: "任务二" });
|
|
expect(tasks[2]).toEqual({ checked: true, text: "任务三" });
|
|
});
|
|
|
|
it("跳过非 checkbox 行", () => {
|
|
const content = `# 标题
|
|
一些描述
|
|
- [ ] 任务一
|
|
普通文本
|
|
- [x] 任务二`;
|
|
const tasks = parseTasks(content);
|
|
expect(tasks).toHaveLength(2);
|
|
});
|
|
|
|
it("空内容返回空数组", () => {
|
|
expect(parseTasks("")).toEqual([]);
|
|
expect(parseTasks("# 只有标题")).toEqual([]);
|
|
});
|
|
|
|
it("支持大写 X 标记", () => {
|
|
const content = `- [X] 大写标记`;
|
|
const tasks = parseTasks(content);
|
|
expect(tasks).toHaveLength(1);
|
|
expect(tasks[0].checked).toBe(true);
|
|
});
|
|
|
|
it("支持缩进的 checkbox", () => {
|
|
const content = ` - [ ] 缩进任务`;
|
|
const tasks = parseTasks(content);
|
|
expect(tasks).toHaveLength(1);
|
|
expect(tasks[0].text).toBe("缩进任务");
|
|
});
|
|
|
|
it("计算已完成和总数", () => {
|
|
const content = `- [x] 已完成
|
|
- [ ] 未完成
|
|
- [x] 另一个完成`;
|
|
const tasks = parseTasks(content);
|
|
const completed = tasks.filter((t) => t.checked).length;
|
|
expect(completed).toBe(2);
|
|
expect(tasks.length).toBe(3);
|
|
});
|
|
});
|
|
|
|
describe("validateTaskFormat", () => {
|
|
it("合法 task 内容通过校验", () => {
|
|
expect(() => validateTaskFormat("- [x] 已完成\n- [ ] 未完成")).not.toThrow();
|
|
});
|
|
|
|
it("无 checkbox 项时抛错", () => {
|
|
expect(() => validateTaskFormat("# 标题\n一些描述")).toThrow(TaskFormatError);
|
|
});
|
|
|
|
it("空内容抛错", () => {
|
|
expect(() => validateTaskFormat("")).toThrow(TaskFormatError);
|
|
});
|
|
|
|
it("checkbox 文本为空时抛错", () => {
|
|
expect(() => validateTaskFormat("- [ ] \n- [x] 有内容")).toThrow(TaskFormatError);
|
|
});
|
|
|
|
it("checkbox 文本仅空格时抛错", () => {
|
|
expect(() => validateTaskFormat("- [ ] ")).toThrow(TaskFormatError);
|
|
});
|
|
|
|
it("有 checkbox 且文本非空时通过", () => {
|
|
expect(() => validateTaskFormat("- [ ] 实现功能A")).not.toThrow();
|
|
});
|
|
});
|