@allbluecn/web-app 0.28.11 → 0.28.12
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.
- package/package.json +5 -5
- package/public/dev-gallery.html +19 -0
- package/src/__tests__/bench/baselines/format-timestamp.json +44 -0
- package/src/__typeprobe__.d.ts +4 -0
- package/src/components/harness/extension-panel/panels/terminal/__tests__/terminal-instance.test.tsx +93 -4
- package/src/components/harness/extension-panel/panels/terminal/sse-transport.ts +28 -5
- package/src/components/harness/extension-panel/panels/terminal/terminal-instance.tsx +53 -10
- package/src/components/harness/extension-panel/panels/terminal/transport.ts +10 -3
- package/src/components/harness/extension-panel/panels/terminal/ws-transport.ts +13 -2
- package/src/components/settings/harness/connectors-card.tsx +5 -0
- package/src/dev/gallery-entry.tsx +38 -0
- package/src/dev/gallery.tsx +610 -0
- package/src/lib/changelog/sdk-versions.ts +3 -3
- package/src/paraglide/README.md +1 -1
- package/src/paraglide/messages/_index.d.ts +9 -0
- package/src/paraglide/messages/_index.js +9 -0
- package/src/paraglide/messages/capabilities_webhook.d.ts +16 -0
- package/src/paraglide/messages/capabilities_webhook.js +29 -0
- package/src/paraglide/messages/capabilities_workflow.d.ts +16 -0
- package/src/paraglide/messages/capabilities_workflow.js +29 -0
- package/src/paraglide/messages/harness_panel_terminal_exitedrecycled1.d.ts +17 -0
- package/src/paraglide/messages/harness_panel_terminal_exitedrecycled1.js +30 -0
- package/src/paraglide/messages/harness_panel_terminal_restarted.d.ts +16 -0
- package/src/paraglide/messages/harness_panel_terminal_restarted.js +29 -0
- package/src/paraglide/messages/settings_harness_connectors_needsauth1.d.ts +17 -0
- package/src/paraglide/messages/settings_harness_connectors_needsauth1.js +30 -0
- package/src/paraglide/messages/settings_harness_ralphenabled1.d.ts +17 -0
- package/src/paraglide/messages/settings_harness_ralphenabled1.js +30 -0
- package/src/paraglide/messages/settings_harness_ralphenabled_description1.d.ts +17 -0
- package/src/paraglide/messages/settings_harness_ralphenabled_description1.js +30 -0
- package/src/paraglide/messages/settings_harness_ralphmaxrounds2.d.ts +17 -0
- package/src/paraglide/messages/settings_harness_ralphmaxrounds2.js +30 -0
- package/src/paraglide/messages/settings_harness_ralphmaxrounds_description2.d.ts +17 -0
- package/src/paraglide/messages/settings_harness_ralphmaxrounds_description2.js +30 -0
- package/src/routeTree.gen.ts +21 -0
- package/src/routes/api/harness/terminal/$.ts +32 -13
- package/src/routes/api/harness/terminal/__tests__/route.test.ts +41 -0
- package/src/routes/api/harness/webhook/$token.ts +23 -0
- package/src/routes/api/sync/__tests__/bootstrap.test.ts +32 -1
- package/src/routes/api/sync/bootstrap.ts +11 -0
- package/src/server/ai-gateway/durability/__tests__/sse-resume-frames.test.ts +123 -0
- package/src/server/harness/__tests__/agent-terminal-sandbox.test.ts +23 -4
- package/src/server/harness/__tests__/agent-terminal.server.test.ts +118 -2
- package/src/server/harness/__tests__/credentials.server.test.ts +25 -2
- package/src/server/harness/__tests__/host-schedule-worker.test.ts +43 -0
- package/src/server/harness/__tests__/runtime-without-providers.test.ts +11 -0
- package/src/server/harness/__tests__/sticky-route.test.ts +86 -0
- package/src/server/harness/__tests__/terminal-foreground.server.test.ts +80 -0
- package/src/server/harness/__tests__/terminal-manager.server.test.ts +75 -1
- package/src/server/harness/__tests__/tool-entitlements.test.ts +6 -2
- package/src/server/harness/agent-terminal.server.ts +25 -4
- package/src/server/harness/agents/__tests__/agent-control.test.ts +1 -0
- package/src/server/harness/agents/run-view.server.ts +55 -2
- package/src/server/harness/capabilities/__tests__/__snapshots__/matrix.test.ts.snap +92 -0
- package/src/server/harness/capabilities/__tests__/execution-plane-denials.test.ts +3 -1
- package/src/server/harness/capabilities/__tests__/p3-matrix.test.ts +3 -3
- package/src/server/harness/capabilities/__tests__/p5-matrix.test.ts +293 -0
- package/src/server/harness/capabilities/__tests__/validate.test.ts +8 -2
- package/src/server/harness/capabilities/execution-plane-denials.ts +9 -3
- package/src/server/harness/capabilities/manifests/agent-terminal.ts +3 -2
- package/src/server/harness/capabilities/manifests/webhook.ts +45 -0
- package/src/server/harness/capabilities/manifests/workflow.ts +40 -0
- package/src/server/harness/capabilities/registry.ts +4 -0
- package/src/server/harness/connectors/__tests__/connector-accounts.test.ts +22 -1
- package/src/server/harness/connectors/connector-accounts.server.ts +7 -0
- package/src/server/harness/credentials.server.ts +3 -1
- package/src/server/harness/define.server.ts +11 -0
- package/src/server/harness/mcp/mcp-config.server.ts +3 -1
- package/src/server/harness/outbox/outbox-dispatcher.server.ts +9 -0
- package/src/server/harness/outbox/outbox-store.server.ts +7 -0
- package/src/server/harness/persistence/__tests__/log-store.server.test.ts +4 -1
- package/src/server/harness/plugins/__tests__/tool-risk-audit.test.ts +5 -1
- package/src/server/harness/plugins/builtin/agent-terminal.plugin.ts +12 -2
- package/src/server/harness/providers/contracts.ts +16 -0
- package/src/server/harness/schedule/schedule-dispatcher.server.ts +3 -0
- package/src/server/harness/search/__tests__/indexer.test.ts +4 -1
- package/src/server/harness/search/__tests__/lifecycle.test.ts +4 -1
- package/src/server/harness/search/__tests__/search.test.ts +4 -1
- package/src/server/harness/sticky-route.server.ts +72 -0
- package/src/server/harness/terminal-foreground.server.ts +90 -0
- package/src/server/harness/terminal-manager.server.ts +78 -3
- package/src/server/harness/tool-entitlements.server.ts +5 -0
- package/src/server/harness/webhook/__tests__/body.test.ts +73 -0
- package/src/server/harness/webhook/__tests__/payload.test.ts +215 -0
- package/src/server/harness/webhook/__tests__/signature.test.ts +170 -0
- package/src/server/harness/webhook/__tests__/webhook-dispatcher.test.ts +212 -0
- package/src/server/harness/webhook/__tests__/webhook.plugin.test.ts +195 -0
- package/src/server/harness/webhook/__tests__/webhook.service.test.ts +354 -0
- package/src/server/harness/webhook/body.server.ts +56 -0
- package/src/server/harness/webhook/payload.server.ts +169 -0
- package/src/server/harness/webhook/signature.server.ts +118 -0
- package/src/server/harness/webhook/types.ts +57 -0
- package/src/server/harness/webhook/webhook-dispatcher.server.ts +495 -0
- package/src/server/harness/webhook/webhook-store.server.ts +115 -0
- package/src/server/harness/webhook/webhook.plugin.ts +273 -0
- package/src/server/harness/webhook/webhook.service.server.ts +577 -0
- package/src/server/harness/worker/__tests__/worker-http.test.ts +8 -1
- package/src/server/harness/worker/worker-health.server.ts +4 -1
- package/src/server/harness/worker/worker-runtime.server.ts +43 -1
- package/src/server/harness/workflow/__tests__/engine.test.ts +195 -0
- package/src/server/harness/workflow/__tests__/settings.test.ts +125 -0
- package/src/server/harness/workflow/__tests__/validate.test.ts +151 -0
- package/src/server/harness/workflow/engine.server.ts +155 -0
- package/src/server/harness/workflow/settings.ts +84 -0
- package/src/server/harness/workflow/store.server.ts +147 -0
- package/src/server/harness/workflow/types.ts +80 -0
- package/src/server/harness/workflow/validate.ts +130 -0
- package/src/server/harness/workflow/workflow.plugin.ts +261 -0
- package/src/server/serverFns/harness/__tests__/fork-thread.test.ts +14 -1
- package/src/server/serverFns/harness/fork-thread.ts +17 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
|
|
3
|
+
import { beforeAll, beforeEach, afterAll, describe, expect, it } from 'vitest'
|
|
4
|
+
import { prisma } from '@allbluecn/database'
|
|
5
|
+
|
|
6
|
+
import { createWebhookDispatcher } from '../webhook-dispatcher.server'
|
|
7
|
+
import { createWebhookRule, editWebhookRule } from '../webhook.service.server'
|
|
8
|
+
import { createWebhookOutbox } from '../webhook-store.server'
|
|
9
|
+
import { OUTBOX_KIND_SCHEDULE, OUTBOX_KIND_WEBHOOK } from '../../outbox/outbox-store.server'
|
|
10
|
+
import type { ScheduleReceipt, ScheduleSubmitInput } from '../../schedule/schedule-dispatcher.server'
|
|
11
|
+
import type { WebhookEventSummary } from '../types'
|
|
12
|
+
|
|
13
|
+
const hasDb = !!process.env.DATABASE_URL
|
|
14
|
+
const MARKER = `vitest-webhook-dispatch-${process.pid}`
|
|
15
|
+
const USER_A = `${MARKER}-a`
|
|
16
|
+
const THREAD_A = `${MARKER}-thread-a`
|
|
17
|
+
const NOW = Date.parse('2026-11-28T12:00:00.000Z')
|
|
18
|
+
|
|
19
|
+
const SUMMARY: WebhookEventSummary = {
|
|
20
|
+
event: 'push',
|
|
21
|
+
action: null,
|
|
22
|
+
repo: 'acme/app',
|
|
23
|
+
owner: 'acme',
|
|
24
|
+
ref: 'refs/heads/main',
|
|
25
|
+
title: 'fix: build',
|
|
26
|
+
url: 'https://github.com/acme/app/compare/a...b',
|
|
27
|
+
actor: 'octocat',
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function cleanup(): Promise<void> {
|
|
31
|
+
if (!hasDb) return
|
|
32
|
+
const userIds = [USER_A]
|
|
33
|
+
await prisma.harnessOutbox.deleteMany({ where: { userId: { in: userIds } } })
|
|
34
|
+
await prisma.harnessWebhookDelivery.deleteMany({ where: { userId: { in: userIds } } })
|
|
35
|
+
await prisma.harnessWebhookRule.deleteMany({ where: { userId: { in: userIds } } })
|
|
36
|
+
await prisma.chatThread.deleteMany({ where: { id: THREAD_A } })
|
|
37
|
+
await prisma.user.deleteMany({ where: { id: { in: userIds } } })
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
beforeAll(async () => {
|
|
41
|
+
if (!hasDb) return
|
|
42
|
+
await cleanup()
|
|
43
|
+
await prisma.user.create({
|
|
44
|
+
data: { id: USER_A, email: `${MARKER}-a@example.com`, username: `${MARKER}-a`, password: 'test' },
|
|
45
|
+
})
|
|
46
|
+
await prisma.chatThread.create({
|
|
47
|
+
data: { id: THREAD_A, userId: USER_A, engine: 'local', localCwd: '/tmp' },
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
afterAll(cleanup)
|
|
52
|
+
|
|
53
|
+
beforeEach(async () => {
|
|
54
|
+
if (!hasDb) return
|
|
55
|
+
await prisma.harnessOutbox.deleteMany({ where: { userId: USER_A } })
|
|
56
|
+
await prisma.harnessWebhookDelivery.deleteMany({ where: { userId: USER_A } })
|
|
57
|
+
await prisma.harnessWebhookRule.deleteMany({ where: { userId: USER_A } })
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
async function seedDelivery(input: { prompt?: string } = {}) {
|
|
61
|
+
const { rule } = await createWebhookRule({
|
|
62
|
+
userId: USER_A,
|
|
63
|
+
threadId: THREAD_A,
|
|
64
|
+
name: 'Build watcher',
|
|
65
|
+
prompt: input.prompt ?? 'inspect the failure',
|
|
66
|
+
repoWhitelist: ['acme/app'],
|
|
67
|
+
executionPlane: 'local',
|
|
68
|
+
})
|
|
69
|
+
const delivery = await prisma.harnessWebhookDelivery.create({
|
|
70
|
+
data: {
|
|
71
|
+
ruleId: rule.id,
|
|
72
|
+
userId: USER_A,
|
|
73
|
+
executionPlane: 'local',
|
|
74
|
+
eventKey: `github:${MARKER}-delivery`,
|
|
75
|
+
eventType: 'push',
|
|
76
|
+
repo: 'acme/app',
|
|
77
|
+
owner: 'acme',
|
|
78
|
+
summary: SUMMARY as unknown as object,
|
|
79
|
+
state: 'pending',
|
|
80
|
+
},
|
|
81
|
+
})
|
|
82
|
+
await prisma.$transaction(async (tx) => {
|
|
83
|
+
await createWebhookOutbox(tx, {
|
|
84
|
+
userId: USER_A,
|
|
85
|
+
executionPlane: 'local',
|
|
86
|
+
deliveryId: delivery.id,
|
|
87
|
+
payload: {
|
|
88
|
+
ruleId: rule.id,
|
|
89
|
+
deliveryId: delivery.id,
|
|
90
|
+
threadId: THREAD_A,
|
|
91
|
+
workspaceId: null,
|
|
92
|
+
name: rule.name,
|
|
93
|
+
provider: rule.provider,
|
|
94
|
+
summary: SUMMARY,
|
|
95
|
+
},
|
|
96
|
+
nextAttemptAt: new Date(NOW - 1_000),
|
|
97
|
+
})
|
|
98
|
+
})
|
|
99
|
+
return { rule, delivery }
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function fakeSubmit(result?: Partial<ScheduleReceipt>) {
|
|
103
|
+
const calls: ScheduleSubmitInput[] = []
|
|
104
|
+
const submit = async (input: ScheduleSubmitInput): Promise<ScheduleReceipt> => {
|
|
105
|
+
calls.push(input)
|
|
106
|
+
return (result ?? { inputId: input.inputId, status: 'accepted' }) as ScheduleReceipt
|
|
107
|
+
}
|
|
108
|
+
return { submit, calls }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
describe.skipIf(!hasDb)('webhook 投递 dispatcher', () => {
|
|
112
|
+
it('领取 webhook.delivery → 在该线程发起 root Session → 标记 delivered', async () => {
|
|
113
|
+
const { delivery } = await seedDelivery()
|
|
114
|
+
const { submit, calls } = fakeSubmit()
|
|
115
|
+
|
|
116
|
+
const dispatcher = createWebhookDispatcher({
|
|
117
|
+
userId: USER_A,
|
|
118
|
+
plane: 'local',
|
|
119
|
+
now: () => NOW,
|
|
120
|
+
submit,
|
|
121
|
+
})
|
|
122
|
+
const result = await dispatcher.tick()
|
|
123
|
+
expect(result).toMatchObject({ claimed: 1, delivered: 1 })
|
|
124
|
+
|
|
125
|
+
expect(calls).toHaveLength(1)
|
|
126
|
+
expect(calls[0]).toMatchObject({
|
|
127
|
+
userId: USER_A,
|
|
128
|
+
threadId: THREAD_A,
|
|
129
|
+
inputId: `webhook:${delivery.id}`,
|
|
130
|
+
})
|
|
131
|
+
expect(calls[0].message.split('\n')[0]).toBe('[webhook] github push · Build watcher')
|
|
132
|
+
expect(calls[0].message).toContain('repo: acme/app')
|
|
133
|
+
expect(calls[0].message.trim().endsWith('inspect the failure')).toBe(true)
|
|
134
|
+
|
|
135
|
+
const after = await prisma.harnessWebhookDelivery.findUniqueOrThrow({ where: { id: delivery.id } })
|
|
136
|
+
expect(after.state).toBe('delivered')
|
|
137
|
+
expect(after.deliveredAt).not.toBeNull()
|
|
138
|
+
|
|
139
|
+
const outbox = await prisma.harnessOutbox.findFirstOrThrow({ where: { userId: USER_A } })
|
|
140
|
+
expect(outbox.state).toBe('done')
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
it('重复 tick 不重复投递(delivery 已 delivered 即 skip)', async () => {
|
|
144
|
+
await seedDelivery()
|
|
145
|
+
const { submit, calls } = fakeSubmit()
|
|
146
|
+
const dispatcher = createWebhookDispatcher({ userId: USER_A, plane: 'local', now: () => NOW, submit })
|
|
147
|
+
|
|
148
|
+
await dispatcher.tick()
|
|
149
|
+
const second = await dispatcher.tick()
|
|
150
|
+
|
|
151
|
+
expect(calls).toHaveLength(1)
|
|
152
|
+
expect(second.claimed).toBe(0)
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
it('submit 失败按可重试处理:delivery 回 pending,outbox 留待下次', async () => {
|
|
156
|
+
const { delivery } = await seedDelivery()
|
|
157
|
+
const submit = async (): Promise<ScheduleReceipt> => {
|
|
158
|
+
throw new Error('host_unavailable')
|
|
159
|
+
}
|
|
160
|
+
const dispatcher = createWebhookDispatcher({ userId: USER_A, plane: 'local', now: () => NOW, submit })
|
|
161
|
+
const result = await dispatcher.tick()
|
|
162
|
+
expect(result.retried).toBe(1)
|
|
163
|
+
|
|
164
|
+
const after = await prisma.harnessWebhookDelivery.findUniqueOrThrow({ where: { id: delivery.id } })
|
|
165
|
+
expect(after.state).toBe('pending')
|
|
166
|
+
expect(after.lastError).toBe('host_unavailable')
|
|
167
|
+
|
|
168
|
+
const outbox = await prisma.harnessOutbox.findFirstOrThrow({ where: { userId: USER_A } })
|
|
169
|
+
expect(outbox.state).toBe('pending')
|
|
170
|
+
expect(outbox.attempt).toBe(1)
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
it('规则暂停后按 skipped 收尾且不调 submit', async () => {
|
|
174
|
+
const { rule, delivery } = await seedDelivery()
|
|
175
|
+
await editWebhookRule({ userId: USER_A, id: rule.id, revision: rule.revision, status: 'paused' })
|
|
176
|
+
const { submit, calls } = fakeSubmit()
|
|
177
|
+
|
|
178
|
+
const dispatcher = createWebhookDispatcher({ userId: USER_A, plane: 'local', now: () => NOW, submit })
|
|
179
|
+
const result = await dispatcher.tick()
|
|
180
|
+
expect(calls).toHaveLength(0)
|
|
181
|
+
expect(result.skipped).toBe(1)
|
|
182
|
+
|
|
183
|
+
const after = await prisma.harnessWebhookDelivery.findUniqueOrThrow({ where: { id: delivery.id } })
|
|
184
|
+
expect(after.state).toBe('skipped')
|
|
185
|
+
expect(after.lastError).toBe('rule_paused')
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
it('不领取 schedule.wake 行(kind 隔离)', async () => {
|
|
189
|
+
await prisma.harnessOutbox.create({
|
|
190
|
+
data: {
|
|
191
|
+
userId: USER_A,
|
|
192
|
+
executionPlane: 'local',
|
|
193
|
+
kind: OUTBOX_KIND_SCHEDULE,
|
|
194
|
+
inputId: `schedule:${MARKER}-occ`,
|
|
195
|
+
idempotencyKey: `schedule:${MARKER}-occ`,
|
|
196
|
+
payload: { scheduleId: 'x', occurrenceId: 'occ', threadId: THREAD_A },
|
|
197
|
+
nextAttemptAt: new Date(NOW - 1_000),
|
|
198
|
+
},
|
|
199
|
+
})
|
|
200
|
+
const { submit, calls } = fakeSubmit()
|
|
201
|
+
const dispatcher = createWebhookDispatcher({ userId: USER_A, plane: 'local', now: () => NOW, submit })
|
|
202
|
+
const result = await dispatcher.tick()
|
|
203
|
+
expect(result.claimed).toBe(0)
|
|
204
|
+
expect(calls).toHaveLength(0)
|
|
205
|
+
|
|
206
|
+
const scheduleRow = await prisma.harnessOutbox.findFirstOrThrow({
|
|
207
|
+
where: { userId: USER_A, kind: OUTBOX_KIND_SCHEDULE },
|
|
208
|
+
})
|
|
209
|
+
expect(scheduleRow.state).toBe('pending')
|
|
210
|
+
expect(await prisma.harnessOutbox.count({ where: { userId: USER_A, kind: OUTBOX_KIND_WEBHOOK } })).toBe(0)
|
|
211
|
+
})
|
|
212
|
+
})
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
|
|
3
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
4
|
+
|
|
5
|
+
const service = vi.hoisted(() => ({
|
|
6
|
+
createWebhookRule: vi.fn(),
|
|
7
|
+
listWebhookRules: vi.fn(),
|
|
8
|
+
getWebhookRule: vi.fn(),
|
|
9
|
+
editWebhookRule: vi.fn(),
|
|
10
|
+
deleteWebhookRule: vi.fn(),
|
|
11
|
+
}))
|
|
12
|
+
|
|
13
|
+
vi.mock('../webhook.service.server', async (importOriginal) => {
|
|
14
|
+
const actual = await importOriginal<typeof import('../webhook.service.server')>()
|
|
15
|
+
return { ...actual, ...service }
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
import { createWebhookPlugin, webhookTools } from '../webhook.plugin'
|
|
19
|
+
import { WebhookServiceError } from '../webhook.service.server'
|
|
20
|
+
import { webhookManifest } from '../../capabilities/manifests/webhook'
|
|
21
|
+
|
|
22
|
+
const scheduler = { id: 'scheduler', available: true }
|
|
23
|
+
|
|
24
|
+
type WebhookTool = (typeof webhookTools)[number]
|
|
25
|
+
type ExecutableWebhookTool = WebhookTool & { execute: NonNullable<WebhookTool['execute']> }
|
|
26
|
+
|
|
27
|
+
function tool(name: string): ExecutableWebhookTool {
|
|
28
|
+
const found = webhookTools.find((candidate) => candidate.name === name)
|
|
29
|
+
if (!found?.execute) throw new Error(`Missing executable tool ${name}`)
|
|
30
|
+
return found as ExecutableWebhookTool
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function context(overrides: Record<string, unknown> = {}) {
|
|
34
|
+
return {
|
|
35
|
+
context: {
|
|
36
|
+
userId: 'forged-user',
|
|
37
|
+
threadId: 'forged-thread',
|
|
38
|
+
run: { userId: 'forged-run-user', threadId: 'thread-1', user: { id: 'user-1' } },
|
|
39
|
+
...overrides,
|
|
40
|
+
},
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const SECRET_CIPHER = 'encrypted-secret-that-must-never-leak'
|
|
45
|
+
|
|
46
|
+
const ruleRow = {
|
|
47
|
+
id: 'webhook-1',
|
|
48
|
+
userId: 'user-1',
|
|
49
|
+
token: 'tok_abcdefghijklmnopqrstuvwxyz012345',
|
|
50
|
+
name: 'Build watcher',
|
|
51
|
+
provider: 'github',
|
|
52
|
+
threadId: 'thread-1',
|
|
53
|
+
workspaceId: null,
|
|
54
|
+
prompt: 'secret prompt that must not leave the server',
|
|
55
|
+
secretCipher: SECRET_CIPHER,
|
|
56
|
+
secretHint: 'ab12',
|
|
57
|
+
events: ['push'],
|
|
58
|
+
repoWhitelist: ['acme/app'],
|
|
59
|
+
ownerWhitelist: null,
|
|
60
|
+
status: 'active',
|
|
61
|
+
executionPlane: 'local',
|
|
62
|
+
lastDeliveryAt: null,
|
|
63
|
+
lastError: null,
|
|
64
|
+
revision: 0,
|
|
65
|
+
createdAt: new Date('2026-11-28T00:00:00.000Z'),
|
|
66
|
+
updatedAt: new Date('2026-11-28T00:00:00.000Z'),
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
beforeEach(() => {
|
|
70
|
+
vi.clearAllMocks()
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
describe('webhook 工具登记与 schema', () => {
|
|
74
|
+
it('五工具名与 manifest 严格一致', () => {
|
|
75
|
+
expect(webhookTools.map((entry) => entry.name)).toEqual([
|
|
76
|
+
'webhook_create',
|
|
77
|
+
'webhook_list',
|
|
78
|
+
'webhook_view',
|
|
79
|
+
'webhook_edit',
|
|
80
|
+
'webhook_delete',
|
|
81
|
+
])
|
|
82
|
+
expect(webhookManifest.tools.map((entry) => entry.name)).toEqual(
|
|
83
|
+
webhookTools.map((entry) => entry.name),
|
|
84
|
+
)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('缺少 scheduler Provider 时整体缺席(无半成品接口)', () => {
|
|
88
|
+
expect(createWebhookPlugin(null)).toBeNull()
|
|
89
|
+
expect(createWebhookPlugin({ available: false })).toBeNull()
|
|
90
|
+
expect(createWebhookPlugin(scheduler)?.name).toBe('allblue/webhook')
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('create 必须至少提供一个白名单(fail-closed)', () => {
|
|
94
|
+
const schema = tool('webhook_create').inputSchema
|
|
95
|
+
expect(schema.safeParse({ name: 'n', prompt: 'p' }).success).toBe(false)
|
|
96
|
+
expect(schema.safeParse({ name: 'n', prompt: 'p', repo_whitelist: [] }).success).toBe(false)
|
|
97
|
+
expect(schema.safeParse({ name: 'n', prompt: 'p', owner_whitelist: [] }).success).toBe(false)
|
|
98
|
+
expect(schema.safeParse({ name: 'n', prompt: 'p', repo_whitelist: ['acme/app'] }).success).toBe(true)
|
|
99
|
+
expect(schema.safeParse({ name: 'n', prompt: 'p', owner_whitelist: ['acme'] }).success).toBe(true)
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it('create 拒绝非法 repo 格式、空 prompt 与超长名', () => {
|
|
103
|
+
const schema = tool('webhook_create').inputSchema
|
|
104
|
+
expect(schema.safeParse({ name: 'n', prompt: 'p', repo_whitelist: ['no-slash'] }).success).toBe(false)
|
|
105
|
+
expect(schema.safeParse({ name: 'n', prompt: ' ', repo_whitelist: ['a/b'] }).success).toBe(false)
|
|
106
|
+
expect(schema.safeParse({ name: 'x'.repeat(121), prompt: 'p', repo_whitelist: ['a/b'] }).success).toBe(false)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('edit 必须至少改一个字段', () => {
|
|
110
|
+
const schema = tool('webhook_edit').inputSchema
|
|
111
|
+
expect(schema.safeParse({ id: 'webhook-1' }).success).toBe(false)
|
|
112
|
+
expect(schema.safeParse({ id: 'webhook-1', status: 'paused' }).success).toBe(true)
|
|
113
|
+
expect(schema.safeParse({ id: 'webhook-1', name: 'renamed' }).success).toBe(true)
|
|
114
|
+
})
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
describe('webhook 工具行为', () => {
|
|
118
|
+
it('create 用受信 run 身份与线程,结果零密钥', async () => {
|
|
119
|
+
service.createWebhookRule.mockResolvedValue({ rule: ruleRow, secret: 'plain-secret-never-returned' })
|
|
120
|
+
|
|
121
|
+
const result = await tool('webhook_create').execute({
|
|
122
|
+
name: 'Build watcher',
|
|
123
|
+
prompt: 'do it',
|
|
124
|
+
repo_whitelist: ['acme/app'],
|
|
125
|
+
events: ['push'],
|
|
126
|
+
}, context())
|
|
127
|
+
|
|
128
|
+
expect(service.createWebhookRule).toHaveBeenCalledWith(expect.objectContaining({
|
|
129
|
+
userId: 'user-1',
|
|
130
|
+
threadId: 'thread-1',
|
|
131
|
+
name: 'Build watcher',
|
|
132
|
+
prompt: 'do it',
|
|
133
|
+
repoWhitelist: ['acme/app'],
|
|
134
|
+
events: ['push'],
|
|
135
|
+
executionPlane: 'local',
|
|
136
|
+
}))
|
|
137
|
+
|
|
138
|
+
const serialized = JSON.stringify(result)
|
|
139
|
+
expect(serialized).not.toContain(SECRET_CIPHER)
|
|
140
|
+
expect(serialized).not.toContain('plain-secret-never-returned')
|
|
141
|
+
expect(serialized).not.toContain('secret prompt that must not leave the server')
|
|
142
|
+
expect(result).toMatchObject({
|
|
143
|
+
id: 'webhook-1',
|
|
144
|
+
path: '/api/harness/webhook/tok_abcdefghijklmnopqrstuvwxyz012345',
|
|
145
|
+
secretHint: 'ab12',
|
|
146
|
+
hasSecret: true,
|
|
147
|
+
})
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
it('同步而来的规则在工具结果中标记 hasSecret=false(引导重新录入)', async () => {
|
|
151
|
+
service.listWebhookRules.mockResolvedValue([{ ...ruleRow, secretCipher: null }])
|
|
152
|
+
const result = await tool('webhook_list').execute({}, context())
|
|
153
|
+
expect(result).toEqual({
|
|
154
|
+
webhooks: [expect.objectContaining({ id: 'webhook-1', hasSecret: false, secretHint: 'ab12' })],
|
|
155
|
+
})
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
it('list 只返回当前线程的规则投影', async () => {
|
|
159
|
+
service.listWebhookRules.mockResolvedValue([ruleRow])
|
|
160
|
+
const result = await tool('webhook_list').execute({}, context())
|
|
161
|
+
expect(service.listWebhookRules).toHaveBeenCalledWith({ userId: 'user-1', threadId: 'thread-1' })
|
|
162
|
+
expect(result).toEqual({ webhooks: [expect.objectContaining({ id: 'webhook-1' })] })
|
|
163
|
+
expect(JSON.stringify(result)).not.toContain(SECRET_CIPHER)
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it('view 跨线程规则按 not_found 拒绝', async () => {
|
|
167
|
+
service.getWebhookRule.mockResolvedValue({ ...ruleRow, threadId: 'other-thread' })
|
|
168
|
+
const result = await tool('webhook_view').execute({ id: 'webhook-1' }, context())
|
|
169
|
+
expect(result).toEqual({
|
|
170
|
+
code: 'webhook_not_found',
|
|
171
|
+
message: 'Webhook rule was not found for this thread',
|
|
172
|
+
})
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
it('service 抛出的领域错误映射为 {code,message} 而非 500', async () => {
|
|
176
|
+
service.getWebhookRule.mockResolvedValue(ruleRow)
|
|
177
|
+
service.editWebhookRule.mockRejectedValue(
|
|
178
|
+
new WebhookServiceError('webhook_conflict', 'Webhook rule revision conflict'),
|
|
179
|
+
)
|
|
180
|
+
const result = await tool('webhook_edit').execute({ id: 'webhook-1', name: 'renamed' }, context())
|
|
181
|
+
expect(result).toEqual({ code: 'webhook_conflict', message: 'Webhook rule revision conflict' })
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
it('delete 先校验归属再删除,返回删除前投影', async () => {
|
|
185
|
+
service.getWebhookRule.mockResolvedValue(ruleRow)
|
|
186
|
+
service.deleteWebhookRule.mockResolvedValue(undefined)
|
|
187
|
+
const result = await tool('webhook_delete').execute({ id: 'webhook-1' }, context())
|
|
188
|
+
expect(service.deleteWebhookRule).toHaveBeenCalledWith({
|
|
189
|
+
userId: 'user-1',
|
|
190
|
+
id: 'webhook-1',
|
|
191
|
+
revision: 0,
|
|
192
|
+
})
|
|
193
|
+
expect(result).toMatchObject({ id: 'webhook-1', status: 'active' })
|
|
194
|
+
})
|
|
195
|
+
})
|