Repository navigation
Commit 93deaf3
authored
refactor: move resource parsing and org connection helpers into config-free modules (#59)
## Value
**V.A.L.U.E. tier:** project — PR 3 of 10 for inline simulation PR checks ([TEST-141](https://linear.app/vapi/issue/TEST-141/gitops-run-simulation-suites-against-pr-changes-inline-as-ci-checks)); this PR is a behaviour-preserving refactor.
- **Problem:** the PR check (PRs 4–7) has to read an org's resource files and resolve org API keys. All the code that does that sits behind `config.ts`, which parses `argv`, binds one org and can `process.exit` at import time. Copying it into the check would let push and the check drift apart, so the check could test a different payload from the one push sends.
- **Who it affects:** gitops users, whose PR checks must test exactly what push would deploy; and maintainers, who get one copy of the loader instead of two (three for the `.md` system-prompt injection).
- **What changes:** code moves into two config-free modules. `resources.ts`, `config.ts` and `promote-cmd.ts` delegate to them and keep their exports, logs and error messages.
- **`src/resource-parse.ts`:**
- from `resources.ts`: `FOLDER_MAP`, `VALID_EXTENSIONS`, frontmatter/file parsing, the directory scan and the loader;
- from `config.ts`: `.vapi-ignore` reading and matching;
- new: `orgResourcesRead(rootDir, org)`, which returns every resource of an org keyed `type:id`;
- the three copies of the `.md` body → system message injection become `markdownResourceParse`.
- **`src/org-connection.ts`:** `envValue`, `tokensParse(envName)`, `connectionLoad` and `childRun`, moved from `promote-cmd.ts` and parameterised on the root dir, the token variable and the base URL.
- **`api.ts`:** takes `VapiApiError`, `parseApiMessage`, `shouldRetry` and the backoff constants from `vapi-client.ts`, and re-exports `VapiApiError`. Both clients now share one error class (so `instanceof` checks see one class) and one retry rule.
- **`promotion.ts`:**
- imports `FOLDER_MAP` / `VALID_EXTENSIONS` instead of keeping its own copies;
- exports `SLUG_RE`, `promotionBindingsParse`, `promotionBindingsResolve`, `promotionBindingsApply` and `PromotionBindingsResolved`, for the check config (PR 4) and the payload builder (PR 5).
- Also dropped: the unused `FOLDER_TO_TYPE` map in `resources.ts`.
## Evidence of value
**The output is byte-identical before and after.** The same fixture org was run at both refs against a local stub API. Nothing reached a real org.
The fixture:
- an `.md` assistant whose frontmatter has a system message the body must replace;
- a nested assistant;
- an ignored assistant;
- a backup copy and an unsupported file;
- a `.ts` tool and a YAML tool;
- a squad, a structured output and all four simulation types;
- a two-org `promotion.yml`.
| Command | `fix/sim-false-green` (06a1492) | This branch (0518af1) |
|---|---|---|
| `push ev-dev --dry-run` (new-file gate refusal) | identical | identical |
| `push ev-dev --dry-run --allow-new-files` (full plan, every would-POST) | identical | identical |
| `loadResources` JSON for every type (full parsed data, incl. the `.md` system message) | identical | identical |
| `validate ev-dev` | identical | identical |
| `promote --pipeline release --from ev-dev --to ev-prod` (plan) | identical | identical |
| **Combined stdout + stderr + stub request log** | sha256 `3f3b609f…1d3b`, 14,927 bytes | sha256 `3f3b609f…1d3b`, 14,927 bytes |
- Dry-run placeholder IDs (`dry-run-post-<Date.now()>`) and the stub's port are normalised before hashing.
- `--allow-new-files` is passed only because every fixture file is new by construction.
**Tests:** `npm test` goes from 377 to 398 passing (21 new), and `npm run build` (src + tests) is clean.
## Testing plan
- **`tests/resource-parse.test.ts`** (temp-dir fixtures) covers:
- `.md` body replacing a frontmatter system message, and an empty body;
- missing frontmatter;
- `.md` parsing being the same for `parseResourceDataFromFile` and the loader;
- sorted order across `.yml`/`.yaml`/`.ts` and nested dirs;
- hidden and `.bkp` files skipped;
- duplicate IDs refused, with the exact YAML-not-an-object message;
- missing directory;
- `.vapi-ignore` comments, blanks and `!`;
- `*` vs `**` vs `?` matching;
- `orgResourcesRead` reading one org and applying its ignore file, and the caller's override.
- **`tests/org-connection.test.ts`** covers:
- plain and quoted `.env` values;
- token-map parsing, with the variable named in every error;
- token precedence (map, then `.env.<org>`) and base URL precedence (configured, then `.env.<org>`);
- the missing-token error;
- `childRun` passing the org and key to a real child, and dropping an inherited `VAPI_BASE_URL`;
- a failing child.
- The existing push dry-run, `.vapi-ignore` push, promotion, audit and cleanup-safety suites pass unchanged.
- **Not tested:**
- a push against a real org (the comparison uses a stub API that returns empty lists, so update/PATCH paths for existing resources aren't exercised; the code they call is unchanged);
- `promote --apply` (the moved `connectionLoad`/`childRun` are covered by unit tests, but no live child `pull`/`apply` was run);
- Node 20 locally (left to CI).
Stacked on #58. Review with `git diff --color-moved=dimmed-zebra fix/sim-false-green...` to see that most of the diff is moved lines.
Refs TEST-141
🤖 Generated with [Claude Code](https://claude.com/claude-code)10 files changed
Lines changed: 1023 additions & 502 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
3 | 10 | | |
4 | 11 | | |
5 | 12 | | |
| |||
40 | 47 | | |
41 | 48 | | |
42 | 49 | | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
55 | 54 | | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | 55 | | |
70 | 56 | | |
71 | 57 | | |
| |||
83 | 69 | | |
84 | 70 | | |
85 | 71 | | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | 72 | | |
94 | 73 | | |
95 | 74 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
6 | 10 | | |
7 | 11 | | |
8 | 12 | | |
| |||
326 | 330 | | |
327 | 331 | | |
328 | 332 | | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
| 333 | + | |
| 334 | + | |
342 | 335 | | |
343 | 336 | | |
344 | 337 | | |
345 | 338 | | |
346 | | - | |
347 | | - | |
348 | | - | |
349 | | - | |
350 | 339 | | |
351 | 340 | | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | | - | |
357 | | - | |
358 | | - | |
359 | | - | |
360 | | - | |
361 | | - | |
362 | | - | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
367 | | - | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
368 | 344 | | |
369 | 345 | | |
370 | 346 | | |
371 | | - | |
372 | | - | |
373 | | - | |
374 | | - | |
375 | | - | |
376 | | - | |
377 | | - | |
378 | | - | |
379 | | - | |
380 | | - | |
381 | | - | |
382 | | - | |
383 | | - | |
384 | | - | |
385 | | - | |
386 | | - | |
387 | | - | |
388 | | - | |
389 | | - | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
397 | | - | |
398 | | - | |
399 | 347 | | |
400 | 348 | | |
401 | 349 | | |
402 | 350 | | |
403 | 351 | | |
404 | 352 | | |
405 | 353 | | |
406 | | - | |
407 | | - | |
408 | | - | |
409 | | - | |
410 | | - | |
411 | | - | |
| 354 | + | |
412 | 355 | | |
413 | 356 | | |
414 | 357 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
0 commit comments