@activate-core/test-forge 2.7.1 → 2.9.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/CHANGELOG.md +26 -0
- package/README.md +21 -0
- package/dist/cli/index.js +10 -3
- package/dist/core/docs/discover.js +15 -0
- package/dist/core/docs/doc-origin.js +42 -0
- package/dist/core/docs/reference.js +18 -1
- package/dist/core/gates/validate.js +8 -0
- package/dist/core/generate/actions.js +200 -0
- package/dist/core/generate/complete-file.js +43 -0
- package/dist/core/generate/harness.js +57 -14
- package/dist/core/generate/run-generate.js +58 -11
- package/dist/core/generate/test-blocks.js +36 -2
- package/dist/core/generate/update-journey.js +23 -3
- package/dist/core/project/preflight.js +69 -0
- package/dist/core/project/update.js +120 -14
- package/dist/core/prompts/blocks.js +17 -0
- package/package.json +1 -1
- package/reporters/_shared.cjs +33 -2
- package/reporters/http.cjs +96 -0
- package/reporters/interface.cjs +25 -2
- package/reporters/jest-reporter.cjs +3 -1
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// TestForge runtime — the testkit's call(), the same in every project.
|
|
2
|
+
//
|
|
3
|
+
// What is the same everywhere lives here as code, not in each AI-written
|
|
4
|
+
// testkit: a header the test gives wins over the role's token, and a 429 from
|
|
5
|
+
// the API's rate limiter is cleared or waited out instead of failing the test.
|
|
6
|
+
// The testkit supplies only what is the product's own: where the server is,
|
|
7
|
+
// how a role gets a token, how to clear the limiter, and (for a Node server it
|
|
8
|
+
// drives in-process) how to send a request.
|
|
9
|
+
'use strict';
|
|
10
|
+
const http = require('http');
|
|
11
|
+
const https = require('https');
|
|
12
|
+
|
|
13
|
+
/** Plain HTTP with Node built-ins; JSON in and out when it is JSON. */
|
|
14
|
+
function httpSend(method, url, { headers = {}, body } = {}) {
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
const u = new URL(url);
|
|
17
|
+
const lib = u.protocol === 'https:' ? https : http;
|
|
18
|
+
const payload = body === undefined ? undefined : (typeof body === 'string' ? body : JSON.stringify(body));
|
|
19
|
+
const h = { Accept: 'application/json', ...headers };
|
|
20
|
+
if (payload !== undefined) {
|
|
21
|
+
if (!Object.keys(h).some((k) => k.toLowerCase() === 'content-type')) h['Content-Type'] = 'application/json';
|
|
22
|
+
h['Content-Length'] = Buffer.byteLength(payload);
|
|
23
|
+
}
|
|
24
|
+
const req = lib.request(u, { method, headers: h, timeout: 30000 }, (res) => {
|
|
25
|
+
const chunks = [];
|
|
26
|
+
res.on('data', (c) => chunks.push(c));
|
|
27
|
+
res.on('end', () => {
|
|
28
|
+
const text = Buffer.concat(chunks).toString('utf8');
|
|
29
|
+
let json;
|
|
30
|
+
try { json = text ? JSON.parse(text) : undefined; } catch (_) { json = undefined; }
|
|
31
|
+
resolve({ status: res.statusCode, headers: res.headers, body: json !== undefined ? json : text });
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
req.on('timeout', () => req.destroy(new Error(`testkit: ${method} ${url} timed out`)));
|
|
35
|
+
req.on('error', reject);
|
|
36
|
+
if (payload !== undefined) req.write(payload);
|
|
37
|
+
req.end();
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* call(method, path, { body, headers, role }) → { status, headers, body }.
|
|
45
|
+
* url(path) the full URL of a path (the product's prefix rules)
|
|
46
|
+
* ensureToken(role, { force }) a token the product accepts for that role
|
|
47
|
+
* resetRateLimits() clears the limiter, resolves how many entries it cleared (optional)
|
|
48
|
+
* send(method, url, { headers, body }) the transport (default: HTTP)
|
|
49
|
+
* refused(status) a status that means "token not accepted" (default: 401)
|
|
50
|
+
*/
|
|
51
|
+
function makeCall({ url, ensureToken, resetRateLimits, send = httpSend, refused = (s) => s === 401, limitMs = 60000 }) {
|
|
52
|
+
if (typeof url !== 'function' || typeof ensureToken !== 'function') throw new Error('makeCall: url and ensureToken are required');
|
|
53
|
+
return async function call(method, path, { body, headers = {}, role = 'customer' } = {}) {
|
|
54
|
+
const m = String(method).toUpperCase();
|
|
55
|
+
const target = url(path);
|
|
56
|
+
// A header the test gives is sent exactly as given ('' = none) and never
|
|
57
|
+
// replaced, retried or re-minted: an old or forged token is what it tests.
|
|
58
|
+
const authKey = Object.keys(headers).find((k) => k.toLowerCase() === 'authorization');
|
|
59
|
+
const own = authKey !== undefined || role === null;
|
|
60
|
+
const given = { ...headers };
|
|
61
|
+
if (authKey !== undefined && !given[authKey]) delete given[authKey];
|
|
62
|
+
const once = async (force) => {
|
|
63
|
+
if (own) return send(m, target, { headers: given, body });
|
|
64
|
+
const tok = await ensureToken(role, { force });
|
|
65
|
+
return send(m, target, { headers: tok ? { ...given, Authorization: `Bearer ${tok}` } : given, body });
|
|
66
|
+
};
|
|
67
|
+
let res = await once(false);
|
|
68
|
+
if (!own && refused(res.status)) res = await once(true);
|
|
69
|
+
const until = Date.now() + limitMs;
|
|
70
|
+
while (res.status === 429 && Date.now() < until) {
|
|
71
|
+
let cleared = 0;
|
|
72
|
+
try { cleared = resetRateLimits ? Number(await resetRateLimits()) || 0 : 0; } catch (_) { cleared = 0; }
|
|
73
|
+
if (!cleared) {
|
|
74
|
+
const after = Number((res.headers || {})['retry-after']);
|
|
75
|
+
await sleep(Math.max(0, Math.min(until - Date.now(), 1000 * (after > 0 ? after : 2))));
|
|
76
|
+
}
|
|
77
|
+
res = await once(false);
|
|
78
|
+
}
|
|
79
|
+
return { status: res.status, headers: res.headers, body: res.body };
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The tables DERIVED_STATE names really exist: a wrong name (a model's class
|
|
85
|
+
* name instead of its table) would quietly derive nothing. query(sql) runs SQL.
|
|
86
|
+
*/
|
|
87
|
+
async function verifyDerivedState(derived, query) {
|
|
88
|
+
const missing = [];
|
|
89
|
+
for (const t of Object.keys(derived || {})) {
|
|
90
|
+
if (!/^[A-Za-z_][\w.]*$/.test(t)) { missing.push(t); continue; }
|
|
91
|
+
try { await query(`SELECT 1 FROM ${t} WHERE 1 = 0`); } catch (_) { missing.push(t); }
|
|
92
|
+
}
|
|
93
|
+
if (missing.length) throw new Error(`testkit: DERIVED_STATE names ${missing.join(', ')}, not a table in the database — the derived rows are never made`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
module.exports = { makeCall, httpSend, verifyDerivedState };
|
package/reporters/interface.cjs
CHANGED
|
@@ -105,7 +105,11 @@ function usersOf(file, name) {
|
|
|
105
105
|
});
|
|
106
106
|
const word = new RegExp(`\\b${v.replace(/\$/g, '\\$')}\\b`);
|
|
107
107
|
// A name inside a string ('redeem points URL') is not a use of the constant URL.
|
|
108
|
-
|
|
108
|
+
// A `${URL}` inside a template literal is a use: keep the interpolations.
|
|
109
|
+
const code = (t) => String(t)
|
|
110
|
+
.replace(/`(?:\\.|[^`\\])*`/g, (m) => (m.match(/\$\{[^}]*\}/g) || []).join(' '))
|
|
111
|
+
.replace(/(['"])(?:\\.|(?!\1)[^\\])*\1/g, '""');
|
|
112
|
+
const uses = { test: (t) => word.test(code(t)) };
|
|
109
113
|
const inTest = (i) => tests.some((t) => i >= t.start && i < t.end);
|
|
110
114
|
if (lines.some((l, i) => i !== declLine && !inTest(i) && uses.test(l))) return undefined;
|
|
111
115
|
return tests.filter((t) => uses.test(t.text));
|
|
@@ -192,4 +196,23 @@ function fillPath(template, ...values) {
|
|
|
192
196
|
return String(template).replace(/\{[^}/]+\}|(?<=\/):[A-Za-z_]\w*/g, (m) => (i < values.length ? encodeURIComponent(String(values[i++])) : m));
|
|
193
197
|
}
|
|
194
198
|
|
|
195
|
-
|
|
199
|
+
/**
|
|
200
|
+
* An action the product does not offer yet (actions module, A7): calling it
|
|
201
|
+
* makes the test wait, saying what was looked for.
|
|
202
|
+
*/
|
|
203
|
+
function waiting(action, why) {
|
|
204
|
+
const message = `[testforge:waiting] "${action}" (action) is not in the product yet${why ? ` — ${why}` : ''}: this test runs for real once it is there.`;
|
|
205
|
+
return async () => { throw new Error(message); };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* The product refused an action's own request (actions module, A6): either the
|
|
210
|
+
* action sends the wrong thing or the product is at fault. The reporters file it
|
|
211
|
+
* apart from a rule the test checks, and generate builds the action again.
|
|
212
|
+
*/
|
|
213
|
+
function refused(action, method, path, res) {
|
|
214
|
+
const body = res && typeof res.body === 'string' ? res.body : JSON.stringify(res && res.body);
|
|
215
|
+
return new Error(`[testforge:action] ${action}: ${method} ${path} answered ${res && res.status}: ${String(body).slice(0, 300)}`);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
module.exports = { need, fillPath, waiting, refused, _usersOf: usersOf };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// TestForge Jest reporter — auto-publishes the run to the dashboard on completion.
|
|
2
2
|
// Add to jest.config.js:
|
|
3
3
|
// reporters: ['default', ['<abs>/reporters/jest-reporter.cjs', { category: 'api' }]]
|
|
4
|
-
const { cleanError, sig, publish, testChecks, expectation, failureDetail, findRoot, suiteName, testRoot, reviewVerdicts, notTheProduct, plainTitle, recordPassed, recordSummary } = require('./_shared.cjs');
|
|
4
|
+
const { cleanError, sig, publish, testChecks, expectation, failureDetail, findRoot, suiteName, testRoot, reviewVerdicts, notTheProduct, plainTitle, recordPassed, recordSummary, recordActionFailures } = require('./_shared.cjs');
|
|
5
5
|
|
|
6
6
|
module.exports = class TestForgeJestReporter {
|
|
7
7
|
constructor(_globalConfig, options) {
|
|
@@ -38,6 +38,8 @@ module.exports = class TestForgeJestReporter {
|
|
|
38
38
|
}
|
|
39
39
|
recordPassed(this.root, (results.testResults || []).flatMap((f) =>
|
|
40
40
|
(f.testResults || f.assertionResults || []).filter((a) => a.status === 'passed').map((a) => ({ file: f.testFilePath, title: a.title }))));
|
|
41
|
+
recordActionFailures(this.root, (results.testResults || []).flatMap((f) =>
|
|
42
|
+
(f.testResults || f.assertionResults || []).filter((a) => a.status === 'failed').map((a) => (a.failureMessages || [])[0])));
|
|
41
43
|
const durationSec = Math.round((results.startTime ? Date.now() - results.startTime : 0) / 1000);
|
|
42
44
|
const list = Object.values(suites);
|
|
43
45
|
recordSummary(this.root, this.category, list);
|