forked from angular/web-codegen-scorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve-testing-worker.ts
More file actions
99 lines (93 loc) · 2.99 KB
/
serve-testing-worker.ts
File metadata and controls
99 lines (93 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { ChildProcess, fork } from 'node:child_process';
import path from 'node:path';
import { Environment } from '../configuration/environment.js';
import { ProgressLogger } from '../progress/progress-logger.js';
import { RootPromptDefinition } from '../shared-interfaces.js';
import { killChildProcessGracefully } from '../utils/kill-gracefully.js';
import {
ServeTestingResult,
ServeTestingWorkerMessage,
ServeTestingWorkerResponseMessage,
} from '../workers/serve-testing/worker-types.js';
import { EvalID, Gateway } from './gateway.js';
import { BrowserAgentTaskInput } from '../testing/browser-agent/models.js';
import PQueue from 'p-queue';
/** Attempts to run & test an eval app. */
export async function serveAndTestApp(
evalID: EvalID,
gateway: Gateway<Environment>,
appDirectoryPath: string,
env: Environment,
rootPromptDef: RootPromptDefinition,
workerConcurrencyQueue: PQueue,
abortSignal: AbortSignal,
progress: ProgressLogger,
skipScreenshots: boolean,
skipAxeTesting: boolean,
enableAutoCsp: boolean,
userJourneyAgentTaskInput?: BrowserAgentTaskInput
): Promise<ServeTestingResult> {
progress.log(rootPromptDef, 'serve-testing', `Testing the app`);
const result = await gateway.serveBuild(
evalID,
env,
appDirectoryPath,
rootPromptDef,
progress,
async (serveUrl) => {
const serveParams: ServeTestingWorkerMessage = {
serveUrl,
appName: rootPromptDef.name,
enableAutoCsp,
includeAxeTesting: skipAxeTesting === false,
takeScreenshots: skipScreenshots === false,
userJourneyAgentTaskInput,
};
return await workerConcurrencyQueue.add(
() =>
new Promise<ServeTestingResult>((resolve, reject) => {
const child: ChildProcess = fork(
path.resolve(
import.meta.dirname,
'../workers/serve-testing/worker.js'
),
{ signal: abortSignal }
);
child.send(serveParams);
child.on(
'message',
async (result: ServeTestingWorkerResponseMessage) => {
if (result.type === 'result') {
await killChildProcessGracefully(child);
resolve(result.payload);
} else {
progress.log(
rootPromptDef,
result.payload.state,
result.payload.message,
result.payload.details
);
}
}
);
child.on('error', async (err) => {
await killChildProcessGracefully(child);
reject(err);
});
}),
{ throwOnTimeout: true }
);
}
);
if (result.errorMessage === undefined) {
progress.log(rootPromptDef, 'success', 'Testing is successful');
} else {
progress.log(
rootPromptDef,
'error',
'Testing has failed',
result.errorMessage
);
}
return result;
}