@gate-forge/cli 0.7.0 → 0.8.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 +511 -51
- package/dist/adapter-audit.d.ts +154 -0
- package/dist/adapter-audit.d.ts.map +1 -0
- package/dist/adapter-audit.js +531 -0
- package/dist/adapter-audit.js.map +1 -0
- package/dist/adapter-scaffold.d.ts +66 -0
- package/dist/adapter-scaffold.d.ts.map +1 -0
- package/dist/adapter-scaffold.js +353 -0
- package/dist/adapter-scaffold.js.map +1 -0
- package/dist/adopted-baseline.d.ts +10 -0
- package/dist/adopted-baseline.d.ts.map +1 -1
- package/dist/adopted-baseline.js +18 -1
- package/dist/adopted-baseline.js.map +1 -1
- package/dist/args.d.ts +15 -0
- package/dist/args.d.ts.map +1 -1
- package/dist/args.js +59 -2
- package/dist/args.js.map +1 -1
- package/dist/behavior-setup.d.ts +239 -0
- package/dist/behavior-setup.d.ts.map +1 -0
- package/dist/behavior-setup.js +738 -0
- package/dist/behavior-setup.js.map +1 -0
- package/dist/broker.d.ts.map +1 -1
- package/dist/broker.js +28 -2
- package/dist/broker.js.map +1 -1
- package/dist/cache-exclusions.d.ts +45 -0
- package/dist/cache-exclusions.d.ts.map +1 -0
- package/dist/cache-exclusions.js +205 -0
- package/dist/cache-exclusions.js.map +1 -0
- package/dist/candidate-tree.d.ts +86 -14
- package/dist/candidate-tree.d.ts.map +1 -1
- package/dist/candidate-tree.js +236 -62
- package/dist/candidate-tree.js.map +1 -1
- package/dist/ci-witnessed-template.d.ts +43 -0
- package/dist/ci-witnessed-template.d.ts.map +1 -0
- package/dist/ci-witnessed-template.js +283 -0
- package/dist/ci-witnessed-template.js.map +1 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +31 -4
- package/dist/cli.js.map +1 -1
- package/dist/commands/adapters.d.ts +15 -0
- package/dist/commands/adapters.d.ts.map +1 -0
- package/dist/commands/adapters.js +284 -0
- package/dist/commands/adapters.js.map +1 -0
- package/dist/commands/adopt.d.ts.map +1 -1
- package/dist/commands/adopt.js +9 -1
- package/dist/commands/adopt.js.map +1 -1
- package/dist/commands/baseline.d.ts.map +1 -1
- package/dist/commands/baseline.js +42 -1
- package/dist/commands/baseline.js.map +1 -1
- package/dist/commands/blocking.d.ts +81 -2
- package/dist/commands/blocking.d.ts.map +1 -1
- package/dist/commands/blocking.js +336 -53
- package/dist/commands/blocking.js.map +1 -1
- package/dist/commands/check.d.ts +36 -0
- package/dist/commands/check.d.ts.map +1 -1
- package/dist/commands/check.js +1008 -62
- package/dist/commands/check.js.map +1 -1
- package/dist/commands/classify.d.ts +2 -1
- package/dist/commands/classify.d.ts.map +1 -1
- package/dist/commands/classify.js +143 -5
- package/dist/commands/classify.js.map +1 -1
- package/dist/commands/enforce.d.ts +4 -4
- package/dist/commands/enforce.d.ts.map +1 -1
- package/dist/commands/enforce.js +41 -9
- package/dist/commands/enforce.js.map +1 -1
- package/dist/commands/enforcement.d.ts +13 -1
- package/dist/commands/enforcement.d.ts.map +1 -1
- package/dist/commands/enforcement.js +800 -30
- package/dist/commands/enforcement.js.map +1 -1
- package/dist/commands/history.d.ts +12 -0
- package/dist/commands/history.d.ts.map +1 -0
- package/dist/commands/history.js +45 -0
- package/dist/commands/history.js.map +1 -0
- package/dist/commands/init-presets.d.ts +148 -0
- package/dist/commands/init-presets.d.ts.map +1 -0
- package/dist/commands/init-presets.js +194 -0
- package/dist/commands/init-presets.js.map +1 -0
- package/dist/commands/init.d.ts +7 -12
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +610 -69
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/keys.d.ts.map +1 -1
- package/dist/commands/keys.js +29 -13
- package/dist/commands/keys.js.map +1 -1
- package/dist/commands/next.d.ts.map +1 -1
- package/dist/commands/next.js +744 -15
- package/dist/commands/next.js.map +1 -1
- package/dist/commands/pre-commit.d.ts +25 -0
- package/dist/commands/pre-commit.d.ts.map +1 -1
- package/dist/commands/pre-commit.js +91 -6
- package/dist/commands/pre-commit.js.map +1 -1
- package/dist/commands/quarantine.d.ts +16 -0
- package/dist/commands/quarantine.d.ts.map +1 -0
- package/dist/commands/quarantine.js +213 -0
- package/dist/commands/quarantine.js.map +1 -0
- package/dist/commands/run.d.ts +39 -0
- package/dist/commands/run.d.ts.map +1 -0
- package/dist/commands/run.js +196 -0
- package/dist/commands/run.js.map +1 -0
- package/dist/commands/test-gates.d.ts +284 -2
- package/dist/commands/test-gates.d.ts.map +1 -1
- package/dist/commands/test-gates.js +3619 -186
- package/dist/commands/test-gates.js.map +1 -1
- package/dist/commands/tests.d.ts +1 -1
- package/dist/commands/tests.d.ts.map +1 -1
- package/dist/commands/tests.js +225 -22
- package/dist/commands/tests.js.map +1 -1
- package/dist/endpoint-compiler.d.ts.map +1 -1
- package/dist/endpoint-compiler.js +29 -8
- package/dist/endpoint-compiler.js.map +1 -1
- package/dist/engine-browser.d.ts +183 -0
- package/dist/engine-browser.d.ts.map +1 -0
- package/dist/engine-browser.js +381 -0
- package/dist/engine-browser.js.map +1 -0
- package/dist/engine-identity.d.ts +100 -0
- package/dist/engine-identity.d.ts.map +1 -0
- package/dist/engine-identity.js +182 -0
- package/dist/engine-identity.js.map +1 -0
- package/dist/evaluate.d.ts +89 -0
- package/dist/evaluate.d.ts.map +1 -1
- package/dist/evaluate.js +137 -16
- package/dist/evaluate.js.map +1 -1
- package/dist/execution.d.ts +253 -9
- package/dist/execution.d.ts.map +1 -1
- package/dist/execution.js +309 -12
- package/dist/execution.js.map +1 -1
- package/dist/git-hooks.d.ts +17 -0
- package/dist/git-hooks.d.ts.map +1 -1
- package/dist/git-hooks.js +119 -0
- package/dist/git-hooks.js.map +1 -1
- package/dist/git-objects.d.ts +60 -0
- package/dist/git-objects.d.ts.map +1 -0
- package/dist/git-objects.js +152 -0
- package/dist/git-objects.js.map +1 -0
- package/dist/history.d.ts +52 -0
- package/dist/history.d.ts.map +1 -0
- package/dist/history.js +116 -0
- package/dist/history.js.map +1 -0
- package/dist/host-load.d.ts +49 -0
- package/dist/host-load.d.ts.map +1 -0
- package/dist/host-load.js +78 -0
- package/dist/host-load.js.map +1 -0
- package/dist/input-snapshot.d.ts +63 -9
- package/dist/input-snapshot.d.ts.map +1 -1
- package/dist/input-snapshot.js +195 -17
- package/dist/input-snapshot.js.map +1 -1
- package/dist/io.d.ts +36 -0
- package/dist/io.d.ts.map +1 -1
- package/dist/io.js +21 -0
- package/dist/io.js.map +1 -1
- package/dist/mapping.d.ts +84 -1
- package/dist/mapping.d.ts.map +1 -1
- package/dist/mapping.js +276 -5
- package/dist/mapping.js.map +1 -1
- package/dist/native-freeze.d.ts +217 -0
- package/dist/native-freeze.d.ts.map +1 -0
- package/dist/native-freeze.js +332 -0
- package/dist/native-freeze.js.map +1 -0
- package/dist/pipeline.d.ts +52 -1
- package/dist/pipeline.d.ts.map +1 -1
- package/dist/pipeline.js +141 -3
- package/dist/pipeline.js.map +1 -1
- package/dist/playwright-browsers.d.ts +210 -0
- package/dist/playwright-browsers.d.ts.map +1 -0
- package/dist/playwright-browsers.js +364 -0
- package/dist/playwright-browsers.js.map +1 -0
- package/dist/plugins.d.ts +15 -5
- package/dist/plugins.d.ts.map +1 -1
- package/dist/plugins.js +59 -9
- package/dist/plugins.js.map +1 -1
- package/dist/progress.d.ts +148 -0
- package/dist/progress.d.ts.map +1 -0
- package/dist/progress.js +263 -0
- package/dist/progress.js.map +1 -0
- package/dist/providers.d.ts +26 -1
- package/dist/providers.d.ts.map +1 -1
- package/dist/providers.js +39 -0
- package/dist/providers.js.map +1 -1
- package/dist/receipts.d.ts +1 -0
- package/dist/receipts.d.ts.map +1 -1
- package/dist/receipts.js +35 -7
- package/dist/receipts.js.map +1 -1
- package/dist/repo-scan.d.ts +8 -1
- package/dist/repo-scan.d.ts.map +1 -1
- package/dist/repo-scan.js +221 -27
- package/dist/repo-scan.js.map +1 -1
- package/dist/reseal-chain.d.ts +184 -0
- package/dist/reseal-chain.d.ts.map +1 -0
- package/dist/reseal-chain.js +735 -0
- package/dist/reseal-chain.js.map +1 -0
- package/dist/reseal-evidence.d.ts +241 -0
- package/dist/reseal-evidence.d.ts.map +1 -0
- package/dist/reseal-evidence.js +319 -0
- package/dist/reseal-evidence.js.map +1 -0
- package/dist/reseal.d.ts +181 -0
- package/dist/reseal.d.ts.map +1 -0
- package/dist/reseal.js +1620 -0
- package/dist/reseal.js.map +1 -0
- package/dist/response-field-guidance.d.ts +82 -0
- package/dist/response-field-guidance.d.ts.map +1 -0
- package/dist/response-field-guidance.js +184 -0
- package/dist/response-field-guidance.js.map +1 -0
- package/dist/run-cache.d.ts +260 -0
- package/dist/run-cache.d.ts.map +1 -0
- package/dist/run-cache.js +771 -0
- package/dist/run-cache.js.map +1 -0
- package/dist/run-preflight.d.ts +77 -0
- package/dist/run-preflight.d.ts.map +1 -0
- package/dist/run-preflight.js +774 -0
- package/dist/run-preflight.js.map +1 -0
- package/dist/run-recipe.d.ts +107 -0
- package/dist/run-recipe.d.ts.map +1 -0
- package/dist/run-recipe.js +295 -0
- package/dist/run-recipe.js.map +1 -0
- package/dist/run-reliability.d.ts +126 -0
- package/dist/run-reliability.d.ts.map +1 -0
- package/dist/run-reliability.js +221 -0
- package/dist/run-reliability.js.map +1 -0
- package/dist/runtime.d.ts +15 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +11 -1
- package/dist/runtime.js.map +1 -1
- package/dist/secret-guard.d.ts +36 -0
- package/dist/secret-guard.d.ts.map +1 -0
- package/dist/secret-guard.js +63 -0
- package/dist/secret-guard.js.map +1 -0
- package/dist/singleton-guidance.d.ts +73 -0
- package/dist/singleton-guidance.d.ts.map +1 -0
- package/dist/singleton-guidance.js +118 -0
- package/dist/singleton-guidance.js.map +1 -0
- package/dist/staged-candidate.d.ts +17 -0
- package/dist/staged-candidate.d.ts.map +1 -1
- package/dist/staged-candidate.js +50 -0
- package/dist/staged-candidate.js.map +1 -1
- package/dist/state-artifacts.d.ts +70 -0
- package/dist/state-artifacts.d.ts.map +1 -0
- package/dist/state-artifacts.js +215 -0
- package/dist/state-artifacts.js.map +1 -0
- package/dist/state.d.ts +170 -2
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +248 -8
- package/dist/state.js.map +1 -1
- package/dist/verifier-keys.d.ts +33 -2
- package/dist/verifier-keys.d.ts.map +1 -1
- package/dist/verifier-keys.js +95 -9
- package/dist/verifier-keys.js.map +1 -1
- package/guides/CONNECT-YOUR-PROJECT.md +367 -0
- package/guides/QUICKSTART.md +404 -0
- package/guides/RUNNER-NEUTRAL-EVIDENCE.md +199 -0
- package/guides/TEST-ENVIRONMENT.md +1226 -0
- package/guides/UPGRADE-0.6-to-0.7.md +65 -0
- package/guides/UPGRADE-0.7-to-0.8.md +114 -0
- package/package.json +12 -10
|
@@ -0,0 +1,738 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Behavior-case setup (plan 2026-09-30 Phase 4): the setup UX that makes
|
|
3
|
+
* behavior cases usable by someone who does not know Gateforge.
|
|
4
|
+
*
|
|
5
|
+
* Two surfaces share this module, and nothing else may re-implement it:
|
|
6
|
+
*
|
|
7
|
+
* 1. `gateforge init` asks (TTY) — or is told by a flag (agent/CI) —
|
|
8
|
+
* which of the behavior packs the repository actually shows should
|
|
9
|
+
* have their cases enabled, and writes a commented skeleton with one
|
|
10
|
+
* example case per ENABLED namespace. Nothing is ever enabled
|
|
11
|
+
* silently: a non-interactive run prints the exact flag that enables
|
|
12
|
+
* the pack it found, and enables nothing.
|
|
13
|
+
* 2. `gateforge next` prints the same vocabulary instantiated for the
|
|
14
|
+
* concrete route the owner is looking at: which file, which case id,
|
|
15
|
+
* and which test-map entry — every line copy-pasteable as printed.
|
|
16
|
+
*
|
|
17
|
+
* Detection is a bounded content scan of the repository's own source
|
|
18
|
+
* files, mirroring `repo-scan.ts` (same ignore list, same size guard). It
|
|
19
|
+
* is deliberately conservative: a namespace is reported only when the
|
|
20
|
+
* repository contains the machinery that namespace's cases are ABOUT
|
|
21
|
+
* (HMAC signature verification, an authorization decision, a state
|
|
22
|
+
* transition, a request schema). A repository that shows none of them
|
|
23
|
+
* gets no behavior output at all, which keeps `init` byte-identical.
|
|
24
|
+
*/
|
|
25
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
26
|
+
import { join, relative } from 'node:path';
|
|
27
|
+
import { parse as parseYaml } from 'yaml';
|
|
28
|
+
/**
|
|
29
|
+
* The namespaces the engine drives through an HTTP request: a case in
|
|
30
|
+
* one of them is a claim about what a route answered and what changed.
|
|
31
|
+
*/
|
|
32
|
+
const HTTP_BEHAVIOR_NAMESPACES = ['webhook', 'workflow', 'auth', 'validation'];
|
|
33
|
+
/**
|
|
34
|
+
* The one namespace whose cases are graded from the delivery queue
|
|
35
|
+
* itself rather than from an HTTP attempt. The engine's own
|
|
36
|
+
* `queueObserver` is what makes them gradable: absent that block every
|
|
37
|
+
* `task:*` case fails closed as `missing`, so the pack is only ever
|
|
38
|
+
* OFFERED when the repository declares one. It is last in print order
|
|
39
|
+
* for that reason — a repository without an observer never reaches it.
|
|
40
|
+
*/
|
|
41
|
+
export const TASK_BEHAVIOR_NAMESPACE = 'task';
|
|
42
|
+
/** The behavior packs a repository can be asked to enable cases for. */
|
|
43
|
+
export const BEHAVIOR_NAMESPACES = [...HTTP_BEHAVIOR_NAMESPACES, TASK_BEHAVIOR_NAMESPACE];
|
|
44
|
+
/**
|
|
45
|
+
* The behavior document list each namespace's cases are declared under:
|
|
46
|
+
* a task case is a claim about a task resource, never about a route.
|
|
47
|
+
*/
|
|
48
|
+
const NAMESPACE_LIST_KEY = Object.freeze({
|
|
49
|
+
webhook: 'endpoints',
|
|
50
|
+
workflow: 'endpoints',
|
|
51
|
+
auth: 'endpoints',
|
|
52
|
+
validation: 'endpoints',
|
|
53
|
+
task: 'resources',
|
|
54
|
+
});
|
|
55
|
+
/** The bundled plugin each namespace's cases are graded through. */
|
|
56
|
+
export const BEHAVIOR_NAMESPACE_PLUGIN = Object.freeze({
|
|
57
|
+
webhook: 'gateforge.pack-webhook',
|
|
58
|
+
workflow: 'gateforge.pack-workflow',
|
|
59
|
+
auth: 'gateforge.pack-auth',
|
|
60
|
+
validation: 'gateforge.pack-validation',
|
|
61
|
+
task: 'gateforge.pack-task',
|
|
62
|
+
});
|
|
63
|
+
/** Directories never descended into (mirrors `repo-scan.ts`). */
|
|
64
|
+
const IGNORED_DIRS = new Set([
|
|
65
|
+
'node_modules',
|
|
66
|
+
'.venv',
|
|
67
|
+
'venv',
|
|
68
|
+
'.git',
|
|
69
|
+
'dist',
|
|
70
|
+
'build',
|
|
71
|
+
'__pycache__',
|
|
72
|
+
'.pytest_cache',
|
|
73
|
+
'.mypy_cache',
|
|
74
|
+
]);
|
|
75
|
+
/** Files larger than this are never content-scanned (binary guard). */
|
|
76
|
+
const MAX_SCAN_BYTES = 1_048_576;
|
|
77
|
+
/** How many files one namespace may be proven from (bounded evidence). */
|
|
78
|
+
const MAX_EVIDENCE_FILES = 5;
|
|
79
|
+
/**
|
|
80
|
+
* The machinery a namespace's cases are about. A namespace is detected
|
|
81
|
+
* only when one of these appears in the repository's own source text: the
|
|
82
|
+
* code that decides a signature, an authorization outcome, a state
|
|
83
|
+
* transition, a request schema, or a background delivery.
|
|
84
|
+
*/
|
|
85
|
+
const NAMESPACE_NEEDLES = Object.freeze({
|
|
86
|
+
webhook: [
|
|
87
|
+
/createHmac\s*\(/,
|
|
88
|
+
/timingSafeEqual\s*\(/,
|
|
89
|
+
/['"`]x-(?:hub-)?signature['"`]/i,
|
|
90
|
+
/\b(?:verify|compute|check)Signature\s*\(/,
|
|
91
|
+
/signatureProfile/,
|
|
92
|
+
],
|
|
93
|
+
workflow: [
|
|
94
|
+
/\bstateMachine\b/,
|
|
95
|
+
/\ballowedTransitions\b/,
|
|
96
|
+
/\bvalidTransitions\b/,
|
|
97
|
+
/\btransitionTo\s*\(/,
|
|
98
|
+
/\bworkflowState\b/,
|
|
99
|
+
],
|
|
100
|
+
auth: [
|
|
101
|
+
/\brequireAuth(?:entication|orized)?\b/,
|
|
102
|
+
/\bisAuthenticated\b/,
|
|
103
|
+
/\bcurrentUser\b/,
|
|
104
|
+
/\bjsonwebtoken\b|\bjwt\s*\.\s*verify\b/,
|
|
105
|
+
/\bpassport\s*\.\s*(?:authenticate|use)\b/,
|
|
106
|
+
/\bget_current_user\b/,
|
|
107
|
+
/\bverifyToken\s*\(/,
|
|
108
|
+
],
|
|
109
|
+
validation: [
|
|
110
|
+
/\bz\s*\.\s*object\s*\(/,
|
|
111
|
+
/\bjoi\s*\.\s*object\s*\(/,
|
|
112
|
+
/\bpydantic\b/,
|
|
113
|
+
/\bclass-validator\b/,
|
|
114
|
+
/\bfield_validator\b/,
|
|
115
|
+
/\bvalidateSchema\s*\(/,
|
|
116
|
+
],
|
|
117
|
+
task: [
|
|
118
|
+
/\bnew\s+Queue\s*\(/,
|
|
119
|
+
/\bnew\s+Worker\s*\(/,
|
|
120
|
+
/\bqueue\.add\s*\(/,
|
|
121
|
+
/\bnew\s+QueueEvents\s*\(/,
|
|
122
|
+
/\battempts\s*:\s*\d/,
|
|
123
|
+
/\bbackoff\s*:/,
|
|
124
|
+
/\bmaxAttempts\b/,
|
|
125
|
+
/\bidempotencyKey\b/,
|
|
126
|
+
/\bBullMQ\b/,
|
|
127
|
+
/\bCelery\b/,
|
|
128
|
+
],
|
|
129
|
+
});
|
|
130
|
+
/**
|
|
131
|
+
* Parses the value of `--behavior-packs`.
|
|
132
|
+
*
|
|
133
|
+
* Args:
|
|
134
|
+
* value: the raw flag value (comma-separated namespaces).
|
|
135
|
+
*
|
|
136
|
+
* Returns:
|
|
137
|
+
* BehaviorNamespace[]: the requested namespaces, deduplicated in the
|
|
138
|
+
* canonical print order.
|
|
139
|
+
*
|
|
140
|
+
* Throws:
|
|
141
|
+
* Error: an unknown namespace name — the caller turns it into a usage
|
|
142
|
+
* error naming the known set, so an agent can never silently enable the
|
|
143
|
+
* wrong pack.
|
|
144
|
+
*/
|
|
145
|
+
export function parseBehaviorPacks(value) {
|
|
146
|
+
const requested = [];
|
|
147
|
+
for (const raw of value.split(',')) {
|
|
148
|
+
const name = raw.trim().toLowerCase();
|
|
149
|
+
if (name.length === 0)
|
|
150
|
+
continue;
|
|
151
|
+
if (!BEHAVIOR_NAMESPACES.includes(name)) {
|
|
152
|
+
throw new Error(`unknown behavior pack '${name}' (known packs: ${BEHAVIOR_NAMESPACES.join(', ')})`);
|
|
153
|
+
}
|
|
154
|
+
if (!requested.includes(name))
|
|
155
|
+
requested.push(name);
|
|
156
|
+
}
|
|
157
|
+
return BEHAVIOR_NAMESPACES.filter((namespace) => requested.includes(namespace));
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Detects which behavior packs the repository shows, in print order.
|
|
161
|
+
*
|
|
162
|
+
* The `task` namespace is scanned ONLY when the repository's config
|
|
163
|
+
* declares a `queueObserver`: the engine can only grade a delivery case
|
|
164
|
+
* from a queue it reads itself, so offering the pack without one would
|
|
165
|
+
* print a flag whose cases can never be satisfied. Absent the block the
|
|
166
|
+
* scan never looks for those needles, which is what keeps a repository
|
|
167
|
+
* without an observer byte-identical.
|
|
168
|
+
*
|
|
169
|
+
* Args:
|
|
170
|
+
* cwd: absolute repository root.
|
|
171
|
+
* queueObserverConfigured: whether `.gateforge.yml` declares a
|
|
172
|
+
* `queueObserver` block.
|
|
173
|
+
*
|
|
174
|
+
* Returns:
|
|
175
|
+
* DetectedBehaviorPack[]: one entry per detected namespace, each with
|
|
176
|
+
* the first file that proved it. Empty for a repository that shows none
|
|
177
|
+
* — the byte-identical `init` case.
|
|
178
|
+
*/
|
|
179
|
+
export function detectBehaviorPacks(cwd, queueObserverConfigured) {
|
|
180
|
+
const offered = queueObserverConfigured
|
|
181
|
+
? BEHAVIOR_NAMESPACES
|
|
182
|
+
: HTTP_BEHAVIOR_NAMESPACES;
|
|
183
|
+
const files = [];
|
|
184
|
+
collectFiles(cwd, cwd, files);
|
|
185
|
+
const found = new Map();
|
|
186
|
+
for (const file of files) {
|
|
187
|
+
const text = readSmallText(join(cwd, file));
|
|
188
|
+
if (text === null)
|
|
189
|
+
continue;
|
|
190
|
+
for (const namespace of offered) {
|
|
191
|
+
if (found.has(namespace))
|
|
192
|
+
continue;
|
|
193
|
+
for (const needle of NAMESPACE_NEEDLES[namespace]) {
|
|
194
|
+
const match = needle.exec(text);
|
|
195
|
+
if (match === null)
|
|
196
|
+
continue;
|
|
197
|
+
found.set(namespace, {
|
|
198
|
+
namespace,
|
|
199
|
+
evidenceFile: file,
|
|
200
|
+
evidenceNeedle: match[0].length > 60 ? `${match[0].slice(0, 60)}…` : match[0],
|
|
201
|
+
});
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (found.size === offered.length)
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
return BEHAVIOR_NAMESPACES.filter((namespace) => found.has(namespace)).map((namespace) => found.get(namespace));
|
|
209
|
+
}
|
|
210
|
+
/** Human sentence naming why a namespace was detected. */
|
|
211
|
+
export function behaviorPackEvidence(pack) {
|
|
212
|
+
return `\`${pack.evidenceNeedle}\` in ${pack.evidenceFile}`;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* The commented example cases init writes for the ENABLED namespaces.
|
|
216
|
+
*
|
|
217
|
+
* Every example is commented: the document itself must still parse as a
|
|
218
|
+
* complete-behavior document with no declarations (a scaffold is never
|
|
219
|
+
* approval), and an example cannot be pasted blind — its resource id is
|
|
220
|
+
* the id `gateforge next` prints for the real route.
|
|
221
|
+
*
|
|
222
|
+
* Args:
|
|
223
|
+
* packs: the detected packs, already filtered to the enabled ones.
|
|
224
|
+
*
|
|
225
|
+
* Returns:
|
|
226
|
+
* string: the YAML text to append to the behavior document (may be
|
|
227
|
+
* empty when no namespace is enabled).
|
|
228
|
+
*/
|
|
229
|
+
export function behaviorSkeletonExamples(packs) {
|
|
230
|
+
if (packs.length === 0)
|
|
231
|
+
return '';
|
|
232
|
+
return [
|
|
233
|
+
'',
|
|
234
|
+
...packs.map((pack) => {
|
|
235
|
+
const isTask = pack.namespace === TASK_BEHAVIOR_NAMESPACE;
|
|
236
|
+
// The header names what THIS pack's example actually contains: a
|
|
237
|
+
// delivery case is declared from the task resource, never from a
|
|
238
|
+
// route, so the placeholders and the list key both differ.
|
|
239
|
+
const header = isTask
|
|
240
|
+
? [
|
|
241
|
+
"# Replace <resource-id>, <recipe-id> and <actor-id> with this repository's own",
|
|
242
|
+
"# values, then uncomment the block below. The engine produces the delivery and",
|
|
243
|
+
'# reads the queue itself, so these cases are graded on job attempts, never on',
|
|
244
|
+
'# an HTTP attempt.',
|
|
245
|
+
]
|
|
246
|
+
: [
|
|
247
|
+
"# Replace <resource-id>, <METHOD>, <path>, <recipe-id> and <actor-id> with this",
|
|
248
|
+
"# repository's own values, then uncomment the block below. `gateforge next`",
|
|
249
|
+
'# prints the complete declaration for each discovered route, the matching',
|
|
250
|
+
'# test-map entries, and the command that runs the gate.',
|
|
251
|
+
];
|
|
252
|
+
return [
|
|
253
|
+
`# --- ${pack.namespace}: ${behaviorPackEvidence(pack)} ---`,
|
|
254
|
+
...header,
|
|
255
|
+
`# ${NAMESPACE_LIST_KEY[pack.namespace]}:`,
|
|
256
|
+
...behaviorExampleCase(pack.namespace)
|
|
257
|
+
.split('\n')
|
|
258
|
+
.map((line) => `# ${line}`),
|
|
259
|
+
].join('\n');
|
|
260
|
+
}),
|
|
261
|
+
].join('\n');
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* The example case for one namespace, as the owner would write it: the
|
|
265
|
+
* entry contract plus its required negative control. Values the engine
|
|
266
|
+
* reads are real (`channel`, `credentialVariant`, `signatureProfile`);
|
|
267
|
+
* values only the owner knows stay visibly placeheld rather than guessed.
|
|
268
|
+
*
|
|
269
|
+
* The `task` namespace is the exception that proves the rule: its case
|
|
270
|
+
* is graded from the queue the ENGINE produced, so it declares a
|
|
271
|
+
* `deliver` action on the `engine-task` channel and an `attempts` state
|
|
272
|
+
* rule — never an HTTP request, which could never settle a delivery.
|
|
273
|
+
*
|
|
274
|
+
* Args:
|
|
275
|
+
* namespace: the behavior namespace.
|
|
276
|
+
*
|
|
277
|
+
* Returns:
|
|
278
|
+
* string: the YAML case list for one endpoint or task resource.
|
|
279
|
+
*/
|
|
280
|
+
export function behaviorExampleCase(namespace) {
|
|
281
|
+
if (namespace === TASK_BEHAVIOR_NAMESPACE) {
|
|
282
|
+
return [
|
|
283
|
+
'- resourceId: <resource-id> # the task resource id the queueObserver binds',
|
|
284
|
+
' effects: []',
|
|
285
|
+
' cases:',
|
|
286
|
+
` - id: ${slugOf(TASK_EXAMPLE_CONTRACT)}`,
|
|
287
|
+
` contract: ${TASK_EXAMPLE_CONTRACT}`,
|
|
288
|
+
' channel: engine-task # only the engine\'s own queue read can settle this',
|
|
289
|
+
' fixture: <recipe-id> # the recipe id in fixtures/behavior-fixtures.yml',
|
|
290
|
+
' actor: <actor-id> # an actor of that recipe',
|
|
291
|
+
' action:',
|
|
292
|
+
' kind: deliver',
|
|
293
|
+
' resourceId: <resource-id>',
|
|
294
|
+
' payload: {from: fixture, key: <payload-key>}',
|
|
295
|
+
' idempotencyKey: {from: literal, value: <idempotency-key>}',
|
|
296
|
+
' deliveryId: {from: literal, value: <delivery-id>}',
|
|
297
|
+
' count: 1',
|
|
298
|
+
' schedule: serial',
|
|
299
|
+
' expect:',
|
|
300
|
+
' statuses: [] # a delivery answers no HTTP status',
|
|
301
|
+
' response: []',
|
|
302
|
+
' state:',
|
|
303
|
+
' - kind: attempts',
|
|
304
|
+
' resourceId: <resource-id>',
|
|
305
|
+
' count: <max-attempts> # the retry bound the queue declares',
|
|
306
|
+
' terminal: succeeded # succeeded | failed | rejected',
|
|
307
|
+
].join('\n');
|
|
308
|
+
}
|
|
309
|
+
const contract = BEHAVIOR_NAMESPACE_CONTRACTS[namespace];
|
|
310
|
+
return [
|
|
311
|
+
'- resourceId: <resource-id>',
|
|
312
|
+
' effects: []',
|
|
313
|
+
' cases:',
|
|
314
|
+
` - id: ${slugOf(contract.accepted)}`,
|
|
315
|
+
` contract: ${contract.accepted}`,
|
|
316
|
+
` channel: ${contract.channel}`,
|
|
317
|
+
` fixture: <recipe-id> # the recipe id in fixtures/behavior-fixtures.yml`,
|
|
318
|
+
' actor: <actor-id> # an actor of that recipe',
|
|
319
|
+
' action:',
|
|
320
|
+
' kind: request',
|
|
321
|
+
' method: <METHOD>',
|
|
322
|
+
' pathTemplate: <path> # what `gateforge next` prints for this route',
|
|
323
|
+
' path: {}',
|
|
324
|
+
' query: {},',
|
|
325
|
+
' body: {encoding: json, fields: {}}',
|
|
326
|
+
` credentialVariant: ${contract.credential}`,
|
|
327
|
+
' expect:',
|
|
328
|
+
` statuses: [${contract.acceptedStatus}]`,
|
|
329
|
+
' response: []',
|
|
330
|
+
` - id: ${slugOf(contract.rejected)}`,
|
|
331
|
+
` contract: ${contract.rejected}`,
|
|
332
|
+
` controlCase: ${slugOf(contract.accepted)}`,
|
|
333
|
+
` channel: ${contract.channel}`,
|
|
334
|
+
' fixture: <recipe-id>',
|
|
335
|
+
' actor: <actor-id>',
|
|
336
|
+
' action:',
|
|
337
|
+
' kind: request',
|
|
338
|
+
' method: <METHOD>',
|
|
339
|
+
' pathTemplate: <path>',
|
|
340
|
+
' path: {}',
|
|
341
|
+
' query: {}',
|
|
342
|
+
' body: {encoding: json, fields: {}}',
|
|
343
|
+
` credentialVariant: ${contract.rejectedCredential}`,
|
|
344
|
+
' expect:',
|
|
345
|
+
` statuses: [${contract.rejectedStatus}]`,
|
|
346
|
+
' response: []',
|
|
347
|
+
].join('\n');
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* The contract the task example proves: the queue's own retry bound, the
|
|
351
|
+
* claim the engine's queue read is the only thing that can settle.
|
|
352
|
+
*/
|
|
353
|
+
const TASK_EXAMPLE_CONTRACT = 'task:retry-policy-enforced';
|
|
354
|
+
/** The engine's per-namespace declaration vocabulary. */
|
|
355
|
+
const BEHAVIOR_NAMESPACE_CONTRACTS = Object.freeze({
|
|
356
|
+
webhook: {
|
|
357
|
+
accepted: 'webhook:signature-accepted',
|
|
358
|
+
rejected: 'webhook:signature-rejected',
|
|
359
|
+
channel: 'engine-http',
|
|
360
|
+
credential: 'valid',
|
|
361
|
+
rejectedCredential: 'valid',
|
|
362
|
+
acceptedStatus: 200,
|
|
363
|
+
rejectedStatus: 401,
|
|
364
|
+
},
|
|
365
|
+
workflow: {
|
|
366
|
+
accepted: 'workflow:transition-allowed',
|
|
367
|
+
rejected: 'workflow:transition-rejected',
|
|
368
|
+
channel: 'engine-http',
|
|
369
|
+
credential: 'valid',
|
|
370
|
+
rejectedCredential: 'valid',
|
|
371
|
+
acceptedStatus: 200,
|
|
372
|
+
rejectedStatus: 409,
|
|
373
|
+
},
|
|
374
|
+
auth: {
|
|
375
|
+
accepted: 'auth:role-allowed',
|
|
376
|
+
rejected: 'auth:role-denied',
|
|
377
|
+
channel: 'engine-http',
|
|
378
|
+
credential: 'valid',
|
|
379
|
+
rejectedCredential: 'invalid',
|
|
380
|
+
acceptedStatus: 200,
|
|
381
|
+
rejectedStatus: 403,
|
|
382
|
+
},
|
|
383
|
+
validation: {
|
|
384
|
+
accepted: 'validation:boundary-accepted',
|
|
385
|
+
rejected: 'validation:boundary-rejected',
|
|
386
|
+
channel: 'engine-http',
|
|
387
|
+
credential: 'valid',
|
|
388
|
+
rejectedCredential: 'valid',
|
|
389
|
+
acceptedStatus: 200,
|
|
390
|
+
rejectedStatus: 422,
|
|
391
|
+
},
|
|
392
|
+
});
|
|
393
|
+
/** The case id slug of a contract name (`webhook:signature-accepted` → `signature-accepted`). */
|
|
394
|
+
function slugOf(contract) {
|
|
395
|
+
return contract.slice(contract.indexOf(':') + 1);
|
|
396
|
+
}
|
|
397
|
+
/** Recursively collects repo-relative file paths, skipping ignored directories. */
|
|
398
|
+
function collectFiles(root, dir, out) {
|
|
399
|
+
let entries;
|
|
400
|
+
try {
|
|
401
|
+
entries = readdirSync(dir);
|
|
402
|
+
}
|
|
403
|
+
catch {
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
for (const entry of entries) {
|
|
407
|
+
if (IGNORED_DIRS.has(entry))
|
|
408
|
+
continue;
|
|
409
|
+
const absolute = join(dir, entry);
|
|
410
|
+
let stat;
|
|
411
|
+
try {
|
|
412
|
+
stat = statSync(absolute);
|
|
413
|
+
}
|
|
414
|
+
catch {
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
if (stat.isDirectory()) {
|
|
418
|
+
collectFiles(root, absolute, out);
|
|
419
|
+
}
|
|
420
|
+
else if (stat.isFile() && /\.(js|jsx|mjs|cjs|ts|tsx|py)$/.test(entry)) {
|
|
421
|
+
out.push(relative(root, absolute).split('\\').join('/'));
|
|
422
|
+
if (out.length >= 4_000)
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
/** Reads a small text file; returns null when unreadable or too large. */
|
|
428
|
+
function readSmallText(absolute) {
|
|
429
|
+
try {
|
|
430
|
+
if (!existsSync(absolute))
|
|
431
|
+
return null;
|
|
432
|
+
const stat = statSync(absolute);
|
|
433
|
+
if (!stat.isFile() || stat.size > MAX_SCAN_BYTES)
|
|
434
|
+
return null;
|
|
435
|
+
return readFileSync(absolute, 'utf8');
|
|
436
|
+
}
|
|
437
|
+
catch {
|
|
438
|
+
return null;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* The per-contract shapes the engine's own webhook driver expects. They
|
|
443
|
+
* are the engine's semantics, not a guess: a positive signature is 200
|
|
444
|
+
* and records a row, a forged one is 401 and records nothing, a replay
|
|
445
|
+
* leaves the single row alone.
|
|
446
|
+
*/
|
|
447
|
+
const CONTRACT_SHAPES = Object.freeze({
|
|
448
|
+
'webhook:signature-accepted': { status: 200, state: 'created', signatureProfile: 'hmac-sha256', credential: 'valid' },
|
|
449
|
+
'webhook:signature-rejected': {
|
|
450
|
+
status: 401,
|
|
451
|
+
state: 'unchanged',
|
|
452
|
+
signatureProfile: 'hmac-sha256;forgery=signature',
|
|
453
|
+
credential: 'valid',
|
|
454
|
+
control: 'webhook:signature-accepted',
|
|
455
|
+
},
|
|
456
|
+
'webhook:malformed-rejected': {
|
|
457
|
+
status: 400,
|
|
458
|
+
state: 'unchanged',
|
|
459
|
+
signatureProfile: 'hmac-sha256;malformed=body',
|
|
460
|
+
credential: 'valid',
|
|
461
|
+
control: 'webhook:signature-accepted',
|
|
462
|
+
},
|
|
463
|
+
'webhook:replay-idempotent': { status: 200, state: 'unchanged', signatureProfile: 'hmac-sha256', credential: 'valid' },
|
|
464
|
+
'webhook:retry-bounded': { status: 429, state: 'unchanged', signatureProfile: 'hmac-sha256', credential: 'valid' },
|
|
465
|
+
});
|
|
466
|
+
/**
|
|
467
|
+
* The case shape a contract falls back to when it has no engine-proven
|
|
468
|
+
* one. A namespace with no per-namespace table (the `task` one, whose
|
|
469
|
+
* cases are graded from the queue rather than from a response) takes the
|
|
470
|
+
* neutral transport shape; the caller refuses to print a delivery case
|
|
471
|
+
* here at all.
|
|
472
|
+
*/
|
|
473
|
+
function defaultShape(contract) {
|
|
474
|
+
const namespace = contract.slice(0, contract.indexOf(':'));
|
|
475
|
+
const contracts = BEHAVIOR_NAMESPACE_CONTRACTS[namespace];
|
|
476
|
+
if (contracts === undefined)
|
|
477
|
+
return { status: 200, state: 'unchanged', credential: 'valid' };
|
|
478
|
+
if (contract === contracts.rejected) {
|
|
479
|
+
return {
|
|
480
|
+
status: contracts.rejectedStatus,
|
|
481
|
+
state: 'unchanged',
|
|
482
|
+
credential: contracts.rejectedCredential,
|
|
483
|
+
control: contracts.accepted,
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
return { status: contracts.acceptedStatus, state: 'unchanged', credential: contracts.credential };
|
|
487
|
+
}
|
|
488
|
+
/** The engine-proven shape of a contract, or its namespace default. */
|
|
489
|
+
function shapeOf(contract) {
|
|
490
|
+
return CONTRACT_SHAPES[contract] ?? defaultShape(contract);
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Builds the complete `endpoints:` entry `gateforge next` prints for one
|
|
494
|
+
* discovered route.
|
|
495
|
+
*
|
|
496
|
+
* Everything in the printed text is real: the resource id, the method and
|
|
497
|
+
* path discovery reported, the adapter-backed entity the case is graded
|
|
498
|
+
* against, the recipe the owner declared next to the app, and the case
|
|
499
|
+
* ids the compiler will hash. When a fact is missing the entry is
|
|
500
|
+
* `null` and the reason is printed instead — a declaration that cannot
|
|
501
|
+
* be completed here is never printed as if it could.
|
|
502
|
+
*
|
|
503
|
+
* Args:
|
|
504
|
+
* input: the endpoint's facts, the contracts the policy requires of
|
|
505
|
+
* it, and the fixture recipe the repository declares.
|
|
506
|
+
* caseIdFor: the canonical case-id recipe (`sha256` over domain,
|
|
507
|
+
* resource id and slug), injected so this module stays free of the
|
|
508
|
+
* engine's hashing import.
|
|
509
|
+
*
|
|
510
|
+
* Returns:
|
|
511
|
+
* BehaviorDeclarationPrint: the printable entry and its cases.
|
|
512
|
+
*/
|
|
513
|
+
export function buildEndpointDeclaration(input) {
|
|
514
|
+
const contracts = input.requiredContracts.filter((contract) => contract.includes(':'));
|
|
515
|
+
if (contracts.length === 0) {
|
|
516
|
+
return {
|
|
517
|
+
entry: null,
|
|
518
|
+
incompleteReason: 'no behavior policy requires a contract for this route, so there is no case to declare — ' +
|
|
519
|
+
'name the contract this route must satisfy in the policy document first',
|
|
520
|
+
cases: [],
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
const effect = input.endpoint.effects[0];
|
|
524
|
+
if (effect === undefined) {
|
|
525
|
+
return {
|
|
526
|
+
entry: null,
|
|
527
|
+
incompleteReason: 'no adapter-backed entity is declared for this repository, so a case has no real state to be ' +
|
|
528
|
+
'graded against — add the entity and its reviewed adapter under .gateforge/adapters first',
|
|
529
|
+
cases: [],
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
if (input.recipe === null) {
|
|
533
|
+
return {
|
|
534
|
+
entry: null,
|
|
535
|
+
incompleteReason: 'no fixture recipe is declared, so the engine has no provider-supplied request to drive — ' +
|
|
536
|
+
'declare one under fixtures/ with its actor, its subject and the row a delivery creates',
|
|
537
|
+
cases: [],
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
const recipe = input.recipe;
|
|
541
|
+
const cases = [];
|
|
542
|
+
const lines = [
|
|
543
|
+
` - resourceId: ${input.endpoint.resourceId}`,
|
|
544
|
+
' effects:',
|
|
545
|
+
` - id: ${effect.id}`,
|
|
546
|
+
` resourceId: ${effect.resourceId}`,
|
|
547
|
+
` adapter: ${effect.adapter}`,
|
|
548
|
+
` scope: ${effect.id}`,
|
|
549
|
+
` identityFields: [${effect.identityFields.join(', ')}]`,
|
|
550
|
+
` fields: [${effect.fields.join(', ')}]`,
|
|
551
|
+
' completion: immediate',
|
|
552
|
+
' cases:',
|
|
553
|
+
];
|
|
554
|
+
for (const contract of contracts) {
|
|
555
|
+
const shape = shapeOf(contract);
|
|
556
|
+
const slug = slugOf(contract);
|
|
557
|
+
if (shape.state === 'created' && recipe.creates === null) {
|
|
558
|
+
return {
|
|
559
|
+
entry: null,
|
|
560
|
+
incompleteReason: `'${contract}' is proved by what a delivery CREATES, and the recipe '${recipe.id}' does not ` +
|
|
561
|
+
'declare that row — add it under `creates:` in the fixture declaration',
|
|
562
|
+
cases: [],
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
const caseId = input.caseIdFor(input.endpoint.resourceId, slug);
|
|
566
|
+
const title = `gateforge proves ${slug}`;
|
|
567
|
+
cases.push({
|
|
568
|
+
id: slug,
|
|
569
|
+
contract,
|
|
570
|
+
caseId,
|
|
571
|
+
obligationId: `${input.endpoint.resourceId}:${contract}`,
|
|
572
|
+
title,
|
|
573
|
+
testKey: `${input.runner}:${input.project}:${input.specFile}:${title}`,
|
|
574
|
+
});
|
|
575
|
+
lines.push(` - id: ${slug}`, ` contract: ${contract}`, ...(shape.control === undefined
|
|
576
|
+
? []
|
|
577
|
+
: [` controlCase: ${slugOf(shape.control)}`]), ' channel: engine-http', ` fixture: ${recipe.id}`, ` actor: ${recipe.actor}`, ' action:', ' kind: request', ` method: ${input.endpoint.method}`, ` pathTemplate: ${input.endpoint.path}`, ' path: {}', ' query: {}', ' body:', ' encoding: raw', ` fixture: ${recipe.subject}`, ` credentialVariant: ${shape.credential}`, ...(shape.signatureProfile === undefined
|
|
578
|
+
? []
|
|
579
|
+
: [` signatureProfile: ${shape.signatureProfile}`]), ' expect:', ` statuses: [${shape.status}]`, ' response: []', ' state:', ...(shape.state === 'unchanged'
|
|
580
|
+
? [` - kind: unchanged`, ` scope: ${effect.id}`]
|
|
581
|
+
: [
|
|
582
|
+
` - kind: created`,
|
|
583
|
+
` scope: ${effect.id}`,
|
|
584
|
+
' rows:',
|
|
585
|
+
' - fields:',
|
|
586
|
+
...Object.entries(recipe.creates ?? {}).map(([field, value]) => ` ${field}: {from: literal, value: ${JSON.stringify(value)}}`),
|
|
587
|
+
]));
|
|
588
|
+
}
|
|
589
|
+
return { entry: lines.join('\n'), incompleteReason: null, cases };
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* The proof test the printed cases are proven through: one test per case,
|
|
593
|
+
* each naming its canonical case id and asking the ENGINE to drive it.
|
|
594
|
+
* The test body asserts only that the engine sealed a record — clicks and
|
|
595
|
+
* assertions of its own would prove nothing.
|
|
596
|
+
*
|
|
597
|
+
* Args:
|
|
598
|
+
* input: the endpoint id and the printed cases.
|
|
599
|
+
*
|
|
600
|
+
* Returns:
|
|
601
|
+
* string: the complete spec file content.
|
|
602
|
+
*/
|
|
603
|
+
export function renderProofSpec(input) {
|
|
604
|
+
return [
|
|
605
|
+
`// Generated for ${input.resourceId} by \`gateforge next\`.`,
|
|
606
|
+
'// Each test names ONE canonical case id and asks the engine to drive it:',
|
|
607
|
+
'// the ENGINE executes the case and seals the record. A test body that',
|
|
608
|
+
'// only clicks proves nothing.',
|
|
609
|
+
"import { test as gateforgeTest, expect } from '@gate-forge/pack-playwright';",
|
|
610
|
+
"import { sha256Canonical, BEHAVIOR_CASE_DOMAIN } from '@gate-forge/core';",
|
|
611
|
+
'',
|
|
612
|
+
`const RESOURCE_ID = ${JSON.stringify(input.resourceId)};`,
|
|
613
|
+
'',
|
|
614
|
+
'function caseIdOf(slug) {',
|
|
615
|
+
' return sha256Canonical({ domain: BEHAVIOR_CASE_DOMAIN, resourceId: RESOURCE_ID, id: slug });',
|
|
616
|
+
'}',
|
|
617
|
+
'',
|
|
618
|
+
...input.cases.flatMap((entry) => [
|
|
619
|
+
`gateforgeTest(${JSON.stringify(entry.title)}, async ({ evidence }) => {`,
|
|
620
|
+
` const sealed = await evidence.prove(caseIdOf(${JSON.stringify(entry.id)}));`,
|
|
621
|
+
" expect(sealed.state).toBe('sealed');",
|
|
622
|
+
' expect(sealed.recordIds.length).toBeGreaterThan(0);',
|
|
623
|
+
'});',
|
|
624
|
+
'',
|
|
625
|
+
]),
|
|
626
|
+
].join('\n');
|
|
627
|
+
}
|
|
628
|
+
/**
|
|
629
|
+
* The `.gateforge/test-map.yml` entries that connect each printed case to
|
|
630
|
+
* its proof test. Mapping is intent — only engine-issued case evidence
|
|
631
|
+
* can satisfy the claim — so the entries carry the real case ids.
|
|
632
|
+
*
|
|
633
|
+
* Args:
|
|
634
|
+
* input: the printed cases and the runner identity that keys them.
|
|
635
|
+
*
|
|
636
|
+
* Returns:
|
|
637
|
+
* string: the `tests:` entries, indented for the document.
|
|
638
|
+
*/
|
|
639
|
+
export function renderTestMapEntries(input) {
|
|
640
|
+
return input.cases
|
|
641
|
+
.map((entry) => [
|
|
642
|
+
` - key: ${JSON.stringify(entry.testKey)}`,
|
|
643
|
+
' selector:',
|
|
644
|
+
` runner: ${input.runner}`,
|
|
645
|
+
` project: ${input.project}`,
|
|
646
|
+
` file: ${input.specFile}`,
|
|
647
|
+
' titlePath:',
|
|
648
|
+
` - ${JSON.stringify(entry.title)}`,
|
|
649
|
+
' kind: browser-e2e',
|
|
650
|
+
' claims:',
|
|
651
|
+
` - ${entry.obligationId}`,
|
|
652
|
+
' caseIds:',
|
|
653
|
+
` - ${entry.caseId}`,
|
|
654
|
+
` reason: the engine drives the ${entry.id} case for this route`,
|
|
655
|
+
].join('\n'))
|
|
656
|
+
.join('\n');
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Reads the fixture recipes a repository declares. Any YAML document
|
|
660
|
+
* under `fixtures/` that carries a `recipes:` list is read, so an
|
|
661
|
+
* application may keep one shared file or one per namespace.
|
|
662
|
+
*
|
|
663
|
+
* Args:
|
|
664
|
+
* cwd: absolute repository root.
|
|
665
|
+
*
|
|
666
|
+
* Returns:
|
|
667
|
+
* BehaviorRecipeFacts[]: the declared recipes in document order; empty
|
|
668
|
+
* when the repository declares none.
|
|
669
|
+
*/
|
|
670
|
+
export function loadBehaviorRecipes(cwd) {
|
|
671
|
+
const fixturesDir = join(cwd, 'fixtures');
|
|
672
|
+
let entries;
|
|
673
|
+
try {
|
|
674
|
+
entries = readdirSync(fixturesDir).filter((name) => /\.ya?ml$/.test(name)).sort();
|
|
675
|
+
}
|
|
676
|
+
catch {
|
|
677
|
+
return [];
|
|
678
|
+
}
|
|
679
|
+
const recipes = [];
|
|
680
|
+
for (const name of entries) {
|
|
681
|
+
const text = readSmallText(join(fixturesDir, name));
|
|
682
|
+
if (text === null)
|
|
683
|
+
continue;
|
|
684
|
+
let parsed;
|
|
685
|
+
try {
|
|
686
|
+
parsed = parseYaml(text);
|
|
687
|
+
}
|
|
688
|
+
catch {
|
|
689
|
+
continue;
|
|
690
|
+
}
|
|
691
|
+
if (typeof parsed !== 'object' || parsed === null)
|
|
692
|
+
continue;
|
|
693
|
+
const declared = parsed.recipes;
|
|
694
|
+
if (!Array.isArray(declared))
|
|
695
|
+
continue;
|
|
696
|
+
for (const raw of declared) {
|
|
697
|
+
if (typeof raw !== 'object' || raw === null)
|
|
698
|
+
continue;
|
|
699
|
+
const recipe = raw;
|
|
700
|
+
if (typeof recipe.id !== 'string' || recipe.id.length === 0)
|
|
701
|
+
continue;
|
|
702
|
+
const subjects = Array.isArray(recipe.subjects) ? recipe.subjects : [];
|
|
703
|
+
const actors = Array.isArray(recipe.actors) ? recipe.actors : [];
|
|
704
|
+
const subject = typeof recipe.subject === 'string'
|
|
705
|
+
? recipe.subject
|
|
706
|
+
: typeof subjects[0] === 'string'
|
|
707
|
+
? subjects[0]
|
|
708
|
+
: `${recipe.id}-input`;
|
|
709
|
+
const actor = typeof recipe.actor === 'string'
|
|
710
|
+
? recipe.actor
|
|
711
|
+
: typeof recipe.actors === 'string'
|
|
712
|
+
? recipe.actors
|
|
713
|
+
: typeof actors[0] === 'string'
|
|
714
|
+
? actors[0]
|
|
715
|
+
: 'provider';
|
|
716
|
+
recipes.push({
|
|
717
|
+
id: recipe.id,
|
|
718
|
+
actor,
|
|
719
|
+
subject,
|
|
720
|
+
creates: readCreates(recipe.creates),
|
|
721
|
+
});
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
return recipes;
|
|
725
|
+
}
|
|
726
|
+
/** The scalar row a recipe declares a successful delivery creates. */
|
|
727
|
+
function readCreates(value) {
|
|
728
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
729
|
+
return null;
|
|
730
|
+
const row = {};
|
|
731
|
+
for (const [field, expected] of Object.entries(value)) {
|
|
732
|
+
if (typeof expected === 'string' || typeof expected === 'number' || typeof expected === 'boolean') {
|
|
733
|
+
row[field] = expected;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
return Object.keys(row).length === 0 ? null : row;
|
|
737
|
+
}
|
|
738
|
+
//# sourceMappingURL=behavior-setup.js.map
|