alepha 0.20.1 → 0.20.2
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/dist/api/files/index.js +2 -1
- package/dist/api/files/index.js.map +1 -1
- package/dist/api/jobs/index.browser.js +64 -148
- package/dist/api/jobs/index.browser.js.map +1 -1
- package/dist/api/jobs/index.d.ts +371 -573
- package/dist/api/jobs/index.d.ts.map +1 -1
- package/dist/api/jobs/index.js +605 -1012
- package/dist/api/jobs/index.js.map +1 -1
- package/dist/api/notifications/index.d.ts +78 -17
- package/dist/api/notifications/index.d.ts.map +1 -1
- package/dist/api/notifications/index.js +90 -23
- package/dist/api/notifications/index.js.map +1 -1
- package/dist/api/payments/index.d.ts +2 -1
- package/dist/api/payments/index.d.ts.map +1 -1
- package/dist/api/payments/index.js +4 -2
- package/dist/api/payments/index.js.map +1 -1
- package/dist/api/users/index.d.ts +34 -31
- package/dist/api/users/index.d.ts.map +1 -1
- package/dist/api/users/index.js +13 -7
- package/dist/api/users/index.js.map +1 -1
- package/dist/api/verifications/index.js +2 -1
- package/dist/api/verifications/index.js.map +1 -1
- package/dist/cli/core/index.d.ts +8 -34
- package/dist/cli/core/index.d.ts.map +1 -1
- package/dist/cli/core/index.js +43 -232
- package/dist/cli/core/index.js.map +1 -1
- package/dist/cli/platform/index.d.ts +36 -11
- package/dist/cli/platform/index.d.ts.map +1 -1
- package/dist/cli/platform/index.js +93 -27
- package/dist/cli/platform/index.js.map +1 -1
- package/dist/command/index.d.ts +1 -1
- package/dist/core/index.browser.js +6 -0
- package/dist/core/index.browser.js.map +1 -1
- package/dist/core/index.d.ts +6 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +6 -0
- package/dist/core/index.js.map +1 -1
- package/dist/core/index.native.js +6 -0
- package/dist/core/index.native.js.map +1 -1
- package/dist/core/index.workerd.js +6 -0
- package/dist/core/index.workerd.js.map +1 -1
- package/dist/react/form/index.d.ts +60 -1
- package/dist/react/form/index.d.ts.map +1 -1
- package/dist/react/form/index.js +86 -1
- package/dist/react/form/index.js.map +1 -1
- package/dist/react/head/index.browser.js +16 -1
- package/dist/react/head/index.browser.js.map +1 -1
- package/dist/react/head/index.d.ts +6 -0
- package/dist/react/head/index.d.ts.map +1 -1
- package/dist/react/head/index.js +16 -1
- package/dist/react/head/index.js.map +1 -1
- package/dist/react/router/index.browser.js +0 -10
- package/dist/react/router/index.browser.js.map +1 -1
- package/dist/react/router/index.d.ts +35 -12
- package/dist/react/router/index.d.ts.map +1 -1
- package/dist/react/router/index.js +0 -10
- package/dist/react/router/index.js.map +1 -1
- package/dist/react/ui/index.d.ts +124 -0
- package/dist/react/ui/index.d.ts.map +1 -0
- package/dist/react/ui/index.js +206 -0
- package/dist/react/ui/index.js.map +1 -0
- package/dist/router/index.d.ts +13 -13
- package/dist/router/index.d.ts.map +1 -1
- package/dist/router/index.js +45 -32
- package/dist/router/index.js.map +1 -1
- package/dist/system/index.d.ts.map +1 -1
- package/dist/system/index.js +1 -0
- package/dist/system/index.js.map +1 -1
- package/dist/topic/core/index.js +1 -1
- package/dist/topic/core/index.js.map +1 -1
- package/package.json +6 -23
- package/src/api/files/jobs/FileJobs.ts +2 -1
- package/src/api/jobs/__tests__/$job.spec.ts +316 -2867
- package/src/api/jobs/controllers/AdminJobController.ts +29 -138
- package/src/api/jobs/entities/jobExecutionEntity.ts +27 -19
- package/src/api/jobs/index.browser.ts +5 -7
- package/src/api/jobs/index.ts +23 -51
- package/src/api/jobs/primitives/$job.ts +66 -58
- package/src/api/jobs/providers/JobProvider.ts +561 -566
- package/src/api/jobs/providers/JobQueueProvider.ts +18 -19
- package/src/api/jobs/schemas/jobConfigAtom.ts +20 -23
- package/src/api/jobs/schemas/jobExecutionQuerySchema.ts +3 -27
- package/src/api/jobs/schemas/jobExecutionResourceSchema.ts +5 -7
- package/src/api/jobs/schemas/jobRegistrationSchema.ts +7 -4
- package/src/api/jobs/schemas/triggerJobSchema.ts +0 -1
- package/src/api/jobs/services/JobService.ts +90 -483
- package/src/api/notifications/controllers/AdminNotificationController.ts +19 -12
- package/src/api/notifications/index.ts +7 -4
- package/src/api/notifications/jobs/NotificationJobs.ts +83 -12
- package/src/api/payments/services/PaymentService.ts +4 -2
- package/src/api/users/__tests__/UserJobs.spec.ts +10 -49
- package/src/api/users/audits/UserAudits.ts +3 -1
- package/src/api/users/buckets/UserBuckets.ts +2 -1
- package/src/api/users/index.ts +1 -4
- package/src/api/users/jobs/UserJobs.ts +5 -4
- package/src/api/verifications/jobs/VerificationJobs.ts +2 -1
- package/src/cli/core/__tests__/init.spec.ts +1 -1
- package/src/cli/core/commands/init.ts +0 -12
- package/src/cli/core/services/PackageManagerUtils.ts +2 -9
- package/src/cli/core/services/ProjectScaffolder.ts +17 -65
- package/src/cli/core/templates/agentMd.ts +2 -8
- package/src/cli/core/templates/apiIndexTs.ts +4 -18
- package/src/cli/core/templates/mainCss.ts +1 -36
- package/src/cli/core/templates/vitestConfigTs.ts +17 -0
- package/src/cli/core/templates/webAppRouterTs.ts +2 -85
- package/src/cli/platform/__tests__/CloudflareAdapter.spec.ts +22 -71
- package/src/cli/platform/adapters/CloudflareAdapter.ts +12 -11
- package/src/cli/platform/atoms/platformOptions.ts +9 -0
- package/src/cli/platform/schemas/cloudflare.ts +3 -2
- package/src/cli/platform/services/CloudflareApi.ts +164 -25
- package/src/cli/platform/services/WranglerApi.ts +0 -17
- package/src/core/Alepha.ts +9 -0
- package/src/react/form/index.ts +2 -0
- package/src/react/form/services/parseField.ts +163 -0
- package/src/react/form/services/prettyName.ts +19 -0
- package/src/react/head/providers/BrowserHeadProvider.ts +31 -10
- package/src/react/router/primitives/$page.ts +35 -12
- package/src/react/ui/atoms/uiAtom.ts +28 -0
- package/src/react/ui/components/ColorScheme.tsx +36 -0
- package/src/react/ui/hooks/useColorMode.ts +49 -0
- package/src/react/ui/hooks/useSidebarState.ts +26 -0
- package/src/react/ui/hooks/useTheme.ts +22 -0
- package/src/react/ui/index.ts +35 -0
- package/src/react/ui/services/UiPersistence.ts +41 -0
- package/src/router/TemplatedPathParser.ts +50 -51
- package/src/router/__tests__/RouterProvider.spec.ts +62 -0
- package/src/router/__tests__/TemplatedPathParser.spec.ts +18 -0
- package/src/router/providers/RouterProvider.ts +10 -5
- package/src/system/providers/NodeShellProvider.ts +1 -0
- package/src/topic/core/providers/TopicProvider.ts +1 -1
- package/dist/api/invitations/index.d.ts +0 -790
- package/dist/api/invitations/index.d.ts.map +0 -1
- package/dist/api/invitations/index.js +0 -662
- package/dist/api/invitations/index.js.map +0 -1
- package/dist/api/issues/index.d.ts +0 -810
- package/dist/api/issues/index.d.ts.map +0 -1
- package/dist/api/issues/index.js +0 -444
- package/dist/api/issues/index.js.map +0 -1
- package/dist/api/subscriptions/index.d.ts +0 -1692
- package/dist/api/subscriptions/index.d.ts.map +0 -1
- package/dist/api/subscriptions/index.js +0 -1867
- package/dist/api/subscriptions/index.js.map +0 -1
- package/dist/api/workflows/index.browser.js +0 -246
- package/dist/api/workflows/index.browser.js.map +0 -1
- package/dist/api/workflows/index.d.ts +0 -1618
- package/dist/api/workflows/index.d.ts.map +0 -1
- package/dist/api/workflows/index.js +0 -1495
- package/dist/api/workflows/index.js.map +0 -1
- package/src/api/invitations/__tests__/InvitationService.spec.ts +0 -439
- package/src/api/invitations/controllers/AdminInvitationController.ts +0 -86
- package/src/api/invitations/controllers/InvitationController.ts +0 -84
- package/src/api/invitations/entities/invitations.ts +0 -33
- package/src/api/invitations/index.ts +0 -58
- package/src/api/invitations/jobs/InvitationJobs.ts +0 -37
- package/src/api/invitations/providers/InvitationProvider.ts +0 -45
- package/src/api/invitations/schemas/createInvitationSchema.ts +0 -12
- package/src/api/invitations/schemas/invitationConfigAtom.ts +0 -20
- package/src/api/invitations/schemas/invitationQuerySchema.ts +0 -15
- package/src/api/invitations/schemas/invitationResourceSchema.ts +0 -6
- package/src/api/invitations/schemas/invitationWithResourceInfoSchema.ts +0 -22
- package/src/api/invitations/schemas/myInvitationsQuerySchema.ts +0 -10
- package/src/api/invitations/services/InvitationService.ts +0 -556
- package/src/api/issues/__tests__/IssueService.spec.ts +0 -263
- package/src/api/issues/controllers/AdminIssueController.ts +0 -149
- package/src/api/issues/controllers/IssueController.ts +0 -44
- package/src/api/issues/entities/issues.ts +0 -49
- package/src/api/issues/index.ts +0 -50
- package/src/api/issues/schemas/createIssueSchema.ts +0 -13
- package/src/api/issues/schemas/issueConfigAtom.ts +0 -13
- package/src/api/issues/schemas/issueQuerySchema.ts +0 -18
- package/src/api/issues/schemas/issueResourceSchema.ts +0 -6
- package/src/api/issues/schemas/myIssueQuerySchema.ts +0 -10
- package/src/api/issues/schemas/updateIssueSchema.ts +0 -13
- package/src/api/issues/services/IssueService.ts +0 -264
- package/src/api/jobs/__tests__/$job-middleware.spec.ts +0 -126
- package/src/api/jobs/__tests__/JobService.spec.ts +0 -31
- package/src/api/jobs/entities/jobExecutionLogEntity.ts +0 -13
- package/src/api/jobs/schemas/jobActivitySchema.ts +0 -15
- package/src/api/jobs/schemas/jobCronInfoSchema.ts +0 -22
- package/src/api/jobs/schemas/jobExecutionDetailResourceSchema.ts +0 -20
- package/src/api/jobs/schemas/jobFailureSchema.ts +0 -9
- package/src/api/jobs/schemas/jobQueueDepthSchema.ts +0 -14
- package/src/api/jobs/schemas/jobStatsSchema.ts +0 -14
- package/src/api/jobs/services/JobService-tests.ts +0 -157
- package/src/api/subscriptions/__tests__/BillingService.spec.ts +0 -218
- package/src/api/subscriptions/__tests__/SubscriptionService.spec.ts +0 -278
- package/src/api/subscriptions/controllers/AdminSubscriptionController.ts +0 -212
- package/src/api/subscriptions/controllers/SubscriptionController.ts +0 -189
- package/src/api/subscriptions/entities/subscriptionEvents.ts +0 -54
- package/src/api/subscriptions/entities/subscriptions.ts +0 -68
- package/src/api/subscriptions/index.ts +0 -133
- package/src/api/subscriptions/jobs/SubscriptionJobs.ts +0 -382
- package/src/api/subscriptions/middleware/$requireLimit.ts +0 -50
- package/src/api/subscriptions/middleware/$requirePlan.ts +0 -49
- package/src/api/subscriptions/notifications/SubscriptionNotifications.ts +0 -110
- package/src/api/subscriptions/schemas/cancelSubscriptionSchema.ts +0 -8
- package/src/api/subscriptions/schemas/changePlanSchema.ts +0 -9
- package/src/api/subscriptions/schemas/createSubscriptionSchema.ts +0 -11
- package/src/api/subscriptions/schemas/entitlementsSchema.ts +0 -21
- package/src/api/subscriptions/schemas/mrrSchema.ts +0 -13
- package/src/api/subscriptions/schemas/planDefinitionSchema.ts +0 -71
- package/src/api/subscriptions/schemas/planResourceSchema.ts +0 -25
- package/src/api/subscriptions/schemas/subscriptionEventResourceSchema.ts +0 -8
- package/src/api/subscriptions/schemas/subscriptionQuerySchema.ts +0 -19
- package/src/api/subscriptions/schemas/subscriptionResourceSchema.ts +0 -6
- package/src/api/subscriptions/schemas/subscriptionSettingsSchema.ts +0 -32
- package/src/api/subscriptions/schemas/subscriptionStatsSchema.ts +0 -23
- package/src/api/subscriptions/services/BillingService.ts +0 -437
- package/src/api/subscriptions/services/SubscriptionConfig.ts +0 -56
- package/src/api/subscriptions/services/SubscriptionService.ts +0 -867
- package/src/api/subscriptions/services/UsageService.ts +0 -118
- package/src/api/workflows/__tests__/$workflow.spec.ts +0 -616
- package/src/api/workflows/controllers/AdminWorkflowController.ts +0 -191
- package/src/api/workflows/entities/workflowExecutions.ts +0 -74
- package/src/api/workflows/entities/workflowStepExecutions.ts +0 -74
- package/src/api/workflows/entities/workflowStepLogs.ts +0 -13
- package/src/api/workflows/index.browser.ts +0 -22
- package/src/api/workflows/index.ts +0 -115
- package/src/api/workflows/jobs/WorkflowJobs.ts +0 -77
- package/src/api/workflows/primitives/$workflow.ts +0 -202
- package/src/api/workflows/providers/WorkflowProvider.ts +0 -1284
- package/src/api/workflows/schemas/workflowActivitySchema.ts +0 -15
- package/src/api/workflows/schemas/workflowConfigAtom.ts +0 -51
- package/src/api/workflows/schemas/workflowExecutionDetailSchema.ts +0 -18
- package/src/api/workflows/schemas/workflowExecutionQuerySchema.ts +0 -26
- package/src/api/workflows/schemas/workflowExecutionResourceSchema.ts +0 -30
- package/src/api/workflows/schemas/workflowRegistrationSchema.ts +0 -26
- package/src/api/workflows/schemas/workflowStatsSchema.ts +0 -16
- package/src/api/workflows/schemas/workflowStepExecutionResourceSchema.ts +0 -15
- package/src/api/workflows/services/WorkflowService.ts +0 -382
- package/src/cli/core/templates/apiAppSecurityTs.ts +0 -43
- package/src/cli/core/templates/webAdminDashboardTsx.ts +0 -17
|
@@ -1,616 +0,0 @@
|
|
|
1
|
-
import { Alepha, t } from "alepha";
|
|
2
|
-
import { $repository } from "alepha/orm";
|
|
3
|
-
import { AlephaOrmPostgres } from "alepha/orm/postgres";
|
|
4
|
-
import { describe, expect, it, vi } from "vitest";
|
|
5
|
-
import {
|
|
6
|
-
$workflow,
|
|
7
|
-
AlephaApiWorkflows,
|
|
8
|
-
workflowExecutions,
|
|
9
|
-
workflowStepExecutions,
|
|
10
|
-
} from "../index.ts";
|
|
11
|
-
|
|
12
|
-
// -----------------------------------------------------------------------------------------------------------------
|
|
13
|
-
|
|
14
|
-
describe("$workflow", () => {
|
|
15
|
-
// ----- Basic functionality -----
|
|
16
|
-
|
|
17
|
-
describe("basic functionality", () => {
|
|
18
|
-
it("should start and complete a single-step workflow", async () => {
|
|
19
|
-
const handler = vi.fn();
|
|
20
|
-
|
|
21
|
-
class App {
|
|
22
|
-
repo = $repository(workflowExecutions);
|
|
23
|
-
stepRepo = $repository(workflowStepExecutions);
|
|
24
|
-
myWorkflow = $workflow({
|
|
25
|
-
schema: t.object({ orderId: t.uuid() }),
|
|
26
|
-
steps: [
|
|
27
|
-
{
|
|
28
|
-
name: "processOrder",
|
|
29
|
-
handler: async ({ payload }) => {
|
|
30
|
-
handler(payload);
|
|
31
|
-
return { processed: true };
|
|
32
|
-
},
|
|
33
|
-
},
|
|
34
|
-
],
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const alepha = Alepha.create().with(AlephaOrmPostgres);
|
|
39
|
-
alepha.with(AlephaApiWorkflows);
|
|
40
|
-
alepha.with(App);
|
|
41
|
-
await alepha.start();
|
|
42
|
-
|
|
43
|
-
const app = alepha.inject(App);
|
|
44
|
-
const orderId = crypto.randomUUID();
|
|
45
|
-
const executionId = await app.myWorkflow.start({ orderId });
|
|
46
|
-
|
|
47
|
-
await vi.waitFor(async () => {
|
|
48
|
-
const exec = await app.repo.findById(executionId);
|
|
49
|
-
expect(exec?.status).toBe("completed");
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
expect(handler).toHaveBeenCalledTimes(1);
|
|
53
|
-
expect(handler).toHaveBeenCalledWith(
|
|
54
|
-
expect.objectContaining({ orderId }),
|
|
55
|
-
);
|
|
56
|
-
|
|
57
|
-
// Verify step is completed with result
|
|
58
|
-
const steps = await app.stepRepo.findMany({
|
|
59
|
-
where: { workflowExecutionId: { eq: executionId } },
|
|
60
|
-
});
|
|
61
|
-
expect(steps).toHaveLength(1);
|
|
62
|
-
expect(steps[0].status).toBe("completed");
|
|
63
|
-
expect(steps[0].result).toEqual({ processed: true });
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it("should execute multi-step workflow in order", async () => {
|
|
67
|
-
const order: string[] = [];
|
|
68
|
-
|
|
69
|
-
class App {
|
|
70
|
-
repo = $repository(workflowExecutions);
|
|
71
|
-
myWorkflow = $workflow({
|
|
72
|
-
schema: t.object({ id: t.text() }),
|
|
73
|
-
steps: [
|
|
74
|
-
{
|
|
75
|
-
name: "step1",
|
|
76
|
-
handler: async () => {
|
|
77
|
-
order.push("step1");
|
|
78
|
-
return { a: 1 };
|
|
79
|
-
},
|
|
80
|
-
},
|
|
81
|
-
{
|
|
82
|
-
name: "step2",
|
|
83
|
-
handler: async ({ results }) => {
|
|
84
|
-
order.push("step2");
|
|
85
|
-
return { b: 2, fromStep1: results.step1 };
|
|
86
|
-
},
|
|
87
|
-
},
|
|
88
|
-
{
|
|
89
|
-
name: "step3",
|
|
90
|
-
handler: async ({ results }) => {
|
|
91
|
-
order.push("step3");
|
|
92
|
-
return { c: 3, fromStep2: results.step2 };
|
|
93
|
-
},
|
|
94
|
-
},
|
|
95
|
-
],
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const alepha = Alepha.create().with(AlephaOrmPostgres);
|
|
100
|
-
alepha.with(AlephaApiWorkflows);
|
|
101
|
-
alepha.with(App);
|
|
102
|
-
await alepha.start();
|
|
103
|
-
|
|
104
|
-
const app = alepha.inject(App);
|
|
105
|
-
const executionId = await app.myWorkflow.start({ id: "test" });
|
|
106
|
-
|
|
107
|
-
await vi.waitFor(async () => {
|
|
108
|
-
const exec = await app.repo.findById(executionId);
|
|
109
|
-
expect(exec?.status).toBe("completed");
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
expect(order).toEqual(["step1", "step2", "step3"]);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it("should pass accumulated results between steps", async () => {
|
|
116
|
-
let step2Results: Record<string, unknown> = {};
|
|
117
|
-
|
|
118
|
-
class App {
|
|
119
|
-
repo = $repository(workflowExecutions);
|
|
120
|
-
myWorkflow = $workflow({
|
|
121
|
-
schema: t.object({ value: t.text() }),
|
|
122
|
-
steps: [
|
|
123
|
-
{
|
|
124
|
-
name: "first",
|
|
125
|
-
handler: async () => ({ key: "from-first" }),
|
|
126
|
-
},
|
|
127
|
-
{
|
|
128
|
-
name: "second",
|
|
129
|
-
handler: async ({ results }) => {
|
|
130
|
-
step2Results = results;
|
|
131
|
-
},
|
|
132
|
-
},
|
|
133
|
-
],
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const alepha = Alepha.create().with(AlephaOrmPostgres);
|
|
138
|
-
alepha.with(AlephaApiWorkflows);
|
|
139
|
-
alepha.with(App);
|
|
140
|
-
await alepha.start();
|
|
141
|
-
|
|
142
|
-
const app = alepha.inject(App);
|
|
143
|
-
const executionId = await app.myWorkflow.start({ value: "test" });
|
|
144
|
-
|
|
145
|
-
await vi.waitFor(async () => {
|
|
146
|
-
const exec = await app.repo.findById(executionId);
|
|
147
|
-
expect(exec?.status).toBe("completed");
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
expect(step2Results.first).toEqual({ key: "from-first" });
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
it("should use ClassName.propertyKey as workflow name", async () => {
|
|
154
|
-
class OrderService {
|
|
155
|
-
processOrder = $workflow({
|
|
156
|
-
schema: t.object({ id: t.text() }),
|
|
157
|
-
steps: [{ name: "step1", handler: async () => {} }],
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const alepha = Alepha.create().with(AlephaOrmPostgres);
|
|
162
|
-
alepha.with(AlephaApiWorkflows);
|
|
163
|
-
alepha.with(OrderService);
|
|
164
|
-
await alepha.start();
|
|
165
|
-
|
|
166
|
-
const app = alepha.inject(OrderService);
|
|
167
|
-
expect(app.processOrder.name).toBe("OrderService.processOrder");
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
// ----- Compensation (saga pattern) -----
|
|
172
|
-
|
|
173
|
-
describe("compensation", () => {
|
|
174
|
-
it("should compensate completed steps in reverse order on failure", async () => {
|
|
175
|
-
const compensations: string[] = [];
|
|
176
|
-
|
|
177
|
-
class App {
|
|
178
|
-
repo = $repository(workflowExecutions);
|
|
179
|
-
myWorkflow = $workflow({
|
|
180
|
-
schema: t.object({ id: t.text() }),
|
|
181
|
-
onError: "compensate",
|
|
182
|
-
steps: [
|
|
183
|
-
{
|
|
184
|
-
name: "step1",
|
|
185
|
-
handler: async () => ({ id: "r1" }),
|
|
186
|
-
compensate: async () => {
|
|
187
|
-
compensations.push("step1");
|
|
188
|
-
},
|
|
189
|
-
},
|
|
190
|
-
{
|
|
191
|
-
name: "step2",
|
|
192
|
-
handler: async () => ({ id: "r2" }),
|
|
193
|
-
compensate: async () => {
|
|
194
|
-
compensations.push("step2");
|
|
195
|
-
},
|
|
196
|
-
},
|
|
197
|
-
{
|
|
198
|
-
name: "step3",
|
|
199
|
-
handler: async () => {
|
|
200
|
-
throw new Error("step3 failed");
|
|
201
|
-
},
|
|
202
|
-
compensate: async () => {
|
|
203
|
-
compensations.push("step3");
|
|
204
|
-
},
|
|
205
|
-
},
|
|
206
|
-
],
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const alepha = Alepha.create().with(AlephaOrmPostgres);
|
|
211
|
-
alepha.with(AlephaApiWorkflows);
|
|
212
|
-
alepha.with(App);
|
|
213
|
-
await alepha.start();
|
|
214
|
-
|
|
215
|
-
const app = alepha.inject(App);
|
|
216
|
-
const executionId = await app.myWorkflow.start({ id: "test" });
|
|
217
|
-
|
|
218
|
-
await vi.waitFor(async () => {
|
|
219
|
-
const exec = await app.repo.findById(executionId);
|
|
220
|
-
expect(exec?.status).toBe("compensated");
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
// step3 never completed so no compensation for it
|
|
224
|
-
// step2 and step1 compensated in reverse order
|
|
225
|
-
expect(compensations).toEqual(["step2", "step1"]);
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
it("should mark as failed when onError is fail", async () => {
|
|
229
|
-
class App {
|
|
230
|
-
repo = $repository(workflowExecutions);
|
|
231
|
-
myWorkflow = $workflow({
|
|
232
|
-
schema: t.object({ id: t.text() }),
|
|
233
|
-
onError: "fail",
|
|
234
|
-
steps: [
|
|
235
|
-
{
|
|
236
|
-
name: "step1",
|
|
237
|
-
handler: async () => ({ ok: true }),
|
|
238
|
-
},
|
|
239
|
-
{
|
|
240
|
-
name: "step2",
|
|
241
|
-
handler: async () => {
|
|
242
|
-
throw new Error("boom");
|
|
243
|
-
},
|
|
244
|
-
},
|
|
245
|
-
],
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
const alepha = Alepha.create().with(AlephaOrmPostgres);
|
|
250
|
-
alepha.with(AlephaApiWorkflows);
|
|
251
|
-
alepha.with(App);
|
|
252
|
-
await alepha.start();
|
|
253
|
-
|
|
254
|
-
const app = alepha.inject(App);
|
|
255
|
-
const executionId = await app.myWorkflow.start({ id: "test" });
|
|
256
|
-
|
|
257
|
-
await vi.waitFor(async () => {
|
|
258
|
-
const exec = await app.repo.findById(executionId);
|
|
259
|
-
expect(exec?.status).toBe("failed");
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
const exec = await app.repo.findById(executionId);
|
|
263
|
-
expect(exec?.error).toBe("boom");
|
|
264
|
-
expect(exec?.errorStep).toBe("step2");
|
|
265
|
-
});
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
// ----- Retry -----
|
|
269
|
-
|
|
270
|
-
describe("retry", () => {
|
|
271
|
-
it("should retry a step on failure with retries configured", async () => {
|
|
272
|
-
let callCount = 0;
|
|
273
|
-
|
|
274
|
-
class App {
|
|
275
|
-
repo = $repository(workflowExecutions);
|
|
276
|
-
stepRepo = $repository(workflowStepExecutions);
|
|
277
|
-
myWorkflow = $workflow({
|
|
278
|
-
schema: t.object({ id: t.text() }),
|
|
279
|
-
steps: [
|
|
280
|
-
{
|
|
281
|
-
name: "flaky",
|
|
282
|
-
retry: { retries: 2, backoff: [10, "millisecond"] },
|
|
283
|
-
handler: async () => {
|
|
284
|
-
callCount++;
|
|
285
|
-
if (callCount < 3) throw new Error(`fail #${callCount}`);
|
|
286
|
-
return { ok: true };
|
|
287
|
-
},
|
|
288
|
-
},
|
|
289
|
-
],
|
|
290
|
-
});
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
const alepha = Alepha.create().with(AlephaOrmPostgres);
|
|
294
|
-
alepha.with(AlephaApiWorkflows);
|
|
295
|
-
alepha.with(App);
|
|
296
|
-
await alepha.start();
|
|
297
|
-
|
|
298
|
-
const app = alepha.inject(App);
|
|
299
|
-
const executionId = await app.myWorkflow.start({ id: "test" });
|
|
300
|
-
|
|
301
|
-
await vi.waitFor(
|
|
302
|
-
async () => {
|
|
303
|
-
const exec = await app.repo.findById(executionId);
|
|
304
|
-
expect(exec?.status).toBe("completed");
|
|
305
|
-
},
|
|
306
|
-
{ timeout: 10_000 },
|
|
307
|
-
);
|
|
308
|
-
|
|
309
|
-
expect(callCount).toBe(3);
|
|
310
|
-
});
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
// ----- Conditional steps -----
|
|
314
|
-
|
|
315
|
-
describe("conditional steps", () => {
|
|
316
|
-
it("should skip steps when condition returns false", async () => {
|
|
317
|
-
const executed: string[] = [];
|
|
318
|
-
|
|
319
|
-
class App {
|
|
320
|
-
repo = $repository(workflowExecutions);
|
|
321
|
-
stepRepo = $repository(workflowStepExecutions);
|
|
322
|
-
myWorkflow = $workflow({
|
|
323
|
-
schema: t.object({ skipMiddle: t.boolean() }),
|
|
324
|
-
steps: [
|
|
325
|
-
{
|
|
326
|
-
name: "step1",
|
|
327
|
-
handler: async () => {
|
|
328
|
-
executed.push("step1");
|
|
329
|
-
return { value: 1 };
|
|
330
|
-
},
|
|
331
|
-
},
|
|
332
|
-
{
|
|
333
|
-
name: "step2",
|
|
334
|
-
when: ({ payload }) => !payload.skipMiddle,
|
|
335
|
-
handler: async () => {
|
|
336
|
-
executed.push("step2");
|
|
337
|
-
return { value: 2 };
|
|
338
|
-
},
|
|
339
|
-
},
|
|
340
|
-
{
|
|
341
|
-
name: "step3",
|
|
342
|
-
handler: async () => {
|
|
343
|
-
executed.push("step3");
|
|
344
|
-
return { value: 3 };
|
|
345
|
-
},
|
|
346
|
-
},
|
|
347
|
-
],
|
|
348
|
-
});
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
const alepha = Alepha.create().with(AlephaOrmPostgres);
|
|
352
|
-
alepha.with(AlephaApiWorkflows);
|
|
353
|
-
alepha.with(App);
|
|
354
|
-
await alepha.start();
|
|
355
|
-
|
|
356
|
-
const app = alepha.inject(App);
|
|
357
|
-
const executionId = await app.myWorkflow.start({ skipMiddle: true });
|
|
358
|
-
|
|
359
|
-
await vi.waitFor(async () => {
|
|
360
|
-
const exec = await app.repo.findById(executionId);
|
|
361
|
-
expect(exec?.status).toBe("completed");
|
|
362
|
-
});
|
|
363
|
-
|
|
364
|
-
expect(executed).toEqual(["step1", "step3"]);
|
|
365
|
-
|
|
366
|
-
// Verify step2 was skipped
|
|
367
|
-
const steps = await app.stepRepo.findMany({
|
|
368
|
-
where: { workflowExecutionId: { eq: executionId } },
|
|
369
|
-
orderBy: { column: "stepIndex", direction: "asc" },
|
|
370
|
-
});
|
|
371
|
-
expect(steps[1].status).toBe("skipped");
|
|
372
|
-
});
|
|
373
|
-
});
|
|
374
|
-
|
|
375
|
-
// ----- Cancel -----
|
|
376
|
-
|
|
377
|
-
describe("cancel", () => {
|
|
378
|
-
it("should cancel a pending workflow", async () => {
|
|
379
|
-
class App {
|
|
380
|
-
repo = $repository(workflowExecutions);
|
|
381
|
-
stepRepo = $repository(workflowStepExecutions);
|
|
382
|
-
myWorkflow = $workflow({
|
|
383
|
-
schema: t.object({ id: t.text() }),
|
|
384
|
-
steps: [
|
|
385
|
-
{
|
|
386
|
-
name: "long",
|
|
387
|
-
handler: async ({ signal }) => {
|
|
388
|
-
await new Promise<void>((resolve) => {
|
|
389
|
-
const check = () => {
|
|
390
|
-
if (signal.aborted) resolve();
|
|
391
|
-
else setTimeout(check, 10);
|
|
392
|
-
};
|
|
393
|
-
check();
|
|
394
|
-
});
|
|
395
|
-
},
|
|
396
|
-
},
|
|
397
|
-
],
|
|
398
|
-
});
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
const alepha = Alepha.create().with(AlephaOrmPostgres);
|
|
402
|
-
alepha.with(AlephaApiWorkflows);
|
|
403
|
-
alepha.with(App);
|
|
404
|
-
await alepha.start();
|
|
405
|
-
|
|
406
|
-
const app = alepha.inject(App);
|
|
407
|
-
const executionId = await app.myWorkflow.start({ id: "test" });
|
|
408
|
-
|
|
409
|
-
// Wait for it to be running
|
|
410
|
-
await vi.waitFor(async () => {
|
|
411
|
-
const steps = await app.stepRepo.findMany({
|
|
412
|
-
where: {
|
|
413
|
-
workflowExecutionId: { eq: executionId },
|
|
414
|
-
status: { eq: "running" },
|
|
415
|
-
},
|
|
416
|
-
});
|
|
417
|
-
expect(steps).toHaveLength(1);
|
|
418
|
-
});
|
|
419
|
-
|
|
420
|
-
await app.myWorkflow.cancel(executionId);
|
|
421
|
-
|
|
422
|
-
await vi.waitFor(async () => {
|
|
423
|
-
const exec = await app.repo.findById(executionId);
|
|
424
|
-
expect(exec?.status).toBe("cancelled");
|
|
425
|
-
});
|
|
426
|
-
});
|
|
427
|
-
});
|
|
428
|
-
|
|
429
|
-
// ----- Retry (admin action) -----
|
|
430
|
-
|
|
431
|
-
describe("admin retry", () => {
|
|
432
|
-
it("should retry a failed workflow from the failed step", async () => {
|
|
433
|
-
let callCount = 0;
|
|
434
|
-
|
|
435
|
-
class App {
|
|
436
|
-
repo = $repository(workflowExecutions);
|
|
437
|
-
myWorkflow = $workflow({
|
|
438
|
-
schema: t.object({ id: t.text() }),
|
|
439
|
-
onError: "fail",
|
|
440
|
-
steps: [
|
|
441
|
-
{
|
|
442
|
-
name: "step1",
|
|
443
|
-
handler: async () => ({ ok: true }),
|
|
444
|
-
},
|
|
445
|
-
{
|
|
446
|
-
name: "step2",
|
|
447
|
-
handler: async () => {
|
|
448
|
-
callCount++;
|
|
449
|
-
if (callCount === 1) throw new Error("transient");
|
|
450
|
-
return { ok: true };
|
|
451
|
-
},
|
|
452
|
-
},
|
|
453
|
-
],
|
|
454
|
-
});
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
const alepha = Alepha.create().with(AlephaOrmPostgres);
|
|
458
|
-
alepha.with(AlephaApiWorkflows);
|
|
459
|
-
alepha.with(App);
|
|
460
|
-
await alepha.start();
|
|
461
|
-
|
|
462
|
-
const app = alepha.inject(App);
|
|
463
|
-
const executionId = await app.myWorkflow.start({ id: "test" });
|
|
464
|
-
|
|
465
|
-
await vi.waitFor(async () => {
|
|
466
|
-
const exec = await app.repo.findById(executionId);
|
|
467
|
-
expect(exec?.status).toBe("failed");
|
|
468
|
-
});
|
|
469
|
-
|
|
470
|
-
// Retry from the failed step
|
|
471
|
-
await app.myWorkflow.retry(executionId);
|
|
472
|
-
|
|
473
|
-
await vi.waitFor(async () => {
|
|
474
|
-
const exec = await app.repo.findById(executionId);
|
|
475
|
-
expect(exec?.status).toBe("completed");
|
|
476
|
-
});
|
|
477
|
-
|
|
478
|
-
expect(callCount).toBe(2);
|
|
479
|
-
});
|
|
480
|
-
});
|
|
481
|
-
|
|
482
|
-
// ----- Restart -----
|
|
483
|
-
|
|
484
|
-
describe("restart", () => {
|
|
485
|
-
it("should create a new execution from the same payload", async () => {
|
|
486
|
-
let callCount = 0;
|
|
487
|
-
|
|
488
|
-
class App {
|
|
489
|
-
repo = $repository(workflowExecutions);
|
|
490
|
-
myWorkflow = $workflow({
|
|
491
|
-
schema: t.object({ id: t.text() }),
|
|
492
|
-
onError: "fail",
|
|
493
|
-
steps: [
|
|
494
|
-
{
|
|
495
|
-
name: "step1",
|
|
496
|
-
handler: async () => {
|
|
497
|
-
callCount++;
|
|
498
|
-
if (callCount === 1) throw new Error("fail first time");
|
|
499
|
-
return { ok: true };
|
|
500
|
-
},
|
|
501
|
-
},
|
|
502
|
-
],
|
|
503
|
-
});
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
const alepha = Alepha.create().with(AlephaOrmPostgres);
|
|
507
|
-
alepha.with(AlephaApiWorkflows);
|
|
508
|
-
alepha.with(App);
|
|
509
|
-
await alepha.start();
|
|
510
|
-
|
|
511
|
-
const app = alepha.inject(App);
|
|
512
|
-
const executionId = await app.myWorkflow.start({ id: "test" });
|
|
513
|
-
|
|
514
|
-
await vi.waitFor(async () => {
|
|
515
|
-
const exec = await app.repo.findById(executionId);
|
|
516
|
-
expect(exec?.status).toBe("failed");
|
|
517
|
-
});
|
|
518
|
-
|
|
519
|
-
const newId = await app.myWorkflow.restart(executionId);
|
|
520
|
-
expect(newId).not.toBe(executionId);
|
|
521
|
-
|
|
522
|
-
await vi.waitFor(async () => {
|
|
523
|
-
const exec = await app.repo.findById(newId);
|
|
524
|
-
expect(exec?.status).toBe("completed");
|
|
525
|
-
});
|
|
526
|
-
|
|
527
|
-
expect(callCount).toBe(2);
|
|
528
|
-
});
|
|
529
|
-
});
|
|
530
|
-
|
|
531
|
-
// ----- Events -----
|
|
532
|
-
|
|
533
|
-
describe("events", () => {
|
|
534
|
-
it("should emit workflow lifecycle events", async () => {
|
|
535
|
-
const events: string[] = [];
|
|
536
|
-
|
|
537
|
-
class App {
|
|
538
|
-
repo = $repository(workflowExecutions);
|
|
539
|
-
myWorkflow = $workflow({
|
|
540
|
-
schema: t.object({ id: t.text() }),
|
|
541
|
-
steps: [{ name: "step1", handler: async () => ({ ok: true }) }],
|
|
542
|
-
});
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
const alepha = Alepha.create().with(AlephaOrmPostgres);
|
|
546
|
-
alepha.with(AlephaApiWorkflows);
|
|
547
|
-
alepha.with(App);
|
|
548
|
-
alepha.events.on("workflow:started", () => {
|
|
549
|
-
events.push("started");
|
|
550
|
-
});
|
|
551
|
-
alepha.events.on("workflow:step:begin", () => {
|
|
552
|
-
events.push("step:begin");
|
|
553
|
-
});
|
|
554
|
-
alepha.events.on("workflow:step:completed", () => {
|
|
555
|
-
events.push("step:completed");
|
|
556
|
-
});
|
|
557
|
-
alepha.events.on("workflow:completed", () => {
|
|
558
|
-
events.push("completed");
|
|
559
|
-
});
|
|
560
|
-
await alepha.start();
|
|
561
|
-
|
|
562
|
-
const app = alepha.inject(App);
|
|
563
|
-
const executionId = await app.myWorkflow.start({ id: "test" });
|
|
564
|
-
|
|
565
|
-
await vi.waitFor(async () => {
|
|
566
|
-
const exec = await app.repo.findById(executionId);
|
|
567
|
-
expect(exec?.status).toBe("completed");
|
|
568
|
-
});
|
|
569
|
-
|
|
570
|
-
expect(events).toContain("started");
|
|
571
|
-
expect(events).toContain("step:begin");
|
|
572
|
-
expect(events).toContain("step:completed");
|
|
573
|
-
expect(events).toContain("completed");
|
|
574
|
-
});
|
|
575
|
-
});
|
|
576
|
-
|
|
577
|
-
// ----- Deduplication -----
|
|
578
|
-
|
|
579
|
-
describe("deduplication", () => {
|
|
580
|
-
it("should return existing execution for same key", async () => {
|
|
581
|
-
class App {
|
|
582
|
-
repo = $repository(workflowExecutions);
|
|
583
|
-
myWorkflow = $workflow({
|
|
584
|
-
schema: t.object({ id: t.text() }),
|
|
585
|
-
steps: [{ name: "step1", handler: async () => ({ ok: true }) }],
|
|
586
|
-
});
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
const alepha = Alepha.create().with(AlephaOrmPostgres);
|
|
590
|
-
alepha.with(AlephaApiWorkflows);
|
|
591
|
-
alepha.with(App);
|
|
592
|
-
await alepha.start();
|
|
593
|
-
|
|
594
|
-
const app = alepha.inject(App);
|
|
595
|
-
|
|
596
|
-
// First start with key — creates a new execution
|
|
597
|
-
const id1 = await app.myWorkflow.start(
|
|
598
|
-
{ id: "test" },
|
|
599
|
-
{ key: "dedup-key" },
|
|
600
|
-
);
|
|
601
|
-
|
|
602
|
-
// Wait for completion (key is cleared on completion)
|
|
603
|
-
await vi.waitFor(async () => {
|
|
604
|
-
const exec = await app.repo.findById(id1);
|
|
605
|
-
expect(exec?.status).toBe("completed");
|
|
606
|
-
});
|
|
607
|
-
|
|
608
|
-
// Second start with same key after completion — creates NEW execution
|
|
609
|
-
const id2 = await app.myWorkflow.start(
|
|
610
|
-
{ id: "test" },
|
|
611
|
-
{ key: "dedup-key" },
|
|
612
|
-
);
|
|
613
|
-
expect(id2).not.toBe(id1);
|
|
614
|
-
});
|
|
615
|
-
});
|
|
616
|
-
});
|