@verisource/cli 0.0.0-stage → 1.0.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.
- package/README.md +67 -2
- package/dist/bin.d.ts +20 -0
- package/dist/bin.js +11973 -0
- package/dist/browser.d.ts +75 -0
- package/dist/browser.js +432 -0
- package/dist/cloud.d.ts +79 -0
- package/dist/cloud.js +93 -0
- package/dist/github-pr.d.ts +24 -0
- package/dist/github-pr.js +129 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8019 -0
- package/dist/logs.d.ts +17 -0
- package/dist/logs.js +168 -0
- package/dist/mapper.d.ts +36 -0
- package/dist/mapper.js +204 -0
- package/dist/skills.d.ts +5 -0
- package/dist/skills.js +45 -0
- package/dist/store.d.ts +56 -0
- package/dist/store.js +133 -0
- package/package.json +54 -3
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { type StoredHandleItem } from './store.js';
|
|
2
|
+
export type WorkflowStepAction = {
|
|
3
|
+
action: 'goto';
|
|
4
|
+
url: string;
|
|
5
|
+
} | {
|
|
6
|
+
action: 'click';
|
|
7
|
+
selector: string;
|
|
8
|
+
} | {
|
|
9
|
+
action: 'fill';
|
|
10
|
+
selector: string;
|
|
11
|
+
value: string;
|
|
12
|
+
} | {
|
|
13
|
+
action: 'select';
|
|
14
|
+
selector: string;
|
|
15
|
+
value: string;
|
|
16
|
+
} | {
|
|
17
|
+
action: 'press';
|
|
18
|
+
key: string;
|
|
19
|
+
} | {
|
|
20
|
+
action: 'expectStatus';
|
|
21
|
+
status: number;
|
|
22
|
+
} | {
|
|
23
|
+
action: 'expectText';
|
|
24
|
+
text: string;
|
|
25
|
+
} | {
|
|
26
|
+
action: 'expectJsonLd';
|
|
27
|
+
type: string;
|
|
28
|
+
};
|
|
29
|
+
export interface VerisourceWorkflowSpec {
|
|
30
|
+
name: string;
|
|
31
|
+
baseUrl: string;
|
|
32
|
+
steps: WorkflowStepAction[];
|
|
33
|
+
}
|
|
34
|
+
export interface BrowserElementHandle {
|
|
35
|
+
handle: string;
|
|
36
|
+
tag: string;
|
|
37
|
+
selector: string;
|
|
38
|
+
label: string;
|
|
39
|
+
href?: string;
|
|
40
|
+
}
|
|
41
|
+
export interface BrowseSessionState {
|
|
42
|
+
currentUrl: string;
|
|
43
|
+
httpStatus: number | null;
|
|
44
|
+
title: string | null;
|
|
45
|
+
elements: BrowserElementHandle[];
|
|
46
|
+
history: WorkflowStepAction[];
|
|
47
|
+
}
|
|
48
|
+
export interface WorkflowRunResult {
|
|
49
|
+
workflow: string;
|
|
50
|
+
status: 'PASS' | 'FAIL';
|
|
51
|
+
stepsPassed: number;
|
|
52
|
+
stepsTotal: number;
|
|
53
|
+
durationMs: number;
|
|
54
|
+
engine: 'playwright' | 'http_dom';
|
|
55
|
+
handles: StoredHandleItem[];
|
|
56
|
+
}
|
|
57
|
+
export declare function validateWorkflowFiles(cwd?: string): {
|
|
58
|
+
valid: boolean;
|
|
59
|
+
checkedCount: number;
|
|
60
|
+
errors: string[];
|
|
61
|
+
};
|
|
62
|
+
export declare function browseOpenUrl(targetUrl: string, cwd?: string): Promise<BrowseSessionState>;
|
|
63
|
+
export declare function browseClickHandle(handleId: string, cwd?: string): Promise<BrowseSessionState>;
|
|
64
|
+
export declare function saveBrowseSessionAsWorkflow(name: string, cwd?: string): string;
|
|
65
|
+
export declare function recordBaselineWorkflow(params: {
|
|
66
|
+
name: string;
|
|
67
|
+
targetUrl: string;
|
|
68
|
+
cwd?: string;
|
|
69
|
+
}): Promise<string>;
|
|
70
|
+
export declare function runSavedWorkflow(params: {
|
|
71
|
+
workflowSpec: VerisourceWorkflowSpec;
|
|
72
|
+
headed?: boolean;
|
|
73
|
+
timeoutMs?: number;
|
|
74
|
+
cwd?: string;
|
|
75
|
+
}): Promise<WorkflowRunResult>;
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { ensureVerisourceWorkspaceDir, saveLastRunLedger, } from './store.js';
|
|
4
|
+
import { logEventsToStoredHandles, queryCorrelatedBackendLogs, } from './logs.js';
|
|
5
|
+
export function validateWorkflowFiles(cwd = process.cwd()) {
|
|
6
|
+
const wfDir = path.resolve(cwd, '.verisource', 'workflows');
|
|
7
|
+
if (!fs.existsSync(wfDir)) {
|
|
8
|
+
return { valid: true, checkedCount: 0, errors: [] };
|
|
9
|
+
}
|
|
10
|
+
const files = fs
|
|
11
|
+
.readdirSync(wfDir)
|
|
12
|
+
.filter((f) => f.endsWith('.json'))
|
|
13
|
+
.sort();
|
|
14
|
+
const errors = [];
|
|
15
|
+
for (const file of files) {
|
|
16
|
+
const full = path.join(wfDir, file);
|
|
17
|
+
try {
|
|
18
|
+
const parsed = JSON.parse(fs.readFileSync(full, 'utf8'));
|
|
19
|
+
if (!parsed.name || typeof parsed.name !== 'string') {
|
|
20
|
+
errors.push(`${file}: missing string "name"`);
|
|
21
|
+
}
|
|
22
|
+
if (!parsed.baseUrl || typeof parsed.baseUrl !== 'string') {
|
|
23
|
+
errors.push(`${file}: missing string "baseUrl"`);
|
|
24
|
+
}
|
|
25
|
+
if (!Array.isArray(parsed.steps) || parsed.steps.length === 0) {
|
|
26
|
+
errors.push(`${file}: "steps" must be a non-empty array`);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
parsed.steps.forEach((st, idx) => {
|
|
30
|
+
if (!st || typeof st !== 'object' || !('action' in st)) {
|
|
31
|
+
errors.push(`${file}[step ${idx}]: missing "action"`);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
catch (err) {
|
|
37
|
+
errors.push(`${file}: invalid JSON (${err instanceof Error ? err.message : 'parse error'})`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
valid: errors.length === 0,
|
|
42
|
+
checkedCount: files.length,
|
|
43
|
+
errors,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function extractInteractiveElementsFromHtml(html) {
|
|
47
|
+
const titleMatch = html.match(/<title[^>]*>([\s\S]*?)<\/title>/i);
|
|
48
|
+
const title = titleMatch?.[1]?.replace(/\s+/g, ' ').trim() ?? null;
|
|
49
|
+
const elements = [];
|
|
50
|
+
let idx = 0;
|
|
51
|
+
const tagRegex = /<(a|button|input|select)\b([^>]*)>([\s\S]*?)<\/\1>|<(input)\b([^>]*)\/?>/gi;
|
|
52
|
+
let m;
|
|
53
|
+
while ((m = tagRegex.exec(html)) !== null && elements.length < 40) {
|
|
54
|
+
const tag = (m[1] ?? m[4] ?? '').toLowerCase();
|
|
55
|
+
const attrs = m[2] ?? m[5] ?? '';
|
|
56
|
+
const inner = (m[3] ?? '')
|
|
57
|
+
.replace(/<[^>]+>/g, ' ')
|
|
58
|
+
.replace(/\s+/g, ' ')
|
|
59
|
+
.trim();
|
|
60
|
+
const idMatch = attrs.match(/\bid\s*=\s*["']([^"']+)["']/i);
|
|
61
|
+
const nameMatch = attrs.match(/\bname\s*=\s*["']([^"']+)["']/i);
|
|
62
|
+
const hrefMatch = attrs.match(/\bhref\s*=\s*["']([^"']+)["']/i);
|
|
63
|
+
const placeholderMatch = attrs.match(/\bplaceholder\s*=\s*["']([^"']+)["']/i);
|
|
64
|
+
const ariaMatch = attrs.match(/\baria-label\s*=\s*["']([^"']+)["']/i);
|
|
65
|
+
const label = (inner ||
|
|
66
|
+
ariaMatch?.[1] ||
|
|
67
|
+
placeholderMatch?.[1] ||
|
|
68
|
+
nameMatch?.[1] ||
|
|
69
|
+
idMatch?.[1] ||
|
|
70
|
+
hrefMatch?.[1] ||
|
|
71
|
+
tag).slice(0, 64);
|
|
72
|
+
const selector = idMatch?.[1]
|
|
73
|
+
? `#${idMatch[1]}`
|
|
74
|
+
: nameMatch?.[1]
|
|
75
|
+
? `${tag}[name="${nameMatch[1]}"]`
|
|
76
|
+
: hrefMatch?.[1] && tag === 'a'
|
|
77
|
+
? `a[href="${hrefMatch[1]}"]`
|
|
78
|
+
: `${tag}:nth-of-type(${idx + 1})`;
|
|
79
|
+
idx += 1;
|
|
80
|
+
elements.push({
|
|
81
|
+
handle: `e${idx}`,
|
|
82
|
+
tag,
|
|
83
|
+
selector,
|
|
84
|
+
label,
|
|
85
|
+
...(hrefMatch?.[1] ? { href: hrefMatch[1] } : {}),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return { title, elements };
|
|
89
|
+
}
|
|
90
|
+
export async function browseOpenUrl(targetUrl, cwd = process.cwd()) {
|
|
91
|
+
const dir = ensureVerisourceWorkspaceDir(cwd);
|
|
92
|
+
const res = await fetch(targetUrl, {
|
|
93
|
+
headers: { Accept: 'text/html,application/xhtml+xml' },
|
|
94
|
+
});
|
|
95
|
+
const html = await res.text();
|
|
96
|
+
const { title, elements } = extractInteractiveElementsFromHtml(html);
|
|
97
|
+
const session = {
|
|
98
|
+
currentUrl: res.url || targetUrl,
|
|
99
|
+
httpStatus: res.status,
|
|
100
|
+
title,
|
|
101
|
+
elements,
|
|
102
|
+
history: [{ action: 'goto', url: targetUrl }],
|
|
103
|
+
};
|
|
104
|
+
fs.writeFileSync(path.join(dir, 'browser-session.json'), JSON.stringify(session, null, 2), 'utf8');
|
|
105
|
+
saveLastRunLedger({
|
|
106
|
+
runId: `browse-${Date.now()}`,
|
|
107
|
+
command: `browse open ${targetUrl}`,
|
|
108
|
+
target: targetUrl,
|
|
109
|
+
startedAt: new Date().toISOString(),
|
|
110
|
+
finishedAt: new Date().toISOString(),
|
|
111
|
+
status: res.ok ? 'PASS' : 'FAIL',
|
|
112
|
+
handles: elements.map((el) => ({
|
|
113
|
+
handle: el.handle,
|
|
114
|
+
kind: 'element',
|
|
115
|
+
severity: 'info',
|
|
116
|
+
summary: `<${el.tag}> "${el.label}" (${el.selector})`,
|
|
117
|
+
url: el.href,
|
|
118
|
+
})),
|
|
119
|
+
}, cwd);
|
|
120
|
+
return session;
|
|
121
|
+
}
|
|
122
|
+
export async function browseClickHandle(handleId, cwd = process.cwd()) {
|
|
123
|
+
const sessionPath = path.resolve(cwd, '.verisource', 'browser-session.json');
|
|
124
|
+
if (!fs.existsSync(sessionPath)) {
|
|
125
|
+
throw new Error('No active browse session. Run `verisource browse open <url>` first.');
|
|
126
|
+
}
|
|
127
|
+
const prev = JSON.parse(fs.readFileSync(sessionPath, 'utf8'));
|
|
128
|
+
const clean = handleId.replace(/^\[|\]$/g, '').trim().toLowerCase();
|
|
129
|
+
const target = prev.elements.find((e) => e.handle.toLowerCase() === clean);
|
|
130
|
+
if (!target) {
|
|
131
|
+
throw new Error(`Element handle "${handleId}" not found in active page.`);
|
|
132
|
+
}
|
|
133
|
+
const nextHistory = [
|
|
134
|
+
...prev.history,
|
|
135
|
+
{ action: 'click', selector: target.selector },
|
|
136
|
+
];
|
|
137
|
+
if (target.href && !target.href.startsWith('#')) {
|
|
138
|
+
const nextUrl = new URL(target.href, prev.currentUrl).toString();
|
|
139
|
+
const updated = await browseOpenUrl(nextUrl, cwd);
|
|
140
|
+
updated.history = nextHistory;
|
|
141
|
+
fs.writeFileSync(sessionPath, JSON.stringify(updated, null, 2), 'utf8');
|
|
142
|
+
return updated;
|
|
143
|
+
}
|
|
144
|
+
prev.history = nextHistory;
|
|
145
|
+
fs.writeFileSync(sessionPath, JSON.stringify(prev, null, 2), 'utf8');
|
|
146
|
+
return prev;
|
|
147
|
+
}
|
|
148
|
+
export function saveBrowseSessionAsWorkflow(name, cwd = process.cwd()) {
|
|
149
|
+
const dir = ensureVerisourceWorkspaceDir(cwd);
|
|
150
|
+
const sessionPath = path.join(dir, 'browser-session.json');
|
|
151
|
+
if (!fs.existsSync(sessionPath)) {
|
|
152
|
+
throw new Error('No active browse session to save. Run `verisource browse open <url>` first.');
|
|
153
|
+
}
|
|
154
|
+
const session = JSON.parse(fs.readFileSync(sessionPath, 'utf8'));
|
|
155
|
+
const parsedOrigin = new URL(session.currentUrl).origin;
|
|
156
|
+
const cleanName = name.replace(/[^a-zA-Z0-9_-]/g, '-');
|
|
157
|
+
const spec = {
|
|
158
|
+
name: cleanName,
|
|
159
|
+
baseUrl: parsedOrigin,
|
|
160
|
+
steps: [
|
|
161
|
+
...session.history,
|
|
162
|
+
{ action: 'expectStatus', status: 200 },
|
|
163
|
+
],
|
|
164
|
+
};
|
|
165
|
+
const outPath = path.join(dir, 'workflows', `${cleanName}.json`);
|
|
166
|
+
fs.writeFileSync(outPath, JSON.stringify(spec, null, 2), 'utf8');
|
|
167
|
+
return outPath;
|
|
168
|
+
}
|
|
169
|
+
export async function recordBaselineWorkflow(params) {
|
|
170
|
+
const cwd = params.cwd ?? process.cwd();
|
|
171
|
+
const dir = ensureVerisourceWorkspaceDir(cwd);
|
|
172
|
+
const cleanName = params.name.replace(/[^a-zA-Z0-9_-]/g, '-');
|
|
173
|
+
const res = await fetch(params.targetUrl);
|
|
174
|
+
const html = await res.text();
|
|
175
|
+
const { title } = extractInteractiveElementsFromHtml(html);
|
|
176
|
+
const parsed = new URL(params.targetUrl);
|
|
177
|
+
const steps = [
|
|
178
|
+
{ action: 'goto', url: parsed.pathname + parsed.search || '/' },
|
|
179
|
+
{ action: 'expectStatus', status: res.status },
|
|
180
|
+
];
|
|
181
|
+
if (title) {
|
|
182
|
+
steps.push({ action: 'expectText', text: title.slice(0, 36) });
|
|
183
|
+
}
|
|
184
|
+
if (/application\/ld\+json/i.test(html)) {
|
|
185
|
+
const typeMatch = html.match(/['"]@type['"]\s*:\s*['"]([A-Za-z0-9_]+)['"]/);
|
|
186
|
+
if (typeMatch?.[1]) {
|
|
187
|
+
steps.push({ action: 'expectJsonLd', type: typeMatch[1] });
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
const spec = {
|
|
191
|
+
name: cleanName,
|
|
192
|
+
baseUrl: parsed.origin,
|
|
193
|
+
steps,
|
|
194
|
+
};
|
|
195
|
+
const outPath = path.join(dir, 'workflows', `${cleanName}.json`);
|
|
196
|
+
fs.writeFileSync(outPath, JSON.stringify(spec, null, 2), 'utf8');
|
|
197
|
+
return outPath;
|
|
198
|
+
}
|
|
199
|
+
async function tryLoadPlaywrightChromium() {
|
|
200
|
+
for (const pkgName of ['playwright', 'playwright-core']) {
|
|
201
|
+
try {
|
|
202
|
+
const dynamicImport = new Function('m', 'return import(m)');
|
|
203
|
+
const mod = await dynamicImport(pkgName);
|
|
204
|
+
if (mod?.chromium)
|
|
205
|
+
return mod.chromium;
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
// Optional peer package not installed
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
export async function runSavedWorkflow(params) {
|
|
214
|
+
const cwd = params.cwd ?? process.cwd();
|
|
215
|
+
const startedAt = Date.now();
|
|
216
|
+
const handles = [];
|
|
217
|
+
let stepsPassed = 0;
|
|
218
|
+
let netCounter = 0;
|
|
219
|
+
const needsInteractiveBrowser = params.workflowSpec.steps.some((s) => ['click', 'fill', 'select', 'press'].includes(s.action));
|
|
220
|
+
const chromium = needsInteractiveBrowser
|
|
221
|
+
? await tryLoadPlaywrightChromium()
|
|
222
|
+
: null;
|
|
223
|
+
if (chromium) {
|
|
224
|
+
// Execute with real Playwright Chromium when interactive steps are present & installed
|
|
225
|
+
const browser = (await chromium.launch({
|
|
226
|
+
headless: !params.headed,
|
|
227
|
+
}));
|
|
228
|
+
try {
|
|
229
|
+
const page = await browser.newPage();
|
|
230
|
+
let lastStatus = 200;
|
|
231
|
+
page.on('response', (resp) => {
|
|
232
|
+
const r = resp;
|
|
233
|
+
if (r.status() >= 400) {
|
|
234
|
+
netCounter += 1;
|
|
235
|
+
handles.push({
|
|
236
|
+
handle: `n${netCounter}`,
|
|
237
|
+
kind: 'network',
|
|
238
|
+
severity: r.status() >= 500 ? 'critical' : 'warn',
|
|
239
|
+
summary: `HTTP ${r.status()} on ${r.url()}`,
|
|
240
|
+
url: r.url(),
|
|
241
|
+
httpStatus: r.status(),
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
for (const step of params.workflowSpec.steps) {
|
|
246
|
+
if (step.action === 'goto') {
|
|
247
|
+
const full = new URL(step.url, params.workflowSpec.baseUrl).toString();
|
|
248
|
+
const nav = await page.goto(full, {
|
|
249
|
+
timeout: params.timeoutMs ?? 10_000,
|
|
250
|
+
});
|
|
251
|
+
lastStatus = nav?.status() ?? 200;
|
|
252
|
+
}
|
|
253
|
+
else if (step.action === 'click') {
|
|
254
|
+
await page.click(step.selector, {
|
|
255
|
+
timeout: params.timeoutMs ?? 6_000,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
else if (step.action === 'fill') {
|
|
259
|
+
await page.fill(step.selector, step.value);
|
|
260
|
+
}
|
|
261
|
+
else if (step.action === 'select') {
|
|
262
|
+
await page.selectOption(step.selector, step.value);
|
|
263
|
+
}
|
|
264
|
+
else if (step.action === 'press') {
|
|
265
|
+
await page.keyboard.press(step.key);
|
|
266
|
+
}
|
|
267
|
+
else if (step.action === 'expectStatus') {
|
|
268
|
+
if (lastStatus !== step.status) {
|
|
269
|
+
throw new Error(`Expected HTTP ${step.status}, got ${lastStatus}`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
else if (step.action === 'expectText') {
|
|
273
|
+
const html = await page.content();
|
|
274
|
+
if (!html.includes(step.text)) {
|
|
275
|
+
throw new Error(`Expected page text "${step.text}" not found`);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
else if (step.action === 'expectJsonLd') {
|
|
279
|
+
const html = await page.content();
|
|
280
|
+
if (!new RegExp(`['"]@type['"]\\s*:\\s*['"]${step.type}['"]`, 'i').test(html)) {
|
|
281
|
+
throw new Error(`Expected JSON-LD @type "${step.type}" not found`);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
stepsPassed += 1;
|
|
285
|
+
}
|
|
286
|
+
const logEvents = await queryCorrelatedBackendLogs({
|
|
287
|
+
windowStartMs: startedAt,
|
|
288
|
+
windowEndMs: Date.now(),
|
|
289
|
+
cwd,
|
|
290
|
+
});
|
|
291
|
+
handles.push(...logEventsToStoredHandles(logEvents));
|
|
292
|
+
const hasCrit = handles.some((h) => h.severity === 'critical');
|
|
293
|
+
return {
|
|
294
|
+
workflow: params.workflowSpec.name,
|
|
295
|
+
status: hasCrit ? 'FAIL' : 'PASS',
|
|
296
|
+
stepsPassed,
|
|
297
|
+
stepsTotal: params.workflowSpec.steps.length,
|
|
298
|
+
durationMs: Date.now() - startedAt,
|
|
299
|
+
engine: 'playwright',
|
|
300
|
+
handles,
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
catch (err) {
|
|
304
|
+
netCounter += 1;
|
|
305
|
+
handles.push({
|
|
306
|
+
handle: `n${netCounter}`,
|
|
307
|
+
kind: 'page_error',
|
|
308
|
+
severity: 'critical',
|
|
309
|
+
summary: err instanceof Error ? err.message : 'Workflow step failed',
|
|
310
|
+
});
|
|
311
|
+
return {
|
|
312
|
+
workflow: params.workflowSpec.name,
|
|
313
|
+
status: 'FAIL',
|
|
314
|
+
stepsPassed,
|
|
315
|
+
stepsTotal: params.workflowSpec.steps.length,
|
|
316
|
+
durationMs: Date.now() - startedAt,
|
|
317
|
+
engine: 'playwright',
|
|
318
|
+
handles,
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
finally {
|
|
322
|
+
await browser.close();
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
// Zero-dependency deterministic HTTP + DOM replay engine
|
|
326
|
+
let currentUrl = params.workflowSpec.baseUrl;
|
|
327
|
+
let lastStatus = null;
|
|
328
|
+
let lastHtml = '';
|
|
329
|
+
try {
|
|
330
|
+
for (const step of params.workflowSpec.steps) {
|
|
331
|
+
if (step.action === 'goto') {
|
|
332
|
+
currentUrl = new URL(step.url, params.workflowSpec.baseUrl).toString();
|
|
333
|
+
const res = await fetch(currentUrl);
|
|
334
|
+
lastStatus = res.status;
|
|
335
|
+
lastHtml = await res.text();
|
|
336
|
+
if (res.status >= 400) {
|
|
337
|
+
netCounter += 1;
|
|
338
|
+
handles.push({
|
|
339
|
+
handle: `n${netCounter}`,
|
|
340
|
+
kind: 'network',
|
|
341
|
+
severity: res.status >= 500 ? 'critical' : 'warn',
|
|
342
|
+
summary: `GET ${currentUrl} -> HTTP ${res.status}`,
|
|
343
|
+
url: currentUrl,
|
|
344
|
+
httpStatus: res.status,
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
else if (step.action === 'click') {
|
|
349
|
+
const hrefMatch = step.selector.match(/a\[href=["']([^"']+)["']\]/i);
|
|
350
|
+
if (hrefMatch?.[1]) {
|
|
351
|
+
currentUrl = new URL(hrefMatch[1], currentUrl).toString();
|
|
352
|
+
const res = await fetch(currentUrl);
|
|
353
|
+
lastStatus = res.status;
|
|
354
|
+
lastHtml = await res.text();
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
else if (step.action === 'expectStatus') {
|
|
358
|
+
if (lastStatus !== step.status) {
|
|
359
|
+
netCounter += 1;
|
|
360
|
+
handles.push({
|
|
361
|
+
handle: `n${netCounter}`,
|
|
362
|
+
kind: 'network',
|
|
363
|
+
severity: 'critical',
|
|
364
|
+
summary: `Expected HTTP ${step.status} on ${currentUrl}, received ${lastStatus}`,
|
|
365
|
+
url: currentUrl,
|
|
366
|
+
httpStatus: lastStatus,
|
|
367
|
+
});
|
|
368
|
+
throw new Error(`Status mismatch: expected ${step.status}, got ${lastStatus}`);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
else if (step.action === 'expectText') {
|
|
372
|
+
if (!lastHtml.includes(step.text)) {
|
|
373
|
+
handles.push({
|
|
374
|
+
handle: `p1`,
|
|
375
|
+
kind: 'page_error',
|
|
376
|
+
severity: 'critical',
|
|
377
|
+
summary: `Expected text "${step.text}" missing on ${currentUrl}`,
|
|
378
|
+
url: currentUrl,
|
|
379
|
+
});
|
|
380
|
+
throw new Error(`Missing text "${step.text}"`);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
else if (step.action === 'expectJsonLd') {
|
|
384
|
+
const re = new RegExp(`['"]@type['"]\\s*:\\s*['"]${step.type}['"]`, 'i');
|
|
385
|
+
if (!re.test(lastHtml)) {
|
|
386
|
+
handles.push({
|
|
387
|
+
handle: `s1`,
|
|
388
|
+
kind: 'ai_readability',
|
|
389
|
+
severity: 'critical',
|
|
390
|
+
summary: `Expected JSON-LD @type "${step.type}" missing on ${currentUrl}`,
|
|
391
|
+
url: currentUrl,
|
|
392
|
+
});
|
|
393
|
+
throw new Error(`Missing JSON-LD @type "${step.type}"`);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
stepsPassed += 1;
|
|
397
|
+
}
|
|
398
|
+
const logEvents = await queryCorrelatedBackendLogs({
|
|
399
|
+
windowStartMs: startedAt,
|
|
400
|
+
windowEndMs: Date.now(),
|
|
401
|
+
cwd,
|
|
402
|
+
});
|
|
403
|
+
handles.push(...logEventsToStoredHandles(logEvents));
|
|
404
|
+
const hasCrit = handles.some((h) => h.severity === 'critical');
|
|
405
|
+
return {
|
|
406
|
+
workflow: params.workflowSpec.name,
|
|
407
|
+
status: hasCrit ? 'FAIL' : 'PASS',
|
|
408
|
+
stepsPassed,
|
|
409
|
+
stepsTotal: params.workflowSpec.steps.length,
|
|
410
|
+
durationMs: Date.now() - startedAt,
|
|
411
|
+
engine: 'http_dom',
|
|
412
|
+
handles,
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
catch {
|
|
416
|
+
const logEvents = await queryCorrelatedBackendLogs({
|
|
417
|
+
windowStartMs: startedAt,
|
|
418
|
+
windowEndMs: Date.now(),
|
|
419
|
+
cwd,
|
|
420
|
+
});
|
|
421
|
+
handles.push(...logEventsToStoredHandles(logEvents));
|
|
422
|
+
return {
|
|
423
|
+
workflow: params.workflowSpec.name,
|
|
424
|
+
status: 'FAIL',
|
|
425
|
+
stepsPassed,
|
|
426
|
+
stepsTotal: params.workflowSpec.steps.length,
|
|
427
|
+
durationMs: Date.now() - startedAt,
|
|
428
|
+
engine: 'http_dom',
|
|
429
|
+
handles,
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
}
|
package/dist/cloud.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { type StoredHandleItem } from './store.js';
|
|
2
|
+
export interface CliCloudStatusResponse {
|
|
3
|
+
ok: boolean;
|
|
4
|
+
authenticated: boolean;
|
|
5
|
+
email: string;
|
|
6
|
+
planTier: string;
|
|
7
|
+
keyPrefix: string;
|
|
8
|
+
keyLabel: string;
|
|
9
|
+
trackedDomainCount: number;
|
|
10
|
+
domainCap: number;
|
|
11
|
+
dashboardUrl: string;
|
|
12
|
+
}
|
|
13
|
+
export interface CliCloudSyncPayload {
|
|
14
|
+
domain: string;
|
|
15
|
+
mode: 'scan' | 'verify';
|
|
16
|
+
status: 'PASS' | 'WARN' | 'FAIL';
|
|
17
|
+
visibilityScore: number;
|
|
18
|
+
llmsTxtStatus: 'present' | 'missing';
|
|
19
|
+
agentsMdStatus: 'missing' | 'default_boilerplate' | 'custom_enriched';
|
|
20
|
+
robotsAiBotStatus: {
|
|
21
|
+
GPTBot: boolean;
|
|
22
|
+
'OAI-SearchBot': boolean;
|
|
23
|
+
PerplexityBot: boolean;
|
|
24
|
+
ClaudeBot: boolean;
|
|
25
|
+
'Google-Extended'?: boolean;
|
|
26
|
+
waf_blocked?: boolean;
|
|
27
|
+
};
|
|
28
|
+
siteHealth?: {
|
|
29
|
+
healthScore: number;
|
|
30
|
+
pagesScanned: number;
|
|
31
|
+
linksChecked: number;
|
|
32
|
+
brokenLinksCount?: number;
|
|
33
|
+
deadAnchorsCount?: number;
|
|
34
|
+
};
|
|
35
|
+
handles: StoredHandleItem[];
|
|
36
|
+
gitMeta?: {
|
|
37
|
+
sha?: string;
|
|
38
|
+
branch?: string;
|
|
39
|
+
repo?: string;
|
|
40
|
+
workflowRunUrl?: string;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export interface CliCloudSyncResult {
|
|
44
|
+
ok: boolean;
|
|
45
|
+
synced: boolean;
|
|
46
|
+
autoRegistered: boolean;
|
|
47
|
+
domainId?: string | null;
|
|
48
|
+
hostname: string;
|
|
49
|
+
planTier: string;
|
|
50
|
+
brandTruthVerified?: boolean;
|
|
51
|
+
brandTruthSummary?: string | null;
|
|
52
|
+
dashboardUrl: string;
|
|
53
|
+
reason?: string;
|
|
54
|
+
message?: string;
|
|
55
|
+
}
|
|
56
|
+
export declare function resolveVerisourceCloudCredentials(params: {
|
|
57
|
+
explicitApiKey?: string;
|
|
58
|
+
explicitApiOrigin?: string;
|
|
59
|
+
cwd?: string;
|
|
60
|
+
}): {
|
|
61
|
+
apiKey: string | null;
|
|
62
|
+
apiOrigin: string;
|
|
63
|
+
};
|
|
64
|
+
export declare function saveVerisourceCloudCredentials(params: {
|
|
65
|
+
apiKey: string;
|
|
66
|
+
apiOrigin?: string;
|
|
67
|
+
cwd?: string;
|
|
68
|
+
}): string;
|
|
69
|
+
export declare function clearVerisourceCloudCredentials(cwd?: string): string;
|
|
70
|
+
export declare function collectGitEnvironmentMeta(): CliCloudSyncPayload['gitMeta'];
|
|
71
|
+
export declare function verifyCliCloudAuth(params: {
|
|
72
|
+
apiKey: string;
|
|
73
|
+
apiOrigin?: string;
|
|
74
|
+
}): Promise<CliCloudStatusResponse>;
|
|
75
|
+
export declare function pushCliRunToCloud(params: {
|
|
76
|
+
apiKey: string;
|
|
77
|
+
apiOrigin?: string;
|
|
78
|
+
payload: CliCloudSyncPayload;
|
|
79
|
+
}): Promise<CliCloudSyncResult>;
|
package/dist/cloud.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { loadLocalProviderConfig, saveLocalProviderConfig, } from './store.js';
|
|
2
|
+
export function resolveVerisourceCloudCredentials(params) {
|
|
3
|
+
const cwd = params.cwd ?? process.cwd();
|
|
4
|
+
const cfg = loadLocalProviderConfig(cwd);
|
|
5
|
+
const rawKey = (params.explicitApiKey ??
|
|
6
|
+
process.env.VERISOURCE_API_KEY ??
|
|
7
|
+
cfg.verisourceCloud?.apiKey ??
|
|
8
|
+
'')
|
|
9
|
+
.trim()
|
|
10
|
+
.replace(/^Bearer\s+/i, '')
|
|
11
|
+
.trim();
|
|
12
|
+
const rawOrigin = (params.explicitApiOrigin ??
|
|
13
|
+
process.env.VERISOURCE_API_ORIGIN ??
|
|
14
|
+
cfg.verisourceCloud?.apiOrigin ??
|
|
15
|
+
'https://verisource.dev')
|
|
16
|
+
.trim()
|
|
17
|
+
.replace(/\/+$/, '');
|
|
18
|
+
return {
|
|
19
|
+
apiKey: rawKey || null,
|
|
20
|
+
apiOrigin: rawOrigin || 'https://verisource.dev',
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export function saveVerisourceCloudCredentials(params) {
|
|
24
|
+
const cwd = params.cwd ?? process.cwd();
|
|
25
|
+
const existing = loadLocalProviderConfig(cwd);
|
|
26
|
+
return saveLocalProviderConfig({
|
|
27
|
+
...existing,
|
|
28
|
+
verisourceCloud: {
|
|
29
|
+
apiKey: params.apiKey.trim(),
|
|
30
|
+
...(params.apiOrigin
|
|
31
|
+
? { apiOrigin: params.apiOrigin.trim().replace(/\/+$/, '') }
|
|
32
|
+
: {}),
|
|
33
|
+
},
|
|
34
|
+
}, cwd);
|
|
35
|
+
}
|
|
36
|
+
export function clearVerisourceCloudCredentials(cwd = process.cwd()) {
|
|
37
|
+
const existing = loadLocalProviderConfig(cwd);
|
|
38
|
+
delete existing.verisourceCloud;
|
|
39
|
+
return saveLocalProviderConfig(existing, cwd);
|
|
40
|
+
}
|
|
41
|
+
export function collectGitEnvironmentMeta() {
|
|
42
|
+
const sha = process.env.GITHUB_SHA?.trim() || process.env.VERCEL_GIT_COMMIT_SHA?.trim();
|
|
43
|
+
const branch = process.env.GITHUB_REF_NAME?.trim() ||
|
|
44
|
+
process.env.VERCEL_GIT_COMMIT_REF?.trim();
|
|
45
|
+
const repo = process.env.GITHUB_REPOSITORY?.trim();
|
|
46
|
+
const serverUrl = process.env.GITHUB_SERVER_URL?.trim() || 'https://github.com';
|
|
47
|
+
const runId = process.env.GITHUB_RUN_ID?.trim();
|
|
48
|
+
const workflowRunUrl = repo && runId ? `${serverUrl}/${repo}/actions/runs/${runId}` : undefined;
|
|
49
|
+
if (!sha && !branch && !repo)
|
|
50
|
+
return undefined;
|
|
51
|
+
return {
|
|
52
|
+
...(sha ? { sha } : {}),
|
|
53
|
+
...(branch ? { branch } : {}),
|
|
54
|
+
...(repo ? { repo } : {}),
|
|
55
|
+
...(workflowRunUrl ? { workflowRunUrl } : {}),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export async function verifyCliCloudAuth(params) {
|
|
59
|
+
const origin = (params.apiOrigin ?? 'https://verisource.dev').replace(/\/+$/, '');
|
|
60
|
+
const res = await fetch(`${origin}/api/cli/sync`, {
|
|
61
|
+
method: 'GET',
|
|
62
|
+
headers: {
|
|
63
|
+
Authorization: `Bearer ${params.apiKey.trim()}`,
|
|
64
|
+
Accept: 'application/json',
|
|
65
|
+
},
|
|
66
|
+
signal: AbortSignal.timeout(10000),
|
|
67
|
+
});
|
|
68
|
+
const body = (await res.json().catch(() => ({})));
|
|
69
|
+
if (!res.ok || !body.authenticated) {
|
|
70
|
+
throw new Error(body.message ??
|
|
71
|
+
`Cloud authentication failed (HTTP ${res.status} ${body.error ?? ''}).`.trim());
|
|
72
|
+
}
|
|
73
|
+
return body;
|
|
74
|
+
}
|
|
75
|
+
export async function pushCliRunToCloud(params) {
|
|
76
|
+
const origin = (params.apiOrigin ?? 'https://verisource.dev').replace(/\/+$/, '');
|
|
77
|
+
const res = await fetch(`${origin}/api/cli/sync`, {
|
|
78
|
+
method: 'POST',
|
|
79
|
+
headers: {
|
|
80
|
+
Authorization: `Bearer ${params.apiKey.trim()}`,
|
|
81
|
+
'Content-Type': 'application/json',
|
|
82
|
+
Accept: 'application/json',
|
|
83
|
+
},
|
|
84
|
+
body: JSON.stringify(params.payload),
|
|
85
|
+
signal: AbortSignal.timeout(15000),
|
|
86
|
+
});
|
|
87
|
+
const body = (await res.json().catch(() => ({})));
|
|
88
|
+
if (!res.ok || !body.ok) {
|
|
89
|
+
throw new Error(body.message ??
|
|
90
|
+
`Cloud sync failed (HTTP ${res.status} ${body.error ?? ''}).`.trim());
|
|
91
|
+
}
|
|
92
|
+
return body;
|
|
93
|
+
}
|