@allbluecn/web-app 0.10.2 → 0.11.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 (169) hide show
  1. package/package.json +26 -11
  2. package/public/favicon.ico +0 -0
  3. package/public/logo/allblue-logo-app-icon.svg +7 -0
  4. package/public/logo/allblue-logo-dark-square.svg +4 -0
  5. package/public/logo/allblue-logo-dark.svg +4 -0
  6. package/public/logo/allblue-logo-light-square.svg +4 -0
  7. package/public/logo/allblue-logo-light.svg +4 -0
  8. package/src/__tests__/bench/baselines/format-timestamp.json +44 -0
  9. package/src/__tests__/bench/core-hot-path.bench.ts +71 -0
  10. package/src/__tests__/integration/db-crud.integration.test.ts +79 -0
  11. package/src/__tests__/rejects-tothrow-regression.test.ts +31 -0
  12. package/src/__tests__/security/ai/prompt-injection.test.ts +105 -0
  13. package/src/__tests__/security/ssrf-url-whitelist.test.ts +54 -0
  14. package/src/__tests__/security/xss-sanitize.test.ts +99 -0
  15. package/src/__tests__/setup.ts +9 -0
  16. package/src/__tests__/visual/__screenshots__/theme-surface.vrt.test.tsx/theme-surface-dark-baseline-1-chromium-darwin.png +0 -0
  17. package/src/__tests__/visual/__screenshots__/theme-surface.vrt.test.tsx/theme-surface-light-baseline-1-chromium-darwin.png +0 -0
  18. package/src/__tests__/visual/__screenshots__/user-avatar.vrt.test.tsx/user-avatar-initial-fallback-baseline-1-chromium-darwin.png +0 -0
  19. package/src/__tests__/visual/theme-surface.vrt.test.tsx +54 -0
  20. package/src/__tests__/visual/user-avatar.vrt.test.tsx +32 -0
  21. package/src/components/error/global-error-boundary.tsx +2 -2
  22. package/src/components/knowledge/general-groups.ts +15 -0
  23. package/src/components/knowledge/kb-filter-popover.tsx +143 -0
  24. package/src/components/knowledge-public/legal-compliance-editor.tsx +4 -2
  25. package/src/components/layout/sidebar-03/__tests__/nav-workspace.test.tsx +9 -6
  26. package/src/components/layout/sidebar-03/nav-knowledge.tsx +19 -17
  27. package/src/components/layout/sidebar-03/nav-secondary.tsx +57 -47
  28. package/src/components/layout/sidebar-03/nav-workspace.tsx +20 -22
  29. package/src/components/mdx.tsx +9 -0
  30. package/src/components/shared-kit/avatars/notion-avatar.tsx +2 -1
  31. package/src/components/shared-kit/icons/__tests__/lucide-curated-icons.test.ts +2 -2
  32. package/src/components/shared-kit/icons/__tests__/lucide-icon-picker.test.tsx +3 -2
  33. package/src/components/shared-kit/icons/lucide-icon-picker/curated-icons.ts +5 -10
  34. package/src/components/shared-kit/icons/lucide-icon-picker/lucide-icon-by-name.tsx +3 -3
  35. package/src/components/shared-kit/theme/theme-surface.tsx +52 -0
  36. package/src/components/story/relations/general-groups.ts +1 -15
  37. package/src/components/story/relations/knowledge-select-dialog.tsx +13 -87
  38. package/src/components/story/story-list-view.tsx +7 -1
  39. package/src/components/tiptap/node/table-node/ui/table-move-row-column-button/use-table-move-row-column.ts +0 -2
  40. package/src/components/workspace/__tests__/activity/workspace-activity-card.test.tsx +36 -0
  41. package/src/components/workspace/__tests__/contribution/workspace-contribution-card.test.tsx +17 -0
  42. package/src/components/workspace/__tests__/dialogs/create-workspace-dialog.test.tsx +63 -0
  43. package/src/components/workspace/__tests__/dialogs/delete-workspace-options.test.tsx +89 -0
  44. package/src/components/workspace/__tests__/dialogs/edit-workspace-dialog.test.tsx +68 -0
  45. package/src/components/workspace/__tests__/header/workspace-header.test.tsx +40 -0
  46. package/src/components/workspace/__tests__/kb/workspace-kb-card.test.tsx +154 -0
  47. package/src/components/workspace/__tests__/members/workspace-member-item.test.tsx +24 -0
  48. package/src/components/workspace/__tests__/members/workspace-members-manager.test.tsx +51 -0
  49. package/src/components/workspace/__tests__/projects/workspace-project-list.test.tsx +46 -0
  50. package/src/components/workspace/{create-workspace-dialog.tsx → dialogs/create-workspace-dialog.tsx} +1 -1
  51. package/src/components/workspace/{edit-workspace-dialog.tsx → dialogs/edit-workspace-dialog.tsx} +1 -1
  52. package/src/components/workspace/kb/workspace-kb-card.tsx +594 -0
  53. package/src/components/workspace/kb/workspace-kb-manage-dialog.tsx +174 -0
  54. package/src/components/workspace/kb/workspace-quick-doc-dialog.tsx +431 -0
  55. package/src/components/workspace/{workspace-members-manager.tsx → members/workspace-members-manager.tsx} +78 -40
  56. package/src/lib/__tests__/msw-sample.test.ts +68 -0
  57. package/src/lib/__tests__/pgvector.test.ts +2 -2
  58. package/src/lib/changelog/sdk-versions.ts +21 -21
  59. package/src/lib/collections/knowledge/knowledge-bases-collection.ts +3 -1
  60. package/src/lib/collections/reference/workspaces-collection.ts +3 -1
  61. package/src/lib/fetch-json.ts +23 -0
  62. package/src/lib/fumadocs-i18n.ts +7 -0
  63. package/src/{components/workspace/workspace-kb-card.tsx → lib/i18n/resolver.ts} +6 -12
  64. package/src/lib/layout.shared.tsx +40 -0
  65. package/src/lib/sanitize.ts +14 -1
  66. package/src/lib/session.ts +9 -1
  67. package/src/lib/source.ts +15 -0
  68. package/src/lib/vector-store/__tests__/factory.test.ts +1 -1
  69. package/src/lib/vector-store/__tests__/sqlite-vec-store.test.ts +2 -2
  70. package/src/plugins/core/tags/__tests__/tags.test.ts +10 -10
  71. package/src/routeTree.gen.ts +65 -0
  72. package/src/routes/$lang/docs/$/route.lazy.tsx +62 -0
  73. package/src/routes/$lang/docs/$/route.tsx +52 -0
  74. package/src/routes/-tests/docs-redirect.test.ts +45 -0
  75. package/src/routes/__root.tsx +13 -7
  76. package/src/routes/__tests__/-edition-routing.test.tsx +4 -4
  77. package/src/routes/__tests__/route-tree-structure.test.ts +65 -0
  78. package/src/routes/_nav/knowledge/compliance/index.lazy.tsx +5 -2
  79. package/src/routes/_nav/knowledge/index.lazy.tsx +1 -1
  80. package/src/routes/_nav/knowledge/laws/index.lazy.tsx +5 -2
  81. package/src/routes/_nav/route.tsx +21 -0
  82. package/src/routes/_nav/workspaces/$workspaceId/route.lazy.tsx +13 -11
  83. package/src/routes/_nav/workspaces/$workspaceId/route.tsx +4 -2
  84. package/src/routes/_nav/workspaces/$workspaceId/settings/route.lazy.tsx +1 -1
  85. package/src/routes/_nav/workspaces/index.lazy.tsx +3 -3
  86. package/src/routes/api/search.ts +13 -0
  87. package/src/routes/docs.tsx +9 -0
  88. package/src/server/__tests__/agent-crud.test.ts +4 -4
  89. package/src/server/__tests__/chat-conversations.test.ts +1 -3
  90. package/src/server/__tests__/fetch-guard.test.ts +99 -0
  91. package/src/server/__tests__/permissions.test.ts +1 -3
  92. package/src/server/__tests__/resources.team-seats.test.ts +2 -0
  93. package/src/server/__tests__/skill-crud.test.ts +3 -3
  94. package/src/server/ai-gateway/__tests__/ai-output-guard.test.ts +95 -0
  95. package/src/server/ai-gateway/__tests__/kb-context-guard.test.ts +136 -0
  96. package/src/server/ai-gateway/__tests__/knowledge-search-fence.test.ts +240 -0
  97. package/src/server/ai-gateway/__tests__/llm-fence.test.ts +125 -0
  98. package/src/server/ai-gateway/__tests__/run-store.test.ts +52 -0
  99. package/src/server/ai-gateway/ai-output-guard.ts +77 -0
  100. package/src/server/ai-gateway/index.ts +5 -0
  101. package/src/server/ai-gateway/kb-context-guard.ts +46 -0
  102. package/src/server/ai-gateway/llm-fence.ts +82 -0
  103. package/src/server/ai-gateway/persistence/run-store.ts +19 -0
  104. package/src/server/ai-gateway/story-prompt.ts +4 -2
  105. package/src/server/ai-gateway/tools/definitions.ts +12 -6
  106. package/src/server/ai-gateway/tools/server.ts +97 -54
  107. package/src/server/builtin-project.ts +21 -0
  108. package/src/server/email/sender.ts +8 -2
  109. package/src/server/fetch-guard.ts +170 -0
  110. package/src/server/middlewares/security-headers.ts +113 -0
  111. package/src/server/serverFns/__tests__/billing.test.ts +263 -0
  112. package/src/server/serverFns/__tests__/link-preview.test.ts +239 -0
  113. package/src/server/serverFns/__tests__/project-kb.test.ts +3 -3
  114. package/src/server/serverFns/__tests__/project-team-assign.test.ts +1 -1
  115. package/src/server/serverFns/__tests__/story-comments.test.ts +265 -0
  116. package/src/server/serverFns/__tests__/story-crud-archive-detail.test.ts +266 -0
  117. package/src/server/serverFns/__tests__/story-crud-labels-users.test.ts +227 -0
  118. package/src/server/serverFns/__tests__/story-crud.test.ts +526 -0
  119. package/src/server/serverFns/__tests__/story-links.test.ts +262 -0
  120. package/src/server/serverFns/__tests__/story-list-query.test.ts +259 -0
  121. package/src/server/serverFns/__tests__/story-reactions.test.ts +230 -0
  122. package/src/server/serverFns/__tests__/story-tree.test.ts +317 -0
  123. package/src/server/serverFns/__tests__/story-types.test.ts +294 -0
  124. package/src/server/serverFns/__tests__/team-accept.test.ts +6 -6
  125. package/src/server/serverFns/__tests__/team-invitation.test.ts +3 -3
  126. package/src/server/serverFns/__tests__/team-join-request.test.ts +8 -8
  127. package/src/server/serverFns/__tests__/update-seats.test.ts +4 -4
  128. package/src/server/serverFns/__tests__/workspace-kb-idor.test.ts +160 -0
  129. package/src/server/serverFns/__tests__/workspace-kb-utils.test.ts +54 -0
  130. package/src/server/serverFns/__tests__/workspace-kb.test.ts +348 -0
  131. package/src/server/serverFns/__tests__/workspace.test.ts +28 -8
  132. package/src/server/serverFns/iteration/__tests__/iteration-crud.test.ts +3 -5
  133. package/src/server/serverFns/iteration/__tests__/iteration-stories.test.ts +1 -1
  134. package/src/server/serverFns/iteration/__tests__/iteration-transfer.test.ts +2 -2
  135. package/src/server/serverFns/kb-doc-search.ts +111 -0
  136. package/src/server/serverFns/knowledge-public/compliance/create.ts +2 -1
  137. package/src/server/serverFns/knowledge-public/legal/create.ts +2 -1
  138. package/src/server/serverFns/story/__tests__/bulk.test.ts +1 -1
  139. package/src/server/serverFns/story/__tests__/label-suggest-fn.test.ts +2 -6
  140. package/src/server/serverFns/story/__tests__/story-assoc.test.ts +2 -4
  141. package/src/server/serverFns/story/__tests__/story-dependencies.test.ts +2 -2
  142. package/src/server/serverFns/story/__tests__/story-groups.test.ts +2 -2
  143. package/src/server/serverFns/story/kb-general-consts.ts +39 -0
  144. package/src/server/serverFns/story/kb-general.ts +30 -17
  145. package/src/server/serverFns/story/story-assoc.ts +13 -103
  146. package/src/server/serverFns/story/story-crud.ts +2 -5
  147. package/src/server/serverFns/story/story-groups.ts +4 -13
  148. package/src/server/serverFns/team.ts +1 -1
  149. package/src/server/serverFns/workspace-kb-utils.ts +138 -0
  150. package/src/server/serverFns/workspace-kb.ts +403 -0
  151. package/src/server/serverFns/workspace.ts +4 -1
  152. package/src/shared/ai-cli/__tests__/adapter.test.ts +67 -0
  153. package/src/shared/ai-cli/__tests__/cli-detector.test.ts +99 -0
  154. package/src/shared/ai-cli/adapter.ts +2 -2
  155. package/src/shared/ai-cli/cli-detector.ts +18 -9
  156. package/src/start.ts +2 -1
  157. package/src/styles/globals.css +116 -0
  158. package/src/types/jest-dom-vitest.d.ts +8 -7
  159. package/vite.shared.ts +18 -1
  160. package/public/file.svg +0 -1
  161. package/public/globe.svg +0 -1
  162. package/public/window.svg +0 -1
  163. package/src/lib/__tests__/cli-detector.test.ts +0 -80
  164. /package/src/components/workspace/{workspace-activity-card.tsx → activity/workspace-activity-card.tsx} +0 -0
  165. /package/src/components/workspace/{workspace-contribution-card.tsx → contribution/workspace-contribution-card.tsx} +0 -0
  166. /package/src/components/workspace/{delete-workspace-options.tsx → dialogs/delete-workspace-options.tsx} +0 -0
  167. /package/src/components/workspace/{workspace-header.tsx → header/workspace-header.tsx} +0 -0
  168. /package/src/components/workspace/{workspace-member-item.tsx → members/workspace-member-item.tsx} +0 -0
  169. /package/src/components/workspace/{workspace-project-list.tsx → projects/workspace-project-list.tsx} +0 -0
@@ -0,0 +1,262 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // AllBlue - 理想之海
3
+ // Copyright (C) 2026 AllBlue Contributors
4
+ //
5
+ // This program is free software: you can redistribute it and/or modify
6
+ // it under the terms of the GNU Affero General Public License as published by
7
+ // the Free Software Foundation, either version 3 of the License, or
8
+ // (at your option) any later version.
9
+ //
10
+ // This program is distributed in the hope that it will be useful,
11
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ // GNU Affero General Public License for more details.
14
+ //
15
+ // You should have received a copy of the GNU Affero General Public License
16
+ // along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+
18
+
19
+ // story-links 域覆盖补全:addLinkFn / updateLinkFn / removeLinkFn / getLinksFn。
20
+ // 关键断言:非法 URL 拒绝且不写入 storyLink/storyActivity(双重零触达);链接去重与
21
+ // 排序断言。mock 骨架同 workspace-kb-idor.test.ts 样板。
22
+ import { beforeEach, describe, expect, it, vi } from "vitest";
23
+
24
+ vi.mock("@tanstack/react-start", () => ({
25
+ createServerFn: (_config: unknown) => {
26
+ const api: any = {
27
+ _validator: undefined,
28
+ validator(schema: any) {
29
+ this._validator = schema;
30
+ return this;
31
+ },
32
+ handler(fn: Function) {
33
+ return async (args: any) => {
34
+ let data = args?.data;
35
+ if (this._validator && data !== undefined) {
36
+ data = this._validator.parse(data);
37
+ }
38
+ return fn({ data });
39
+ };
40
+ },
41
+ };
42
+ return api;
43
+ },
44
+ }));
45
+ vi.mock("@tanstack/react-start/server", () => ({ getRequest: () => ({}) }));
46
+
47
+ const M = vi.hoisted(() => {
48
+ const f = () => vi.fn();
49
+ return {
50
+ requireAuth: f(),
51
+ storyLink: { create: f(), update: f(), findUnique: f(), findMany: f(), delete: f() },
52
+ storyActivity: { create: f() },
53
+ };
54
+ });
55
+
56
+ vi.mock("@allbluecn/database", () => ({
57
+ prisma: {
58
+ storyLink: M.storyLink,
59
+ storyActivity: M.storyActivity,
60
+ },
61
+ Prisma: {},
62
+ }));
63
+ vi.mock("@/server/auth-helpers", () => ({ requireAuth: M.requireAuth }));
64
+
65
+ import {
66
+ addLinkFn,
67
+ getLinksFn,
68
+ removeLinkFn,
69
+ updateLinkFn,
70
+ } from "../story/story-links";
71
+
72
+ const USER = "u-current";
73
+ const STORY_ID = "st-1";
74
+ const LINK_ID = "lk-1";
75
+ const URL_1 = "https://docs.example.com/spec";
76
+ const T1 = new Date("2026-01-01T00:00:00Z");
77
+
78
+ const userRow = {
79
+ id: USER,
80
+ username: "alice",
81
+ email: "alice@example.com",
82
+ avatarConfig: null,
83
+ bgShape: null,
84
+ bgColor: null,
85
+ };
86
+ const activityRow = (over: Record<string, unknown> = {}) => ({
87
+ id: "act-1",
88
+ action: "link_added",
89
+ field: null,
90
+ oldValue: null,
91
+ newValue: null,
92
+ isAI: false,
93
+ createdAt: T1,
94
+ user: userRow,
95
+ ...over,
96
+ });
97
+ const linkRow = (over: Record<string, unknown> = {}) => ({
98
+ id: LINK_ID,
99
+ storyId: STORY_ID,
100
+ url: URL_1,
101
+ title: null,
102
+ description: null,
103
+ ...over,
104
+ });
105
+
106
+ beforeEach(() => {
107
+ vi.clearAllMocks();
108
+ M.requireAuth.mockResolvedValue({ user: { id: USER } });
109
+ M.storyActivity.create.mockResolvedValue(activityRow());
110
+ });
111
+
112
+ describe("addLinkFn", () => {
113
+ it("写入链接并附带 link_added 活动记录", async () => {
114
+ M.storyLink.create.mockResolvedValue(linkRow({ title: "规范文档" }));
115
+ M.storyActivity.create.mockResolvedValue(activityRow({ action: "link_added", newValue: "规范文档" }));
116
+
117
+ const res = await addLinkFn({
118
+ data: { storyId: STORY_ID, url: URL_1, title: "规范文档", description: "外链说明" },
119
+ });
120
+
121
+ expect(M.storyLink.create).toHaveBeenCalledWith({
122
+ data: { storyId: STORY_ID, url: URL_1, title: "规范文档", description: "外链说明" },
123
+ });
124
+ expect(M.storyActivity.create).toHaveBeenCalledWith({
125
+ data: { storyId: STORY_ID, userId: USER, action: "link_added", newValue: "规范文档" },
126
+ select: expect.any(Object),
127
+ });
128
+ expect(res.id).toBe(LINK_ID);
129
+ expect(res.activity).toMatchObject({
130
+ id: "act-1",
131
+ action: "link_added",
132
+ createdAt: T1.toISOString(),
133
+ user: expect.objectContaining({ id: USER, username: "alice" }),
134
+ });
135
+ });
136
+
137
+ it("无标题时活动记录回退使用 URL", async () => {
138
+ M.storyLink.create.mockResolvedValue(linkRow({ title: null }));
139
+ await addLinkFn({ data: { storyId: STORY_ID, url: URL_1 } });
140
+ expect(M.storyActivity.create).toHaveBeenCalledWith(
141
+ expect.objectContaining({ data: expect.objectContaining({ newValue: URL_1 }) }),
142
+ );
143
+ });
144
+
145
+ it("非合法 URL 被 validator 拒绝且不写库", async () => {
146
+ await expect(addLinkFn({ data: { storyId: STORY_ID, url: "not-a-url" } })).rejects.toThrow();
147
+ expect(M.storyLink.create).not.toHaveBeenCalled();
148
+ expect(M.storyActivity.create).not.toHaveBeenCalled();
149
+ });
150
+
151
+ it("未登录拒绝且不写库", async () => {
152
+ M.requireAuth.mockRejectedValue(new Error("UNAUTHORIZED"));
153
+ await expect(addLinkFn({ data: { storyId: STORY_ID, url: URL_1 } })).rejects.toThrow("UNAUTHORIZED");
154
+ expect(M.storyLink.create).not.toHaveBeenCalled();
155
+ expect(M.storyActivity.create).not.toHaveBeenCalled();
156
+ });
157
+ });
158
+
159
+ describe("updateLinkFn", () => {
160
+ it("仅写入实际传入的字段", async () => {
161
+ M.storyLink.update.mockResolvedValue(linkRow({ title: "新标题" }));
162
+ await updateLinkFn({ data: { linkId: LINK_ID, title: "新标题" } });
163
+ expect(M.storyLink.update).toHaveBeenCalledWith({
164
+ where: { id: LINK_ID },
165
+ data: { title: "新标题" },
166
+ });
167
+ });
168
+
169
+ it("未传任何可变字段时 data 为空对象(不做无意义写入)", async () => {
170
+ M.storyLink.update.mockResolvedValue(linkRow());
171
+ await updateLinkFn({ data: { linkId: LINK_ID } });
172
+ expect(M.storyLink.update).toHaveBeenCalledWith({ where: { id: LINK_ID }, data: {} });
173
+ });
174
+
175
+ it("活动记录归属链接所属需求", async () => {
176
+ M.storyLink.update.mockResolvedValue(linkRow({ title: "T" }));
177
+ await updateLinkFn({ data: { linkId: LINK_ID, url: URL_1 } });
178
+ expect(M.storyActivity.create).toHaveBeenCalledWith(
179
+ expect.objectContaining({
180
+ data: expect.objectContaining({ storyId: STORY_ID, action: "link_updated", newValue: "T" }),
181
+ }),
182
+ );
183
+ });
184
+
185
+ it("非合法 URL 被 validator 拒绝且不更新", async () => {
186
+ await expect(updateLinkFn({ data: { linkId: LINK_ID, url: "bad" } })).rejects.toThrow();
187
+ expect(M.storyLink.update).not.toHaveBeenCalled();
188
+ });
189
+
190
+ it("未登录拒绝且不更新", async () => {
191
+ M.requireAuth.mockRejectedValue(new Error("UNAUTHORIZED"));
192
+ await expect(updateLinkFn({ data: { linkId: LINK_ID } })).rejects.toThrow("UNAUTHORIZED");
193
+ expect(M.storyLink.update).not.toHaveBeenCalled();
194
+ });
195
+ });
196
+
197
+ describe("removeLinkFn", () => {
198
+ it("链接存在时删除并记录 link_removed 活动", async () => {
199
+ M.storyLink.findUnique.mockResolvedValue(linkRow({ title: "规范文档" }));
200
+ M.storyLink.delete.mockResolvedValue(linkRow());
201
+ M.storyActivity.create.mockResolvedValue(activityRow({ action: "link_removed", newValue: "规范文档" }));
202
+
203
+ const res = await removeLinkFn({ data: { linkId: LINK_ID } });
204
+
205
+ expect(M.storyLink.delete).toHaveBeenCalledWith({ where: { id: LINK_ID } });
206
+ expect(M.storyActivity.create).toHaveBeenCalledWith(
207
+ expect.objectContaining({
208
+ data: expect.objectContaining({ storyId: STORY_ID, action: "link_removed", newValue: "规范文档" }),
209
+ }),
210
+ );
211
+ expect(res.activity).toMatchObject({ action: "link_removed" });
212
+ });
213
+
214
+ it("链接已不存在时仍执行删除但活动为 null", async () => {
215
+ M.storyLink.findUnique.mockResolvedValue(null);
216
+ M.storyLink.delete.mockResolvedValue(linkRow());
217
+
218
+ const res = await removeLinkFn({ data: { linkId: LINK_ID } });
219
+
220
+ expect(M.storyLink.delete).toHaveBeenCalledWith({ where: { id: LINK_ID } });
221
+ expect(M.storyActivity.create).not.toHaveBeenCalled();
222
+ expect(res).toEqual({ activity: null });
223
+ });
224
+
225
+ it("未登录拒绝且不查询不删除", async () => {
226
+ M.requireAuth.mockRejectedValue(new Error("UNAUTHORIZED"));
227
+ await expect(removeLinkFn({ data: { linkId: LINK_ID } })).rejects.toThrow("UNAUTHORIZED");
228
+ expect(M.storyLink.findUnique).not.toHaveBeenCalled();
229
+ expect(M.storyLink.delete).not.toHaveBeenCalled();
230
+ });
231
+ });
232
+
233
+ describe("getLinksFn", () => {
234
+ it("按 createdAt 倒序返回精简链接字段", async () => {
235
+ M.storyLink.findMany.mockResolvedValue([
236
+ { id: "lk-2", storyId: STORY_ID, url: URL_1, title: "B", description: "d2" },
237
+ { id: "lk-1", storyId: STORY_ID, url: URL_1, title: null, description: null },
238
+ ]);
239
+
240
+ const res = await getLinksFn({ data: { storyId: STORY_ID } });
241
+
242
+ expect(M.storyLink.findMany).toHaveBeenCalledWith({
243
+ where: { storyId: STORY_ID },
244
+ orderBy: { createdAt: "desc" },
245
+ });
246
+ expect(res).toEqual([
247
+ { id: "lk-2", url: URL_1, title: "B", description: "d2" },
248
+ { id: "lk-1", url: URL_1, title: null, description: null },
249
+ ]);
250
+ });
251
+
252
+ it("无链接时返回空数组", async () => {
253
+ M.storyLink.findMany.mockResolvedValue([]);
254
+ await expect(getLinksFn({ data: { storyId: STORY_ID } })).resolves.toEqual([]);
255
+ });
256
+
257
+ it("未登录拒绝且不查询", async () => {
258
+ M.requireAuth.mockRejectedValue(new Error("UNAUTHORIZED"));
259
+ await expect(getLinksFn({ data: { storyId: STORY_ID } })).rejects.toThrow("UNAUTHORIZED");
260
+ expect(M.storyLink.findMany).not.toHaveBeenCalled();
261
+ });
262
+ });
@@ -0,0 +1,259 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // AllBlue - 理想之海
3
+ // Copyright (C) 2026 AllBlue Contributors
4
+ //
5
+ // This program is free software: you can redistribute it and/or modify
6
+ // it under the terms of the GNU Affero General Public License as published by
7
+ // the Free Software Foundation, either version 3 of the License, or
8
+ // (at your option) any later version.
9
+ //
10
+ // This program is distributed in the hope that it will be useful,
11
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ // GNU Affero General Public License for more details.
14
+ //
15
+ // You should have received a copy of the GNU Affero General Public License
16
+ // along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+
18
+
19
+ // story 列表查询纯函数覆盖补全:buildListWhere / buildGroupFilter。
20
+ // 纯函数无 serverFn,仅 mock @allbluecn/database 的 Prisma(json 路径构造)。
21
+ // 关键断言:JSON 条件 path/operator 枚举映射、空条件短路、分组过滤的 AND/OR 组合形状。
22
+ import { describe, expect, it, vi } from "vitest";
23
+
24
+ vi.mock("@allbluecn/database", () => ({ Prisma: {} }));
25
+
26
+ import { buildListWhere, buildGroupFilter } from "../story/story-list-query";
27
+
28
+ /** 单值/多值字段构造过滤器 JSON */
29
+ const fc = (property: string, operator: string, value: string) =>
30
+ JSON.stringify([{ property, operator, value }]);
31
+
32
+ describe("buildListWhere 基础过滤", () => {
33
+ it("仅 archived 恒为过滤项", () => {
34
+ expect(buildListWhere({ archived: true })).toEqual({ archived: true });
35
+ });
36
+
37
+ it("state/priority 支持单值与逗号多值", () => {
38
+ expect(buildListWhere({ archived: false, state: "todo" })).toEqual({ archived: false, state: "todo" });
39
+ expect(
40
+ buildListWhere({ archived: false, state: "todo,backlog", priority: "high" }),
41
+ ).toEqual({ archived: false, state: { in: ["todo", "backlog"] }, priority: "high" });
42
+ expect(
43
+ buildListWhere({ archived: false, priority: "high,low" }),
44
+ ).toEqual({ archived: false, priority: { in: ["high", "low"] } });
45
+ });
46
+
47
+ it("assigneeId 支持指定成员、__none__ 占位符与多值", () => {
48
+ expect(buildListWhere({ archived: false, assigneeId: "u-1" })).toEqual({
49
+ archived: false,
50
+ assigneeId: "u-1",
51
+ });
52
+ expect(buildListWhere({ archived: false, assigneeId: "__none__" })).toEqual({
53
+ archived: false,
54
+ assigneeId: null,
55
+ });
56
+ expect(buildListWhere({ archived: false, assigneeId: "u-1,u-2" })).toEqual({
57
+ archived: false,
58
+ assigneeId: { in: ["u-1", "u-2"] },
59
+ });
60
+ });
61
+
62
+ it("labelId 单值/多值映射为 labels.some", () => {
63
+ expect(buildListWhere({ archived: false, labelId: "lb-1" })).toEqual({
64
+ archived: false,
65
+ labels: { some: { labelId: "lb-1" } },
66
+ });
67
+ expect(buildListWhere({ archived: false, labelId: "lb-1,lb-2" })).toEqual({
68
+ archived: false,
69
+ labels: { some: { labelId: { in: ["lb-1", "lb-2"] } } },
70
+ });
71
+ });
72
+
73
+ it("projectId 原样透传", () => {
74
+ expect(buildListWhere({ archived: false, projectId: "pr-9" })).toEqual({
75
+ archived: false,
76
+ projectId: "pr-9",
77
+ });
78
+ });
79
+
80
+ it("search 对 name 与 identifier 做不区分大小写包含匹配", () => {
81
+ expect(buildListWhere({ archived: false, search: "登录" })).toEqual({
82
+ archived: false,
83
+ OR: [
84
+ { name: { contains: "登录", mode: "insensitive" } },
85
+ { identifier: { contains: "登录", mode: "insensitive" } },
86
+ ],
87
+ });
88
+ });
89
+ });
90
+
91
+ describe("buildListWhere filters(JSON 条件)", () => {
92
+ it("state/priority 支持 is 与 is_not_any_of", () => {
93
+ expect(buildListWhere({ archived: false, filters: fc("state", "is_not_any_of", "done") })).toEqual({
94
+ archived: false,
95
+ AND: [{ state: { notIn: ["done"] } }],
96
+ });
97
+ expect(buildListWhere({ archived: false, filters: fc("priority", "is", "high,urgent") })).toEqual({
98
+ archived: false,
99
+ AND: [{ priority: { in: ["high", "urgent"] } }],
100
+ });
101
+ });
102
+
103
+ it("assigneeId 支持 is_empty / is_not_empty 与指定值", () => {
104
+ expect(buildListWhere({ archived: false, filters: fc("assigneeId", "is_empty", "") })).toEqual({
105
+ archived: false,
106
+ AND: [{ assigneeId: null }],
107
+ });
108
+ expect(buildListWhere({ archived: false, filters: fc("assigneeId", "is_not_empty", "") })).toEqual({
109
+ archived: false,
110
+ AND: [{ assigneeId: { not: null } }],
111
+ });
112
+ expect(buildListWhere({ archived: false, filters: fc("assigneeId", "is", "u-7") })).toEqual({
113
+ archived: false,
114
+ AND: [{ assigneeId: "u-7" }],
115
+ });
116
+ expect(buildListWhere({ archived: false, filters: fc("assigneeId", "is_not", "u-7") })).toEqual({
117
+ archived: false,
118
+ AND: [{ assigneeId: { not: "u-7" } }],
119
+ });
120
+ });
121
+
122
+ it("labelId 支持 some / none 与有无标签判断", () => {
123
+ expect(buildListWhere({ archived: false, filters: fc("labelId", "is", "lb-1,lb-2") })).toEqual({
124
+ archived: false,
125
+ AND: [{ labels: { some: { labelId: { in: ["lb-1", "lb-2"] } } } }],
126
+ });
127
+ expect(buildListWhere({ archived: false, filters: fc("labelId", "is_not_any_of", "lb-1") })).toEqual({
128
+ archived: false,
129
+ AND: [{ labels: { none: { labelId: { in: ["lb-1"] } } } }],
130
+ });
131
+ expect(buildListWhere({ archived: false, filters: fc("labelId", "is_empty", "") })).toEqual({
132
+ archived: false,
133
+ AND: [{ labels: { none: {} } }],
134
+ });
135
+ expect(buildListWhere({ archived: false, filters: fc("labelId", "is_not_empty", "") })).toEqual({
136
+ archived: false,
137
+ AND: [{ labels: { some: {} } }],
138
+ });
139
+ });
140
+
141
+ it("createdDate 支持 before/after/on_or_after/on_or_before/is/is_not/between", () => {
142
+ const d = "2026-03-05";
143
+ const day = new Date(d);
144
+ const next = new Date(d);
145
+ next.setDate(next.getDate() + 1);
146
+
147
+ expect(buildListWhere({ archived: false, filters: fc("createdDate", "before", d) })).toEqual({
148
+ archived: false,
149
+ AND: [{ createdAt: { lt: day } }],
150
+ });
151
+ expect(buildListWhere({ archived: false, filters: fc("createdDate", "after", d) })).toEqual({
152
+ archived: false,
153
+ AND: [{ createdAt: { gte: day } }],
154
+ });
155
+ expect(buildListWhere({ archived: false, filters: fc("createdDate", "on_or_before", d) })).toEqual({
156
+ archived: false,
157
+ AND: [{ createdAt: { lte: day } }],
158
+ });
159
+ expect(buildListWhere({ archived: false, filters: fc("createdDate", "is", d) })).toEqual({
160
+ archived: false,
161
+ AND: [{ createdAt: { gte: day, lt: next } }],
162
+ });
163
+ expect(buildListWhere({ archived: false, filters: fc("createdDate", "is_not", d) })).toEqual({
164
+ archived: false,
165
+ AND: [{ createdAt: { lt: day, gte: next } }],
166
+ });
167
+ expect(
168
+ buildListWhere({ archived: false, filters: JSON.stringify([{ property: "createdDate", operator: "between", value: "2026-03-01,2026-03-05" }]) }),
169
+ ).toEqual({
170
+ archived: false,
171
+ AND: [{ createdAt: { gte: new Date("2026-03-01") } }, { createdAt: { lte: day } }],
172
+ });
173
+ });
174
+
175
+ it("targetDate 的日期条件与 createdDate 同构", () => {
176
+ const d = "2026-04-01";
177
+ const day = new Date(d);
178
+ expect(buildListWhere({ archived: false, filters: fc("targetDate", "before", d) })).toEqual({
179
+ archived: false,
180
+ AND: [{ targetDate: { lt: day } }],
181
+ });
182
+ expect(buildListWhere({ archived: false, filters: fc("targetDate", "after", d) })).toEqual({
183
+ archived: false,
184
+ AND: [{ targetDate: { gte: day } }],
185
+ });
186
+ expect(
187
+ buildListWhere({ archived: false, filters: JSON.stringify([{ property: "targetDate", operator: "between", value: "2026-04-01,2026-04-10" }]) }),
188
+ ).toEqual({
189
+ archived: false,
190
+ AND: [{ targetDate: { gte: day } }, { targetDate: { lte: new Date("2026-04-10") } }],
191
+ });
192
+ });
193
+
194
+ it("title 支持 is_empty / is_not_empty / 包含 / not_contains", () => {
195
+ expect(buildListWhere({ archived: false, filters: fc("title", "is_empty", "") })).toEqual({
196
+ archived: false,
197
+ AND: [{ name: { equals: "" } }],
198
+ });
199
+ expect(buildListWhere({ archived: false, filters: fc("title", "is_not_empty", "") })).toEqual({
200
+ archived: false,
201
+ AND: [{ name: { not: "" } }],
202
+ });
203
+ expect(buildListWhere({ archived: false, filters: fc("title", "contains", " 登录 ") })).toEqual({
204
+ archived: false,
205
+ AND: [{ name: { contains: "登录", mode: "insensitive" } }],
206
+ });
207
+ expect(buildListWhere({ archived: false, filters: fc("title", "not_contains", "bug") })).toEqual({
208
+ archived: false,
209
+ AND: [{ name: { not: { contains: "bug" }, mode: "insensitive" } }],
210
+ });
211
+ });
212
+
213
+ it("非法 JSON 与非法日期静默忽略,不影响其他条件", () => {
214
+ expect(buildListWhere({ archived: false, filters: "{not-json" })).toEqual({ archived: false });
215
+ expect(
216
+ buildListWhere({
217
+ archived: false,
218
+ filters: JSON.stringify([
219
+ { property: "createdDate", operator: "before", value: "不是日期" },
220
+ { property: "state", operator: "is", value: "todo" },
221
+ ]),
222
+ }),
223
+ ).toEqual({ archived: false, AND: [{ state: "todo" }] });
224
+ });
225
+
226
+ it("多条条件按序聚合到 AND 数组", () => {
227
+ expect(
228
+ buildListWhere({
229
+ archived: false,
230
+ filters: JSON.stringify([
231
+ { property: "state", operator: "is", value: "todo" },
232
+ { property: "assigneeId", operator: "is_empty", value: "" },
233
+ { property: "labelId", operator: "is_not_empty", value: "" },
234
+ ]),
235
+ }),
236
+ ).toEqual({
237
+ archived: false,
238
+ AND: [{ state: "todo" }, { assigneeId: null }, { labels: { some: {} } }],
239
+ });
240
+ });
241
+ });
242
+
243
+ describe("buildGroupFilter", () => {
244
+ it("groupBy 为 none 时返回空过滤", () => {
245
+ expect(buildGroupFilter("none", "todo")).toEqual({});
246
+ });
247
+
248
+ it("各分组维度映射为对应查询条件", () => {
249
+ expect(buildGroupFilter("state", "todo")).toEqual({ state: "todo" });
250
+ expect(buildGroupFilter("priority", "high")).toEqual({ priority: "high" });
251
+ expect(buildGroupFilter("assignee", "u-1")).toEqual({ assigneeId: "u-1" });
252
+ expect(buildGroupFilter("assignee", "unassigned")).toEqual({ assigneeId: null });
253
+ expect(buildGroupFilter("createdBy", "u-1")).toEqual({ createdById: "u-1" });
254
+ expect(buildGroupFilter("labels", "lb-1")).toEqual({ labels: { some: { labelId: "lb-1" } } });
255
+ expect(buildGroupFilter("labels", "no-labels")).toEqual({ labels: { none: {} } });
256
+ expect(buildGroupFilter("project", "pr-1")).toEqual({ projectId: "pr-1" });
257
+ expect(buildGroupFilter("project", "no-project")).toEqual({ projectId: null });
258
+ });
259
+ });