@rankcli/agent-runtime 0.0.1
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/README.md +242 -0
- package/dist/analyzer-2CSWIQGD.mjs +6 -0
- package/dist/chunk-YNZYHEYM.mjs +774 -0
- package/dist/index.d.mts +4012 -0
- package/dist/index.d.ts +4012 -0
- package/dist/index.js +29672 -0
- package/dist/index.mjs +28602 -0
- package/package.json +53 -0
- package/scripts/build-deno.ts +134 -0
- package/src/audit/ai/analyzer.ts +347 -0
- package/src/audit/ai/index.ts +29 -0
- package/src/audit/ai/prompts/content-analysis.ts +271 -0
- package/src/audit/ai/types.ts +179 -0
- package/src/audit/checks/additional-checks.ts +439 -0
- package/src/audit/checks/ai-citation-worthiness.ts +399 -0
- package/src/audit/checks/ai-content-structure.ts +325 -0
- package/src/audit/checks/ai-readiness.ts +339 -0
- package/src/audit/checks/anchor-text.ts +179 -0
- package/src/audit/checks/answer-conciseness.ts +322 -0
- package/src/audit/checks/asset-minification.ts +270 -0
- package/src/audit/checks/bing-optimization.ts +206 -0
- package/src/audit/checks/brand-mention-optimization.ts +349 -0
- package/src/audit/checks/caching-headers.ts +305 -0
- package/src/audit/checks/canonical-advanced.ts +150 -0
- package/src/audit/checks/canonical-domain.ts +196 -0
- package/src/audit/checks/citation-quality.ts +358 -0
- package/src/audit/checks/client-rendering.ts +542 -0
- package/src/audit/checks/color-contrast.ts +342 -0
- package/src/audit/checks/content-freshness.ts +170 -0
- package/src/audit/checks/content-science.ts +589 -0
- package/src/audit/checks/conversion-elements.ts +526 -0
- package/src/audit/checks/crawlability.ts +220 -0
- package/src/audit/checks/directory-listing.ts +172 -0
- package/src/audit/checks/dom-analysis.ts +191 -0
- package/src/audit/checks/dom-size.ts +246 -0
- package/src/audit/checks/duplicate-content.ts +194 -0
- package/src/audit/checks/eeat-signals.ts +990 -0
- package/src/audit/checks/entity-seo.ts +396 -0
- package/src/audit/checks/featured-snippet.ts +473 -0
- package/src/audit/checks/freshness-signals.ts +443 -0
- package/src/audit/checks/funnel-intent.ts +463 -0
- package/src/audit/checks/hreflang.ts +174 -0
- package/src/audit/checks/html-compliance.ts +302 -0
- package/src/audit/checks/image-dimensions.ts +167 -0
- package/src/audit/checks/images.ts +160 -0
- package/src/audit/checks/indexnow.ts +275 -0
- package/src/audit/checks/interactive-tools.ts +475 -0
- package/src/audit/checks/internal-link-graph.ts +436 -0
- package/src/audit/checks/keyword-analysis.ts +239 -0
- package/src/audit/checks/keyword-cannibalization.ts +385 -0
- package/src/audit/checks/keyword-placement.ts +471 -0
- package/src/audit/checks/links.ts +203 -0
- package/src/audit/checks/llms-txt.ts +224 -0
- package/src/audit/checks/local-seo.ts +296 -0
- package/src/audit/checks/mobile.ts +167 -0
- package/src/audit/checks/modern-images.ts +226 -0
- package/src/audit/checks/navboost-signals.ts +395 -0
- package/src/audit/checks/on-page.ts +209 -0
- package/src/audit/checks/page-resources.ts +285 -0
- package/src/audit/checks/pagination.ts +180 -0
- package/src/audit/checks/performance.ts +153 -0
- package/src/audit/checks/platform-presence.ts +580 -0
- package/src/audit/checks/redirect-analysis.ts +153 -0
- package/src/audit/checks/redirect-chain.ts +389 -0
- package/src/audit/checks/resource-hints.ts +420 -0
- package/src/audit/checks/responsive-css.ts +247 -0
- package/src/audit/checks/responsive-images.ts +396 -0
- package/src/audit/checks/review-ecosystem.ts +415 -0
- package/src/audit/checks/robots-validation.ts +373 -0
- package/src/audit/checks/security-headers.ts +172 -0
- package/src/audit/checks/security.ts +144 -0
- package/src/audit/checks/serp-preview.ts +251 -0
- package/src/audit/checks/site-maturity.ts +444 -0
- package/src/audit/checks/social-meta.test.ts +275 -0
- package/src/audit/checks/social-meta.ts +134 -0
- package/src/audit/checks/soft-404.ts +151 -0
- package/src/audit/checks/structured-data.ts +238 -0
- package/src/audit/checks/tech-detection.ts +496 -0
- package/src/audit/checks/topical-clusters.ts +435 -0
- package/src/audit/checks/tracker-bloat.ts +462 -0
- package/src/audit/checks/tracking-verification.test.ts +371 -0
- package/src/audit/checks/tracking-verification.ts +636 -0
- package/src/audit/checks/url-safety.ts +682 -0
- package/src/audit/deno-entry.ts +66 -0
- package/src/audit/discovery/index.ts +15 -0
- package/src/audit/discovery/link-crawler.ts +232 -0
- package/src/audit/discovery/repo-routes.ts +347 -0
- package/src/audit/engine.ts +620 -0
- package/src/audit/fixes/index.ts +209 -0
- package/src/audit/fixes/social-meta-fixes.test.ts +329 -0
- package/src/audit/fixes/social-meta-fixes.ts +463 -0
- package/src/audit/index.ts +74 -0
- package/src/audit/runner.test.ts +299 -0
- package/src/audit/runner.ts +130 -0
- package/src/audit/types.ts +1953 -0
- package/src/content/featured-snippet.ts +367 -0
- package/src/content/generator.test.ts +534 -0
- package/src/content/generator.ts +501 -0
- package/src/content/headline.ts +317 -0
- package/src/content/index.ts +62 -0
- package/src/content/intent.ts +258 -0
- package/src/content/keyword-density.ts +349 -0
- package/src/content/readability.ts +262 -0
- package/src/executor.ts +336 -0
- package/src/fixer.ts +416 -0
- package/src/frameworks/detector.test.ts +248 -0
- package/src/frameworks/detector.ts +371 -0
- package/src/frameworks/index.ts +68 -0
- package/src/frameworks/recipes/angular.yaml +171 -0
- package/src/frameworks/recipes/astro.yaml +206 -0
- package/src/frameworks/recipes/django.yaml +180 -0
- package/src/frameworks/recipes/laravel.yaml +137 -0
- package/src/frameworks/recipes/nextjs.yaml +268 -0
- package/src/frameworks/recipes/nuxt.yaml +175 -0
- package/src/frameworks/recipes/rails.yaml +188 -0
- package/src/frameworks/recipes/react.yaml +202 -0
- package/src/frameworks/recipes/sveltekit.yaml +154 -0
- package/src/frameworks/recipes/vue.yaml +137 -0
- package/src/frameworks/recipes/wordpress.yaml +209 -0
- package/src/frameworks/suggestion-engine.ts +320 -0
- package/src/geo/geo-content.test.ts +305 -0
- package/src/geo/geo-content.ts +266 -0
- package/src/geo/geo-history.test.ts +473 -0
- package/src/geo/geo-history.ts +433 -0
- package/src/geo/geo-tracker.test.ts +359 -0
- package/src/geo/geo-tracker.ts +411 -0
- package/src/geo/index.ts +10 -0
- package/src/git/commit-helper.test.ts +261 -0
- package/src/git/commit-helper.ts +329 -0
- package/src/git/index.ts +12 -0
- package/src/git/pr-helper.test.ts +284 -0
- package/src/git/pr-helper.ts +307 -0
- package/src/index.ts +66 -0
- package/src/keywords/ai-keyword-engine.ts +1062 -0
- package/src/keywords/ai-summarizer.ts +387 -0
- package/src/keywords/ci-mode.ts +555 -0
- package/src/keywords/engine.ts +359 -0
- package/src/keywords/index.ts +151 -0
- package/src/keywords/llm-judge.ts +357 -0
- package/src/keywords/nlp-analysis.ts +706 -0
- package/src/keywords/prioritizer.ts +295 -0
- package/src/keywords/site-crawler.ts +342 -0
- package/src/keywords/sources/autocomplete.ts +139 -0
- package/src/keywords/sources/competitive-search.ts +450 -0
- package/src/keywords/sources/competitor-analysis.ts +374 -0
- package/src/keywords/sources/dataforseo.ts +206 -0
- package/src/keywords/sources/free-sources.ts +294 -0
- package/src/keywords/sources/gsc.ts +123 -0
- package/src/keywords/topic-grouping.ts +327 -0
- package/src/keywords/types.ts +144 -0
- package/src/keywords/wizard.ts +457 -0
- package/src/loader.ts +40 -0
- package/src/reports/index.ts +7 -0
- package/src/reports/report-generator.test.ts +293 -0
- package/src/reports/report-generator.ts +713 -0
- package/src/scheduler/alerts.test.ts +458 -0
- package/src/scheduler/alerts.ts +328 -0
- package/src/scheduler/index.ts +8 -0
- package/src/scheduler/scheduled-audit.test.ts +377 -0
- package/src/scheduler/scheduled-audit.ts +149 -0
- package/src/test/integration-test.ts +325 -0
- package/src/tools/analyzer.ts +373 -0
- package/src/tools/crawl.ts +293 -0
- package/src/tools/files.ts +301 -0
- package/src/tools/h1-fixer.ts +249 -0
- package/src/tools/index.ts +67 -0
- package/src/tracking/github-action.ts +326 -0
- package/src/tracking/google-analytics.ts +265 -0
- package/src/tracking/index.ts +45 -0
- package/src/tracking/report-generator.ts +386 -0
- package/src/tracking/search-console.ts +335 -0
- package/src/types.ts +134 -0
- package/src/utils/http.ts +302 -0
- package/src/wasm-adapter.ts +297 -0
- package/src/wasm-entry.ts +14 -0
- package/tsconfig.json +17 -0
- package/tsup.wasm.config.ts +26 -0
- package/vitest.config.ts +15 -0
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
sendSlackAlert,
|
|
4
|
+
sendDiscordAlert,
|
|
5
|
+
sendAlert,
|
|
6
|
+
formatAlertMessage,
|
|
7
|
+
shouldSendAlert,
|
|
8
|
+
AlertConfig,
|
|
9
|
+
AlertType,
|
|
10
|
+
AuditAlertPayload,
|
|
11
|
+
} from './alerts.js';
|
|
12
|
+
|
|
13
|
+
// Mock fetch globally
|
|
14
|
+
const mockFetch = vi.fn();
|
|
15
|
+
vi.stubGlobal('fetch', mockFetch);
|
|
16
|
+
|
|
17
|
+
describe('alerts', () => {
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
vi.clearAllMocks();
|
|
20
|
+
mockFetch.mockReset();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('formatAlertMessage', () => {
|
|
24
|
+
it('formats score drop alert with details', () => {
|
|
25
|
+
const payload: AuditAlertPayload = {
|
|
26
|
+
projectId: 'proj-1',
|
|
27
|
+
projectName: 'My App',
|
|
28
|
+
url: 'https://myapp.com',
|
|
29
|
+
score: 55,
|
|
30
|
+
previousScore: 72,
|
|
31
|
+
issuesFound: 5,
|
|
32
|
+
timestamp: '2024-01-15T10:00:00Z',
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const message = formatAlertMessage('score_drop', payload);
|
|
36
|
+
|
|
37
|
+
expect(message.title).toContain('Score Drop');
|
|
38
|
+
expect(message.title).toContain('My App');
|
|
39
|
+
expect(message.body).toContain('55');
|
|
40
|
+
expect(message.body).toContain('72');
|
|
41
|
+
expect(message.body).toContain('-17');
|
|
42
|
+
expect(message.severity).toBe('warning');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('formats error alert with severity', () => {
|
|
46
|
+
const payload: AuditAlertPayload = {
|
|
47
|
+
projectId: 'proj-1',
|
|
48
|
+
projectName: 'My App',
|
|
49
|
+
url: 'https://myapp.com',
|
|
50
|
+
score: 45,
|
|
51
|
+
issuesFound: 8,
|
|
52
|
+
errorCount: 3,
|
|
53
|
+
timestamp: '2024-01-15T10:00:00Z',
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const message = formatAlertMessage('error', payload);
|
|
57
|
+
|
|
58
|
+
expect(message.title).toContain('Critical Issues');
|
|
59
|
+
expect(message.body).toContain('3 errors');
|
|
60
|
+
expect(message.severity).toBe('error');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('formats weekly report alert', () => {
|
|
64
|
+
const payload: AuditAlertPayload = {
|
|
65
|
+
projectId: 'proj-1',
|
|
66
|
+
projectName: 'My App',
|
|
67
|
+
url: 'https://myapp.com',
|
|
68
|
+
score: 78,
|
|
69
|
+
previousScore: 72,
|
|
70
|
+
issuesFound: 2,
|
|
71
|
+
timestamp: '2024-01-15T10:00:00Z',
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const message = formatAlertMessage('weekly_report', payload);
|
|
75
|
+
|
|
76
|
+
expect(message.title).toContain('Weekly SEO Report');
|
|
77
|
+
expect(message.body).toContain('78');
|
|
78
|
+
expect(message.body).toContain('+6');
|
|
79
|
+
expect(message.severity).toBe('info');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('formats PR created alert', () => {
|
|
83
|
+
const payload: AuditAlertPayload = {
|
|
84
|
+
projectId: 'proj-1',
|
|
85
|
+
projectName: 'My App',
|
|
86
|
+
url: 'https://myapp.com',
|
|
87
|
+
score: 65,
|
|
88
|
+
issuesFound: 4,
|
|
89
|
+
prUrl: 'https://github.com/owner/repo/pull/42',
|
|
90
|
+
prNumber: 42,
|
|
91
|
+
timestamp: '2024-01-15T10:00:00Z',
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const message = formatAlertMessage('pr_created', payload);
|
|
95
|
+
|
|
96
|
+
expect(message.title).toContain('PR Created');
|
|
97
|
+
expect(message.body).toContain('#42');
|
|
98
|
+
expect(message.body).toContain('github.com');
|
|
99
|
+
expect(message.severity).toBe('success');
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe('shouldSendAlert', () => {
|
|
104
|
+
it('returns true when alert type is in config', () => {
|
|
105
|
+
const config: AlertConfig = {
|
|
106
|
+
type: 'slack',
|
|
107
|
+
webhookUrl: 'https://hooks.slack.com/...',
|
|
108
|
+
on: ['error', 'score_drop'],
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
expect(shouldSendAlert(config, 'error')).toBe(true);
|
|
112
|
+
expect(shouldSendAlert(config, 'score_drop')).toBe(true);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('returns false when alert type is not in config', () => {
|
|
116
|
+
const config: AlertConfig = {
|
|
117
|
+
type: 'slack',
|
|
118
|
+
webhookUrl: 'https://hooks.slack.com/...',
|
|
119
|
+
on: ['error'],
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
expect(shouldSendAlert(config, 'weekly_report')).toBe(false);
|
|
123
|
+
expect(shouldSendAlert(config, 'pr_created')).toBe(false);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('returns true for score_drop when threshold exceeded', () => {
|
|
127
|
+
const config: AlertConfig = {
|
|
128
|
+
type: 'slack',
|
|
129
|
+
webhookUrl: 'https://hooks.slack.com/...',
|
|
130
|
+
on: ['score_drop'],
|
|
131
|
+
scoreDropThreshold: 10,
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const payload: AuditAlertPayload = {
|
|
135
|
+
projectId: 'proj-1',
|
|
136
|
+
projectName: 'My App',
|
|
137
|
+
url: 'https://myapp.com',
|
|
138
|
+
score: 55,
|
|
139
|
+
previousScore: 70, // 15 point drop
|
|
140
|
+
issuesFound: 5,
|
|
141
|
+
timestamp: '2024-01-15T10:00:00Z',
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
expect(shouldSendAlert(config, 'score_drop', payload)).toBe(true);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('returns false for score_drop when threshold not exceeded', () => {
|
|
148
|
+
const config: AlertConfig = {
|
|
149
|
+
type: 'slack',
|
|
150
|
+
webhookUrl: 'https://hooks.slack.com/...',
|
|
151
|
+
on: ['score_drop'],
|
|
152
|
+
scoreDropThreshold: 10,
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const payload: AuditAlertPayload = {
|
|
156
|
+
projectId: 'proj-1',
|
|
157
|
+
projectName: 'My App',
|
|
158
|
+
url: 'https://myapp.com',
|
|
159
|
+
score: 68,
|
|
160
|
+
previousScore: 72, // 4 point drop, below threshold
|
|
161
|
+
issuesFound: 1,
|
|
162
|
+
timestamp: '2024-01-15T10:00:00Z',
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
expect(shouldSendAlert(config, 'score_drop', payload)).toBe(false);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
describe('sendSlackAlert', () => {
|
|
170
|
+
it('sends formatted message to Slack webhook', async () => {
|
|
171
|
+
mockFetch.mockResolvedValue({ ok: true });
|
|
172
|
+
|
|
173
|
+
const payload: AuditAlertPayload = {
|
|
174
|
+
projectId: 'proj-1',
|
|
175
|
+
projectName: 'My App',
|
|
176
|
+
url: 'https://myapp.com',
|
|
177
|
+
score: 72,
|
|
178
|
+
issuesFound: 3,
|
|
179
|
+
timestamp: '2024-01-15T10:00:00Z',
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const result = await sendSlackAlert(
|
|
183
|
+
'https://hooks.slack.com/services/xxx',
|
|
184
|
+
'weekly_report',
|
|
185
|
+
payload
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
expect(result.success).toBe(true);
|
|
189
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
190
|
+
'https://hooks.slack.com/services/xxx',
|
|
191
|
+
expect.objectContaining({
|
|
192
|
+
method: 'POST',
|
|
193
|
+
headers: { 'Content-Type': 'application/json' },
|
|
194
|
+
body: expect.stringContaining('Weekly SEO Report'),
|
|
195
|
+
})
|
|
196
|
+
);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('formats Slack message with blocks', async () => {
|
|
200
|
+
mockFetch.mockResolvedValue({ ok: true });
|
|
201
|
+
|
|
202
|
+
const payload: AuditAlertPayload = {
|
|
203
|
+
projectId: 'proj-1',
|
|
204
|
+
projectName: 'My App',
|
|
205
|
+
url: 'https://myapp.com',
|
|
206
|
+
score: 55,
|
|
207
|
+
previousScore: 70,
|
|
208
|
+
issuesFound: 5,
|
|
209
|
+
timestamp: '2024-01-15T10:00:00Z',
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
await sendSlackAlert('https://hooks.slack.com/xxx', 'score_drop', payload);
|
|
213
|
+
|
|
214
|
+
const callBody = JSON.parse(mockFetch.mock.calls[0][1].body);
|
|
215
|
+
expect(callBody.blocks).toBeDefined();
|
|
216
|
+
expect(callBody.blocks.length).toBeGreaterThan(0);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('handles Slack API errors', async () => {
|
|
220
|
+
mockFetch.mockResolvedValue({ ok: false, status: 404 });
|
|
221
|
+
|
|
222
|
+
const payload: AuditAlertPayload = {
|
|
223
|
+
projectId: 'proj-1',
|
|
224
|
+
projectName: 'My App',
|
|
225
|
+
url: 'https://myapp.com',
|
|
226
|
+
score: 72,
|
|
227
|
+
issuesFound: 2,
|
|
228
|
+
timestamp: '2024-01-15T10:00:00Z',
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const result = await sendSlackAlert(
|
|
232
|
+
'https://hooks.slack.com/invalid',
|
|
233
|
+
'weekly_report',
|
|
234
|
+
payload
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
expect(result.success).toBe(false);
|
|
238
|
+
expect(result.error).toContain('404');
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it('handles network errors', async () => {
|
|
242
|
+
mockFetch.mockRejectedValue(new Error('Network error'));
|
|
243
|
+
|
|
244
|
+
const payload: AuditAlertPayload = {
|
|
245
|
+
projectId: 'proj-1',
|
|
246
|
+
projectName: 'My App',
|
|
247
|
+
url: 'https://myapp.com',
|
|
248
|
+
score: 72,
|
|
249
|
+
issuesFound: 2,
|
|
250
|
+
timestamp: '2024-01-15T10:00:00Z',
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const result = await sendSlackAlert(
|
|
254
|
+
'https://hooks.slack.com/xxx',
|
|
255
|
+
'weekly_report',
|
|
256
|
+
payload
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
expect(result.success).toBe(false);
|
|
260
|
+
expect(result.error).toContain('Network error');
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
describe('sendDiscordAlert', () => {
|
|
265
|
+
it('sends formatted message to Discord webhook', async () => {
|
|
266
|
+
mockFetch.mockResolvedValue({ ok: true });
|
|
267
|
+
|
|
268
|
+
const payload: AuditAlertPayload = {
|
|
269
|
+
projectId: 'proj-1',
|
|
270
|
+
projectName: 'My App',
|
|
271
|
+
url: 'https://myapp.com',
|
|
272
|
+
score: 72,
|
|
273
|
+
issuesFound: 3,
|
|
274
|
+
timestamp: '2024-01-15T10:00:00Z',
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
const result = await sendDiscordAlert(
|
|
278
|
+
'https://discord.com/api/webhooks/xxx',
|
|
279
|
+
'weekly_report',
|
|
280
|
+
payload
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
expect(result.success).toBe(true);
|
|
284
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
285
|
+
'https://discord.com/api/webhooks/xxx',
|
|
286
|
+
expect.objectContaining({
|
|
287
|
+
method: 'POST',
|
|
288
|
+
headers: { 'Content-Type': 'application/json' },
|
|
289
|
+
})
|
|
290
|
+
);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it('formats Discord message with embeds', async () => {
|
|
294
|
+
mockFetch.mockResolvedValue({ ok: true });
|
|
295
|
+
|
|
296
|
+
const payload: AuditAlertPayload = {
|
|
297
|
+
projectId: 'proj-1',
|
|
298
|
+
projectName: 'My App',
|
|
299
|
+
url: 'https://myapp.com',
|
|
300
|
+
score: 55,
|
|
301
|
+
previousScore: 70,
|
|
302
|
+
issuesFound: 5,
|
|
303
|
+
timestamp: '2024-01-15T10:00:00Z',
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
await sendDiscordAlert('https://discord.com/api/webhooks/xxx', 'score_drop', payload);
|
|
307
|
+
|
|
308
|
+
const callBody = JSON.parse(mockFetch.mock.calls[0][1].body);
|
|
309
|
+
expect(callBody.embeds).toBeDefined();
|
|
310
|
+
expect(callBody.embeds.length).toBeGreaterThan(0);
|
|
311
|
+
expect(callBody.embeds[0].color).toBeDefined();
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it('handles Discord API errors', async () => {
|
|
315
|
+
mockFetch.mockResolvedValue({ ok: false, status: 401 });
|
|
316
|
+
|
|
317
|
+
const payload: AuditAlertPayload = {
|
|
318
|
+
projectId: 'proj-1',
|
|
319
|
+
projectName: 'My App',
|
|
320
|
+
url: 'https://myapp.com',
|
|
321
|
+
score: 72,
|
|
322
|
+
issuesFound: 2,
|
|
323
|
+
timestamp: '2024-01-15T10:00:00Z',
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
const result = await sendDiscordAlert(
|
|
327
|
+
'https://discord.com/api/webhooks/invalid',
|
|
328
|
+
'weekly_report',
|
|
329
|
+
payload
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
expect(result.success).toBe(false);
|
|
333
|
+
expect(result.error).toContain('401');
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
describe('sendAlert', () => {
|
|
338
|
+
it('sends to Slack when type is slack', async () => {
|
|
339
|
+
mockFetch.mockResolvedValue({ ok: true });
|
|
340
|
+
|
|
341
|
+
const config: AlertConfig = {
|
|
342
|
+
type: 'slack',
|
|
343
|
+
webhookUrl: 'https://hooks.slack.com/xxx',
|
|
344
|
+
on: ['weekly_report'],
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
const payload: AuditAlertPayload = {
|
|
348
|
+
projectId: 'proj-1',
|
|
349
|
+
projectName: 'My App',
|
|
350
|
+
url: 'https://myapp.com',
|
|
351
|
+
score: 72,
|
|
352
|
+
issuesFound: 3,
|
|
353
|
+
timestamp: '2024-01-15T10:00:00Z',
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
const result = await sendAlert(config, 'weekly_report', payload);
|
|
357
|
+
|
|
358
|
+
expect(result.success).toBe(true);
|
|
359
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
360
|
+
'https://hooks.slack.com/xxx',
|
|
361
|
+
expect.anything()
|
|
362
|
+
);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it('sends to Discord when type is discord', async () => {
|
|
366
|
+
mockFetch.mockResolvedValue({ ok: true });
|
|
367
|
+
|
|
368
|
+
const config: AlertConfig = {
|
|
369
|
+
type: 'discord',
|
|
370
|
+
webhookUrl: 'https://discord.com/api/webhooks/xxx',
|
|
371
|
+
on: ['error'],
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
const payload: AuditAlertPayload = {
|
|
375
|
+
projectId: 'proj-1',
|
|
376
|
+
projectName: 'My App',
|
|
377
|
+
url: 'https://myapp.com',
|
|
378
|
+
score: 45,
|
|
379
|
+
issuesFound: 8,
|
|
380
|
+
errorCount: 3,
|
|
381
|
+
timestamp: '2024-01-15T10:00:00Z',
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
const result = await sendAlert(config, 'error', payload);
|
|
385
|
+
|
|
386
|
+
expect(result.success).toBe(true);
|
|
387
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
388
|
+
'https://discord.com/api/webhooks/xxx',
|
|
389
|
+
expect.anything()
|
|
390
|
+
);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it('skips alert if type not in config.on', async () => {
|
|
394
|
+
const config: AlertConfig = {
|
|
395
|
+
type: 'slack',
|
|
396
|
+
webhookUrl: 'https://hooks.slack.com/xxx',
|
|
397
|
+
on: ['error'],
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
const payload: AuditAlertPayload = {
|
|
401
|
+
projectId: 'proj-1',
|
|
402
|
+
projectName: 'My App',
|
|
403
|
+
url: 'https://myapp.com',
|
|
404
|
+
score: 72,
|
|
405
|
+
issuesFound: 3,
|
|
406
|
+
timestamp: '2024-01-15T10:00:00Z',
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
const result = await sendAlert(config, 'weekly_report', payload);
|
|
410
|
+
|
|
411
|
+
expect(result.success).toBe(true);
|
|
412
|
+
expect(result.skipped).toBe(true);
|
|
413
|
+
expect(mockFetch).not.toHaveBeenCalled();
|
|
414
|
+
});
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
describe('integration scenarios', () => {
|
|
418
|
+
it('handles multiple alert configs', async () => {
|
|
419
|
+
mockFetch.mockResolvedValue({ ok: true });
|
|
420
|
+
|
|
421
|
+
const configs: AlertConfig[] = [
|
|
422
|
+
{
|
|
423
|
+
type: 'slack',
|
|
424
|
+
webhookUrl: 'https://hooks.slack.com/xxx',
|
|
425
|
+
on: ['error', 'score_drop'],
|
|
426
|
+
},
|
|
427
|
+
{
|
|
428
|
+
type: 'discord',
|
|
429
|
+
webhookUrl: 'https://discord.com/api/webhooks/xxx',
|
|
430
|
+
on: ['weekly_report'],
|
|
431
|
+
},
|
|
432
|
+
];
|
|
433
|
+
|
|
434
|
+
const payload: AuditAlertPayload = {
|
|
435
|
+
projectId: 'proj-1',
|
|
436
|
+
projectName: 'My App',
|
|
437
|
+
url: 'https://myapp.com',
|
|
438
|
+
score: 55,
|
|
439
|
+
previousScore: 70,
|
|
440
|
+
issuesFound: 5,
|
|
441
|
+
timestamp: '2024-01-15T10:00:00Z',
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
// Send to all configs
|
|
445
|
+
const results = await Promise.all(
|
|
446
|
+
configs.map(config => sendAlert(config, 'score_drop', payload))
|
|
447
|
+
);
|
|
448
|
+
|
|
449
|
+
// Slack should send (score_drop is in its config)
|
|
450
|
+
expect(results[0].success).toBe(true);
|
|
451
|
+
expect(results[0].skipped).toBeFalsy();
|
|
452
|
+
|
|
453
|
+
// Discord should skip (score_drop not in its config)
|
|
454
|
+
expect(results[1].success).toBe(true);
|
|
455
|
+
expect(results[1].skipped).toBe(true);
|
|
456
|
+
});
|
|
457
|
+
});
|
|
458
|
+
});
|