@specter-automation/test 1.0.150 → 1.0.151

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.
@@ -0,0 +1,7 @@
1
+ export declare function setActiveWorld(world: any): void;
2
+ export declare function getActiveWorld(): any;
3
+ export declare function setAiDiagnosticsFormatter(fn: (diag: any) => string): void;
4
+ export declare function captureSpecterFailure(world: any, testInfo: any, source?: string): Promise<void>;
5
+ export declare function extractFunctionParams(fn: any): string;
6
+ export declare function wrapTestFunction(fn: any): any;
7
+ export declare function wrapTestInstance<T extends (...args: any[]) => any>(targetTest: T): T;
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setActiveWorld = setActiveWorld;
4
+ exports.getActiveWorld = getActiveWorld;
5
+ exports.setAiDiagnosticsFormatter = setAiDiagnosticsFormatter;
6
+ exports.captureSpecterFailure = captureSpecterFailure;
7
+ exports.extractFunctionParams = extractFunctionParams;
8
+ exports.wrapTestFunction = wrapTestFunction;
9
+ exports.wrapTestInstance = wrapTestInstance;
10
+ let activeWorld = null;
11
+ let diagnosticsFormatter = null;
12
+ function setActiveWorld(world) {
13
+ activeWorld = world;
14
+ }
15
+ function getActiveWorld() {
16
+ return activeWorld;
17
+ }
18
+ function setAiDiagnosticsFormatter(fn) {
19
+ diagnosticsFormatter = fn;
20
+ }
21
+ async function captureSpecterFailure(world, testInfo, source = 'test') {
22
+ if (!world || !testInfo)
23
+ return;
24
+ if (testInfo._specterFailureCaptured)
25
+ return;
26
+ testInfo._specterFailureCaptured = true;
27
+ const title = testInfo.title || 'Unknown_Test';
28
+ const safeName = `Specter_Fail_${title.replace(/[^a-zA-Z0-9]/g, '_')}`;
29
+ try {
30
+ if (typeof world.takeScreenshot === 'function') {
31
+ await world.takeScreenshot(safeName, false, { attachmentName: 'Unreal Engine Viewport' });
32
+ }
33
+ }
34
+ catch (e) {
35
+ console.error(`[Specter] Failed to capture error screenshot (${source}):`, e);
36
+ }
37
+ try {
38
+ if (typeof world.getDiagnostics === 'function') {
39
+ const diagnostics = await world.getDiagnostics({ limit: 35 });
40
+ if (diagnostics) {
41
+ const formattedSummary = diagnosticsFormatter ? diagnosticsFormatter(diagnostics) : JSON.stringify(diagnostics, null, 2);
42
+ console.log('\n' + formattedSummary + '\n');
43
+ const alreadyAttached = testInfo.attachments?.some((a) => a.name.includes('Specter AI Diagnostics'));
44
+ if (!alreadyAttached && testInfo.attachments) {
45
+ testInfo.attachments.push({
46
+ name: 'Specter AI Diagnostics (JSON)',
47
+ contentType: 'application/json',
48
+ body: Buffer.from(JSON.stringify(diagnostics, null, 2))
49
+ });
50
+ testInfo.attachments.push({
51
+ name: 'Specter AI Diagnostics (Markdown)',
52
+ contentType: 'text/markdown',
53
+ body: Buffer.from(formattedSummary)
54
+ });
55
+ }
56
+ }
57
+ }
58
+ }
59
+ catch (e) {
60
+ console.error(`[Specter] Failed to capture AI diagnostics on test failure (${source}):`, e);
61
+ }
62
+ }
63
+ function extractFunctionParams(fn) {
64
+ const str = fn.toString();
65
+ const match = str.match(/^(?:async\s*)?(?:function\s*)?\s*\(([^)]*)\)/);
66
+ if (match)
67
+ return match[1];
68
+ const arrowMatch = str.match(/^(?:async\s*)?([a-zA-Z0-9_{}\s,]+)=>/);
69
+ if (arrowMatch)
70
+ return arrowMatch[1];
71
+ return "fixtures, testInfo";
72
+ }
73
+ function wrapTestFunction(fn) {
74
+ if (typeof fn !== "function")
75
+ return fn;
76
+ const params = extractFunctionParams(fn);
77
+ try {
78
+ const wrapperFactory = new Function("fn", "captureSpecterFailure", "getActiveWorld", `
79
+ return async function(${params}) {
80
+ try {
81
+ return await fn.apply(this, arguments);
82
+ } catch (err) {
83
+ const testInfo = (arguments.length > 1 && arguments[1]) ? arguments[1] : undefined;
84
+ const world = (testInfo && testInfo._specterWorld) || (arguments[0] && arguments[0].world) || (arguments[0] && arguments[0].server && arguments[0].server.world) || getActiveWorld();
85
+ if (world && testInfo) {
86
+ try {
87
+ await captureSpecterFailure(world, testInfo, 'test-body');
88
+ } catch (e) {}
89
+ }
90
+ throw err;
91
+ }
92
+ };
93
+ `);
94
+ return wrapperFactory(fn, captureSpecterFailure, getActiveWorld);
95
+ }
96
+ catch (e) {
97
+ return fn;
98
+ }
99
+ }
100
+ function wrapTestInstance(targetTest) {
101
+ const wrapCall = (originalFn) => {
102
+ return function (title, ...rest) {
103
+ if (rest.length > 0 && typeof rest[rest.length - 1] === "function") {
104
+ rest[rest.length - 1] = wrapTestFunction(rest[rest.length - 1]);
105
+ }
106
+ return originalFn.apply(this, [title, ...rest]);
107
+ };
108
+ };
109
+ const wrapped = wrapCall(targetTest);
110
+ Object.setPrototypeOf(wrapped, targetTest);
111
+ for (const key of Object.getOwnPropertyNames(targetTest)) {
112
+ const desc = Object.getOwnPropertyDescriptor(targetTest, key);
113
+ if (desc)
114
+ Object.defineProperty(wrapped, key, desc);
115
+ }
116
+ for (const modifier of ["only", "skip", "fixme", "fail"]) {
117
+ if (typeof targetTest[modifier] === "function") {
118
+ wrapped[modifier] = wrapCall(targetTest[modifier]);
119
+ Object.setPrototypeOf(wrapped[modifier], targetTest[modifier]);
120
+ }
121
+ }
122
+ const origExtend = targetTest.extend.bind(targetTest);
123
+ wrapped.extend = function (...args) {
124
+ const extended = origExtend(...args);
125
+ return wrapTestInstance(extended);
126
+ };
127
+ return wrapped;
128
+ }
@@ -40,5 +40,6 @@ type CreateWorldOptions = {
40
40
  autoJoinHost?: boolean;
41
41
  launchClient?: boolean;
42
42
  };
43
- export declare const test: import("playwright/test").TestType<import("playwright/test").PlaywrightTestArgs & import("playwright/test").PlaywrightTestOptions & SpecterTestFixtures & SpecterTestOptions, import("playwright/test").PlaywrightWorkerArgs & import("playwright/test").PlaywrightWorkerOptions & SpecterWorkerFixtures>;
43
+ declare const extendedTest: import("playwright/test").TestType<import("playwright/test").PlaywrightTestArgs & import("playwright/test").PlaywrightTestOptions & SpecterTestFixtures & SpecterTestOptions, import("playwright/test").PlaywrightWorkerArgs & import("playwright/test").PlaywrightWorkerOptions & SpecterWorkerFixtures>;
44
+ export declare const test: typeof extendedTest;
44
45
  export {};
@@ -44,9 +44,10 @@ const material_matchers_1 = require("../matchers/material-matchers");
44
44
  const ai_matchers_1 = require("../matchers/ai-matchers");
45
45
  const performance_matchers_1 = require("../matchers/performance-matchers");
46
46
  const visual_matchers_1 = require("../matchers/visual-matchers");
47
+ const failure_capture_1 = require("./failure-capture");
47
48
  exports.expect = (0, test_1.mergeExpects)(gas_matchers_1.gasExpect, actor_matchers_1.actorExpect, locator_matchers_1.locatorExpect, widget_matchers_1.widgetExpect, material_matchers_1.materialExpect, ai_matchers_1.aiExpect, performance_matchers_1.performanceExpect, visual_matchers_1.visualExpect);
48
49
  // 2. Define the scopes in the extend block
49
- exports.test = test_1.test.extend({
50
+ const extendedTest = test_1.test.extend({
50
51
  // OPTIONS FIXTURES: Configurable via playwright.config.ts `use: { ... }` or `test.use({ ... })`
51
52
  timeDilation: [1.0, { option: true }],
52
53
  fixedDeltaSeconds: [0, { option: true }],
@@ -70,22 +71,29 @@ exports.test = test_1.test.extend({
70
71
  const activeDilation = (envDilation !== undefined && !isNaN(envDilation) && envDilation > 0) ? envDilation : (timeDilation || 1.0);
71
72
  const world = new specter_client_1.SpecterWorld(specter);
72
73
  world.timeDilation = activeDilation;
74
+ testInfo._specterWorld = world;
75
+ (0, failure_capture_1.setActiveWorld)(world);
73
76
  if (activeDilation !== 1.0) {
74
77
  await world.setTimeScale(activeDilation);
75
78
  }
76
79
  if (fixedDeltaSeconds && fixedDeltaSeconds > 0) {
77
80
  await world.console(`r.FixedDeltaTime ${fixedDeltaSeconds}`);
78
81
  }
79
- await use(world);
80
- // Teardown: Reset fixed delta time if configured
81
- if (fixedDeltaSeconds && fixedDeltaSeconds > 0) {
82
- try {
83
- await world.console(`r.FixedDeltaTime 0`);
82
+ try {
83
+ await use(world);
84
+ }
85
+ finally {
86
+ (0, failure_capture_1.setActiveWorld)(null);
87
+ // Teardown: Reset fixed delta time if configured
88
+ if (fixedDeltaSeconds && fixedDeltaSeconds > 0) {
89
+ try {
90
+ await world.console(`r.FixedDeltaTime 0`);
91
+ }
92
+ catch (e) { }
84
93
  }
85
- catch (e) { }
94
+ // Wipe the level clean for the next test in the queue (also restores slomo 1.0 if dilated)
95
+ await world.cleanup();
86
96
  }
87
- // Wipe the level clean for the next test in the queue (also restores slomo 1.0 if dilated)
88
- await world.cleanup();
89
97
  },
90
98
  // THE MULTIPLAYER FACTORY FIXTURE
91
99
  createWorld: async ({ timeDilation }, use) => {
@@ -210,41 +218,18 @@ exports.test = test_1.test.extend({
210
218
  }
211
219
  },
212
220
  autoScreenshot: [async ({ world }, use, testInfo) => {
213
- // Yield control to run the actual test
214
- await use();
215
- // FIX: Catch 'failed', 'timedOut', and 'interrupted' statuses
216
- if (testInfo.status !== 'passed' && testInfo.status !== 'skipped') {
217
- const safeName = `Specter_Fail_${testInfo.title.replace(/[^a-zA-Z0-9]/g, '_')}`;
218
- try {
219
- // Attached by takeScreenshot itself
220
- await world.takeScreenshot(safeName, true, { attachmentName: 'Unreal Engine Viewport' });
221
- }
222
- catch (e) {
223
- console.error("[Specter] Failed to capture error screenshot:", e);
224
- }
225
- try {
226
- const diagnostics = await world.getDiagnostics({ limit: 35 });
227
- if (diagnostics) {
228
- const formattedSummary = (0, gas_matchers_1.formatAiDiagnostics)(diagnostics);
229
- console.log('\n' + formattedSummary + '\n');
230
- const alreadyAttached = testInfo.attachments.some(a => a.name.includes('Specter AI Diagnostics'));
231
- if (!alreadyAttached) {
232
- testInfo.attachments.push({
233
- name: 'Specter AI Diagnostics (JSON)',
234
- contentType: 'application/json',
235
- body: Buffer.from(JSON.stringify(diagnostics, null, 2))
236
- });
237
- testInfo.attachments.push({
238
- name: 'Specter AI Diagnostics (Markdown)',
239
- contentType: 'text/markdown',
240
- body: Buffer.from(formattedSummary)
241
- });
242
- }
221
+ try {
222
+ await use();
223
+ }
224
+ finally {
225
+ // FIX: Catch 'failed', 'timedOut', and 'interrupted' statuses if not already captured earlier
226
+ if (testInfo.status !== 'passed' && testInfo.status !== 'skipped') {
227
+ if (!testInfo._specterFailureCaptured) {
228
+ await (0, failure_capture_1.captureSpecterFailure)(world, testInfo, 'autoScreenshot-fallback');
243
229
  }
244
230
  }
245
- catch (e) {
246
- console.error("[Specter] Failed to capture AI diagnostics on test failure:", e);
247
- }
248
231
  }
249
232
  }, { auto: true }]
250
233
  });
234
+ (0, failure_capture_1.setAiDiagnosticsFormatter)(gas_matchers_1.formatAiDiagnostics);
235
+ exports.test = (0, failure_capture_1.wrapTestInstance)(extendedTest);
@@ -7,6 +7,7 @@ exports.instrumentMatchers = instrumentMatchers;
7
7
  const test_1 = __importDefault(require("@playwright/test"));
8
8
  const specter_matcher_report_1 = require("../core/specter-matcher-report");
9
9
  const specter_perf_1 = require("../core/specter-perf");
10
+ const failure_capture_1 = require("../core/failure-capture");
10
11
  const MAX_ARG_LENGTH = 60;
11
12
  const MAX_ARGS_LENGTH = 140;
12
13
  const MAX_REPORT_VALUE_LENGTH = 4000;
@@ -27,17 +28,36 @@ function instrumentMatchers(matchers) {
27
28
  // Keep the client tag ("[Server] ", "[Client 1] ") in front, as the old per-matcher titles did.
28
29
  const title = `${clientTagOf(receiver)}${isNot ? 'not.' : ''}${name}(${formatArgs(args)}) @ ${describe(receiver)}`;
29
30
  const result = await test_1.default.step(title, async () => matcher.call(this, receiver, ...args));
31
+ const passed = isNot ? !result.pass : result.pass;
30
32
  // Back in the expect step's context here, so the attachment lands on it.
31
33
  recordResult({
32
34
  matcher: name,
33
35
  isNot,
34
- pass: isNot ? !result.pass : result.pass,
36
+ pass: passed,
35
37
  locator: selectorOf(receiver),
36
38
  locatorLabel: labelOf(receiver),
37
39
  client: clientTagOf(receiver).replace(/^\[|\]\s*$/g, '') || undefined,
38
40
  expected: result.expected,
39
41
  actual: result.actual,
40
42
  });
43
+ // If the matcher failed, capture the engine failure screenshot & diagnostics immediately
44
+ // before the assertion error throws and before any afterEach cleanup hooks run.
45
+ if (!passed) {
46
+ let info;
47
+ try {
48
+ info = test_1.default.info();
49
+ }
50
+ catch { }
51
+ const targetWorld = info?._specterWorld || receiver?.world || (0, failure_capture_1.getActiveWorld)();
52
+ if (targetWorld && info) {
53
+ try {
54
+ await (0, failure_capture_1.captureSpecterFailure)(targetWorld, info, 'matcher');
55
+ }
56
+ catch (e) {
57
+ console.error('[Specter] Error capturing failure state in matcher:', e);
58
+ }
59
+ }
60
+ }
41
61
  return result;
42
62
  };
43
63
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@specter-automation/test",
3
- "version": "1.0.150",
3
+ "version": "1.0.151",
4
4
  "description": "Playwright integration for Unreal Engine automation via Specter",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",