pi-ate-workflow 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.en.md +102 -0
  2. package/README.md +104 -0
  3. package/agent-loader.ts +88 -0
  4. package/index.ts +18 -0
  5. package/output.ts +89 -0
  6. package/package.json +50 -0
  7. package/research-analysis.ts +64 -0
  8. package/research-catalog.ts +33 -0
  9. package/research-scan.ts +65 -0
  10. package/research-state.ts +46 -0
  11. package/research-types.ts +53 -0
  12. package/research-utils.ts +107 -0
  13. package/research.ts +52 -0
  14. package/state.ts +121 -0
  15. package/subagent-spawn-once.ts +164 -0
  16. package/subagent-spawn-visible.ts +194 -0
  17. package/subagent-utils.ts +38 -0
  18. package/subagent.ts +93 -0
  19. package/tests/agent-loader.test.ts +183 -0
  20. package/tests/agent-schema-extra.test.ts +107 -0
  21. package/tests/agent-schema.test.ts +116 -0
  22. package/tests/output.test.ts +107 -0
  23. package/tests/research-analysis.test.ts +155 -0
  24. package/tests/research-extra.test.ts +175 -0
  25. package/tests/research.test.ts +418 -0
  26. package/tests/smoke.subagent.test.ts +127 -0
  27. package/tests/state-manager.test.ts +245 -0
  28. package/tests/state.test.ts +298 -0
  29. package/tests/subagent-api.test.ts +158 -0
  30. package/tests/subagent-spawn-once.test.ts +154 -0
  31. package/tests/subagent-spawn-visible.test.ts +139 -0
  32. package/tests/subagent-utils.test.ts +124 -0
  33. package/tests/test-helpers.ts +40 -0
  34. package/tests/ui-updater.test.ts +129 -0
  35. package/tests/utils-find-session.test.ts +114 -0
  36. package/tests/utils-status-summary.test.ts +158 -0
  37. package/tests/utils.test.ts +48 -0
  38. package/tests/widget.test.ts +114 -0
  39. package/tests/workflow-execution.test.ts +165 -0
  40. package/tests/workflow-registration.test.ts +155 -0
  41. package/tests/workflow.test.ts +207 -0
  42. package/types.ts +24 -0
  43. package/utils.ts +74 -0
  44. package/vitest.config.ts +21 -0
  45. package/widget.ts +104 -0
  46. package/workflow.ts +113 -0
@@ -0,0 +1,155 @@
1
+ /**
2
+ * research-analysis.ts 测试
3
+ *
4
+ * 覆盖:loadAnalysis / saveAnalysis / defaultAnalysis 的全路径
5
+ */
6
+
7
+ import { describe, it, expect, vi, beforeEach } from "vitest";
8
+
9
+ const mockFs = vi.hoisted(() => ({
10
+ existsSync: vi.fn<[string], boolean>(),
11
+ readFileSync: vi.fn<[string, string], string>(),
12
+ writeFileSync: vi.fn<[string, string, string], void>(),
13
+ mkdirSync: vi.fn<[string, object], void>(),
14
+ }));
15
+
16
+ vi.mock("node:fs", () => mockFs);
17
+
18
+ import { loadAnalysis, saveAnalysis, defaultAnalysis } from "../research-analysis";
19
+
20
+ beforeEach(() => {
21
+ vi.clearAllMocks();
22
+ });
23
+
24
+ describe("defaultAnalysis", () => {
25
+ it("should return ArticleAnalysis with all false", () => {
26
+ const result = defaultAnalysis();
27
+ expect(result).toEqual({
28
+ direction_done: false,
29
+ score_evaluated: false,
30
+ interpretation_done: false,
31
+ });
32
+ });
33
+ });
34
+
35
+ describe("loadAnalysis", () => {
36
+ it("should return empty object when file does not exist", () => {
37
+ mockFs.existsSync.mockReturnValue(false);
38
+ const result = loadAnalysis("/test/project");
39
+ expect(result).toEqual({});
40
+ expect(mockFs.existsSync).toHaveBeenCalledWith(
41
+ expect.stringContaining("analysis.json"),
42
+ );
43
+ });
44
+
45
+ it("should parse and return valid JSON from file", () => {
46
+ const saved = {
47
+ "abc123": {
48
+ direction_done: true,
49
+ score_evaluated: false,
50
+ interpretation_done: true,
51
+ },
52
+ "def456": {
53
+ direction_done: false,
54
+ score_evaluated: true,
55
+ interpretation_done: false,
56
+ },
57
+ };
58
+ mockFs.existsSync.mockReturnValue(true);
59
+ mockFs.readFileSync.mockReturnValue(JSON.stringify(saved));
60
+ const result = loadAnalysis("/test/project");
61
+ expect(result).toEqual(saved);
62
+ });
63
+
64
+ it("should return empty object on JSON parse error", () => {
65
+ mockFs.existsSync.mockReturnValue(true);
66
+ mockFs.readFileSync.mockReturnValue("{invalid json}");
67
+ const result = loadAnalysis("/test/project");
68
+ expect(result).toEqual({});
69
+ });
70
+
71
+ it("should return empty object when readFileSync throws", () => {
72
+ mockFs.existsSync.mockReturnValue(true);
73
+ mockFs.readFileSync.mockImplementation(() => {
74
+ throw new Error("read error");
75
+ });
76
+ const result = loadAnalysis("/test/project");
77
+ expect(result).toEqual({});
78
+ });
79
+ });
80
+
81
+ describe("saveAnalysis", () => {
82
+ it("should create directory if missing then write file", () => {
83
+ mockFs.existsSync.mockReturnValue(false);
84
+
85
+ const state = { "art001": defaultAnalysis() };
86
+ saveAnalysis("/test/project", state);
87
+
88
+ expect(mockFs.existsSync).toHaveBeenCalledWith(
89
+ expect.stringContaining("docs/research"),
90
+ );
91
+ expect(mockFs.mkdirSync).toHaveBeenCalledWith(
92
+ expect.stringContaining("docs/research"),
93
+ { recursive: true },
94
+ );
95
+ expect(mockFs.writeFileSync).toHaveBeenCalledWith(
96
+ expect.stringContaining("analysis.json"),
97
+ expect.stringContaining("art001"),
98
+ "utf-8",
99
+ );
100
+ });
101
+
102
+ it("should write file when directory already exists", () => {
103
+ mockFs.existsSync.mockReturnValue(true);
104
+
105
+ const state = { "art002": defaultAnalysis() };
106
+ saveAnalysis("/test/project", state);
107
+
108
+ expect(mockFs.mkdirSync).not.toHaveBeenCalled();
109
+ expect(mockFs.writeFileSync).toHaveBeenCalledWith(
110
+ expect.stringContaining("analysis.json"),
111
+ expect.any(String),
112
+ "utf-8",
113
+ );
114
+ });
115
+
116
+ it("should write prettified JSON with 2-space indent", () => {
117
+ mockFs.existsSync.mockReturnValue(true);
118
+
119
+ const state = { "key1": defaultAnalysis() };
120
+ saveAnalysis("/test/project", state);
121
+
122
+ const callArgs = mockFs.writeFileSync.mock.calls[0];
123
+ const writtenJson = callArgs[1] as string;
124
+ const parsed = JSON.parse(writtenJson);
125
+ expect(parsed).toEqual(state);
126
+ // 验证漂亮打印:键前应有空格
127
+ expect(writtenJson).toContain("\n ");
128
+ });
129
+ });
130
+
131
+ describe("loadAnalysis -> saveAnalysis round-trip", () => {
132
+ it("should round-trip state through JSON", () => {
133
+ // loadAnalysis: file exists
134
+ mockFs.existsSync.mockReturnValue(true);
135
+ const state1 = {
136
+ "art001": { direction_done: true, score_evaluated: false, interpretation_done: false },
137
+ "art002": { direction_done: true, score_evaluated: true, interpretation_done: true },
138
+ };
139
+ mockFs.readFileSync.mockReturnValue(JSON.stringify(state1));
140
+
141
+ const loaded = loadAnalysis("/test/project");
142
+ expect(loaded).toEqual(state1);
143
+
144
+ // saveAnalysis: write back
145
+ loaded.art003 = defaultAnalysis();
146
+ saveAnalysis("/test/project", loaded);
147
+
148
+ const savedArg = mockFs.writeFileSync.mock.calls[0][1] as string;
149
+ const parsed = JSON.parse(savedArg);
150
+ expect(parsed).toHaveProperty("art001");
151
+ expect(parsed).toHaveProperty("art002");
152
+ expect(parsed).toHaveProperty("art003");
153
+ expect(parsed.art003).toEqual(defaultAnalysis());
154
+ });
155
+ });
@@ -0,0 +1,175 @@
1
+ /**
2
+ * Phase 1 额外测试:fixture 格式兼容性、缺失字段、损坏内容
3
+ */
4
+
5
+ import { describe, it, expect, beforeEach, afterEach } from "vitest";
6
+ import * as fs from "node:fs";
7
+ import * as path from "node:path";
8
+ import * as os from "node:os";
9
+
10
+ import {
11
+ parseFrontmatter,
12
+ scanNewSources,
13
+ type Article,
14
+ } from "../research";
15
+
16
+ describe("parseFrontmatter — 补充测试", () => {
17
+ it("should parse all fields from a complete frontmatter fixture", () => {
18
+ const content = `---
19
+ url: https://arxiv.org/abs/2301.12345
20
+ title: Deep Learning for Factor Investing
21
+ score: 8
22
+ source_type: academic
23
+ found_at: "2026-05-09T12:00:00"
24
+ ---
25
+ This paper explores deep learning approaches...`;
26
+
27
+ const fm = parseFrontmatter(content);
28
+ expect(fm.url).toBe("https://arxiv.org/abs/2301.12345");
29
+ expect(fm.title).toBe("Deep Learning for Factor Investing");
30
+ expect(fm.score).toBe(8);
31
+ expect(fm.source_type).toBe("academic");
32
+ expect(fm.found_at).toBe("2026-05-09T12:00:00");
33
+ });
34
+
35
+ it("should handle missing score field gracefully (return undefined)", () => {
36
+ const content = `---
37
+ url: https://example.com/article
38
+ title: Test Article
39
+ source_type: blog
40
+ found_at: "2026-05-09T12:00:00"
41
+ ---
42
+ Content without score`;
43
+
44
+ const fm = parseFrontmatter(content);
45
+ expect(fm.url).toBe("https://example.com/article");
46
+ expect(fm.title).toBe("Test Article");
47
+ expect(fm.source_type).toBe("blog");
48
+ expect(fm.score).toBeUndefined();
49
+ expect(fm.found_at).toBe("2026-05-09T12:00:00");
50
+ });
51
+
52
+ it("should handle missing title and found_at fields", () => {
53
+ const content = `---
54
+ url: https://example.com/minimal
55
+ score: 6
56
+ ---
57
+ Just URL and score`;
58
+
59
+ const fm = parseFrontmatter(content);
60
+ expect(fm.url).toBe("https://example.com/minimal");
61
+ expect(fm.score).toBe(6);
62
+ expect(fm.title).toBeUndefined();
63
+ expect(fm.found_at).toBeUndefined();
64
+ });
65
+
66
+ it("should handle corrupted frontmatter (illegal YAML-like content)", () => {
67
+ // 首页有 --- 分隔符,但内容是非法 YAML(二进制、冒号过多等)
68
+ const content = `---
69
+ url: https://example.com
70
+ : : bad : yaml : : :
71
+ value: :colon: explosion
72
+ binary: \\x00\\x01\\x02
73
+ ---
74
+ Some content`;
75
+
76
+ // 不应崩溃,应返回可解析的字段
77
+ const fm = parseFrontmatter(content);
78
+ // 至少 url 应该被解析
79
+ expect(fm.url).toBe("https://example.com");
80
+ });
81
+
82
+ it("should handle frontmatter with unusual whitespace", () => {
83
+ const content = `---
84
+ url: https://example.com/spaced
85
+ title: "Title with spaces"
86
+ score: 7
87
+ source_type: academic
88
+ found_at: "2026-05-09T12:00:00"
89
+ ---
90
+ Content`;
91
+
92
+ const fm = parseFrontmatter(content);
93
+ expect(fm.url).toBe("https://example.com/spaced");
94
+ expect(fm.title).toBe("Title with spaces");
95
+ expect(fm.score).toBe(7);
96
+ expect(fm.source_type).toBe("academic");
97
+ });
98
+ });
99
+
100
+ describe("scanNewSources — 缺失 score 行为", () => {
101
+ let tmpDir: string;
102
+ let topicDir: string;
103
+
104
+ beforeEach(() => {
105
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "research-extra-"));
106
+ // scanNewSources 使用 getResearchDir(cwd) = cwd/docs/research
107
+ topicDir = path.join(tmpDir, "project");
108
+ fs.mkdirSync(path.join(topicDir, "docs", "research", "sources", "arxiv"), { recursive: true });
109
+ });
110
+
111
+ afterEach(() => {
112
+ fs.rmSync(tmpDir, { recursive: true, force: true });
113
+ });
114
+
115
+ it("should assign default score 5 when frontmatter has no score", () => {
116
+ const content = `---
117
+ url: https://example.com/no-score
118
+ title: No Score Article
119
+ source_type: blog
120
+ ---
121
+ Content without score`;
122
+
123
+ fs.writeFileSync(
124
+ path.join(topicDir, "docs", "research", "sources", "arxiv", "2026-05-09T1200_article.md"),
125
+ content,
126
+ );
127
+
128
+ const articles = scanNewSources(topicDir, []);
129
+ expect(articles).toHaveLength(1);
130
+ expect(articles[0].score).toBe(5); // default
131
+ expect(articles[0].url).toBe("https://example.com/no-score");
132
+ });
133
+
134
+ it("should handle corrupted article file without crashing", () => {
135
+ // 没有 frontmatter 分隔符
136
+ fs.writeFileSync(
137
+ path.join(topicDir, "docs", "research", "sources", "arxiv", "corrupted.md"),
138
+ "This is just plain text without any frontmatter",
139
+ );
140
+
141
+ // 不应崩溃,应返回空数组
142
+ const articles = scanNewSources(topicDir, []);
143
+ expect(articles).toEqual([]);
144
+ });
145
+
146
+ it("should handle binary garbage file without crashing", () => {
147
+ const garbage = Buffer.alloc(100, 0xff);
148
+ fs.writeFileSync(
149
+ path.join(topicDir, "docs", "research", "sources", "arxiv", "garbage.bin"),
150
+ garbage,
151
+ );
152
+
153
+ // 不匹配 *_article.md 模式,所以应被忽略
154
+ const articles = scanNewSources(topicDir, []);
155
+ expect(articles).toEqual([]);
156
+ });
157
+
158
+ it("should skip article with empty URL even when other fields exist", () => {
159
+ const content = `---
160
+ url: ""
161
+ title: Empty URL Article
162
+ score: 7
163
+ source_type: academic
164
+ ---
165
+ Content with empty URL`;
166
+
167
+ fs.writeFileSync(
168
+ path.join(topicDir, "docs", "research", "sources", "arxiv", "2026-05-09T1200_article.md"),
169
+ content,
170
+ );
171
+
172
+ const articles = scanNewSources(topicDir, []);
173
+ expect(articles).toHaveLength(0);
174
+ });
175
+ });