|
1 | 1 | const assert = require('node:assert/strict'); |
2 | 2 | const sinon = require('sinon'); |
3 | 3 | const domainEvents = require('@tryghost/domain-events'); |
4 | | -const models = require('../../../../core/server/models'); |
5 | 4 | const automationsController = require('../../../../core/server/api/endpoints/automations'); |
6 | 5 | const StartAutomationsPollEvent = require('../../../../core/server/services/automations/events/start-automations-poll-event'); |
7 | 6 |
|
8 | 7 | describe('Automations controller', function () { |
9 | | - before(function () { |
10 | | - models.init(); |
11 | | - }); |
12 | | - |
13 | | - function createMockAutomation(id, name, slug, status) { |
14 | | - return { |
15 | | - get(key) { |
16 | | - switch (key) { |
17 | | - case 'id': |
18 | | - return id; |
19 | | - case 'name': |
20 | | - return name; |
21 | | - case 'slug': |
22 | | - return slug; |
23 | | - case 'status': |
24 | | - return status; |
25 | | - default: |
26 | | - throw new Error(`Unexpected field: ${key}`); |
27 | | - } |
28 | | - } |
29 | | - }; |
30 | | - } |
| 8 | + // Other endpoints are tested in E2E tests. |
31 | 9 |
|
32 | 10 | let dispatchStub; |
33 | 11 |
|
34 | 12 | beforeEach(function () { |
35 | | - sinon.stub(models.WelcomeEmailAutomation, 'findAll').resolves([ |
36 | | - createMockAutomation('automation-id-1', 'Welcome Email (Free)', 'member-welcome-email-free', 'active'), |
37 | | - createMockAutomation('automation-id-2', 'Welcome Email (Premium)', 'member-welcome-email-premium', 'inactive') |
38 | | - ]); |
39 | | - |
40 | 13 | dispatchStub = sinon.stub(domainEvents, 'dispatch'); |
41 | 14 | }); |
42 | 15 |
|
43 | 16 | afterEach(function () { |
44 | 17 | sinon.restore(); |
45 | 18 | }); |
46 | 19 |
|
47 | | - describe('browse', function () { |
48 | | - it('returns only id, name, slug, and status fields', async function () { |
49 | | - const result = await automationsController.browse.query({}); |
50 | | - |
51 | | - assert.deepEqual(result.data, [{ |
52 | | - id: 'automation-id-1', |
53 | | - name: 'Welcome Email (Free)', |
54 | | - slug: 'member-welcome-email-free', |
55 | | - status: 'active' |
56 | | - }, { |
57 | | - id: 'automation-id-2', |
58 | | - name: 'Welcome Email (Premium)', |
59 | | - slug: 'member-welcome-email-premium', |
60 | | - status: 'inactive' |
61 | | - }]); |
62 | | - }); |
63 | | - }); |
64 | | - |
65 | | - describe('read', function () { |
66 | | - it('returns a placeholder automation for the requested id', function () { |
67 | | - const result = automationsController.read.query({ |
68 | | - data: { |
69 | | - id: '67f3f3f3f3f3f3f3f3f3f3f3' |
70 | | - } |
71 | | - }); |
72 | | - |
73 | | - assert.deepEqual(result, { |
74 | | - id: '67f3f3f3f3f3f3f3f3f3f3f3', |
75 | | - slug: 'member-welcome-email-free', |
76 | | - name: 'Welcome email', |
77 | | - status: 'active', |
78 | | - created_at: '2026-05-05T00:00:00.000Z', |
79 | | - updated_at: '2026-05-05T00:00:00.000Z', |
80 | | - actions: [{ |
81 | | - id: '67f3f3f3f3f3f3f3f3f3f3f4', |
82 | | - type: 'delay', |
83 | | - data: { |
84 | | - delay_hours: 24 |
85 | | - } |
86 | | - }, { |
87 | | - id: '67f3f3f3f3f3f3f3f3f3f3f5', |
88 | | - type: 'send email', |
89 | | - data: { |
90 | | - email_subject: 'Welcome!', |
91 | | - email_lexical: '{"root":{"children":[]}}', |
92 | | - email_sender_name: null, |
93 | | - email_sender_email: null, |
94 | | - email_sender_reply_to: null, |
95 | | - email_design_setting_id: '680000000000000000000001' |
96 | | - } |
97 | | - }], |
98 | | - edges: [{ |
99 | | - source_action_id: '67f3f3f3f3f3f3f3f3f3f3f4', |
100 | | - target_action_id: '67f3f3f3f3f3f3f3f3f3f3f5' |
101 | | - }] |
102 | | - }); |
103 | | - }); |
104 | | - }); |
105 | | - |
106 | 20 | describe('poll', function () { |
107 | 21 | it('dispatches a StartAutomationsPollEvent', function () { |
108 | 22 | const result = automationsController.poll.query({}); |
|
0 commit comments