@t0.labs/daxta 0.4.9 → 0.4.11

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 CHANGED
@@ -33,8 +33,8 @@ Jest finishes — Docs ready on /docs
33
33
  Docs ready → http://localhost:3000/docs
34
34
  ```
35
35
 
36
- 1. **Install** attaches DAxTA as a Jest plugin (`setupFilesAfterEnv` + reporter). Your existing `test:integration` stays the entrypoint — DAxTA does not wrap the process.
37
- 2. **During the run** only traffic is recorded (disk flush). OpenAPI/HTML is built once when Jest finishes.
36
+ 1. **Install** attaches DAxTA as a Jest plugin (`setupFilesAfterEnv` + reporter, and `globalSetup` when you don’t already have one). Your existing `test:integration` stays the entrypoint — DAxTA does not wrap the process.
37
+ 2. **During the run** only traffic is recorded (disk flush). OpenAPI/HTML is rebuilt once when Jest finishes, from **this run’s hits only** (previous worker shards are cleared so examples do not stack).
38
38
  3. `apiDocs(app)` in `main.ts` serves the generated viewer when `DAXTA_DOCS=true`.
39
39
 
40
40
 
@@ -78,7 +78,7 @@ npx @t0.labs/daxta uninstall
78
78
  Otomatik temizler:
79
79
 
80
80
  - `apiDocs` + `@t0.labs/daxta` import (`main.ts`)
81
- - Jest `setup` + `jest-reporter` hooks
81
+ - Jest `setup` + `jest-reporter` (+ `globalSetup` when missing)
82
82
  - `daxta.config.*` ve `daxta/` output
83
83
  - `.gitignore` / `.env` içindeki DAxTA satırları
84
84
  - `@t0.labs/daxta` paketi
@@ -515,12 +515,43 @@
515
515
  .nav-examples {
516
516
  display: none;
517
517
  flex-direction: column;
518
- gap: 1px;
518
+ gap: 6px;
519
519
  margin: 0 4px 6px 18px;
520
520
  padding-left: 10px;
521
521
  border-left: 1px solid var(--hairline);
522
522
  }
523
523
  .nav-op.examples-open > .nav-examples { display: flex; }
524
+ .nav-ex-group { display: flex; flex-direction: column; gap: 1px; }
525
+ .nav-ex-group-label {
526
+ display: flex;
527
+ align-items: center;
528
+ justify-content: space-between;
529
+ gap: 8px;
530
+ padding: 6px 8px 2px;
531
+ font: 600 10px/1.2 var(--mono);
532
+ letter-spacing: 0.06em;
533
+ text-transform: uppercase;
534
+ color: var(--stone);
535
+ }
536
+ .nav-ex-count,
537
+ .nav-ex-total,
538
+ .dd-count {
539
+ flex: none;
540
+ min-width: 16px;
541
+ padding: 1px 6px;
542
+ border-radius: 9999px;
543
+ background: var(--inset);
544
+ color: var(--charcoal);
545
+ font: 600 10px/1.4 var(--mono);
546
+ letter-spacing: 0;
547
+ text-transform: none;
548
+ text-align: center;
549
+ }
550
+ .nav-ex-total {
551
+ margin-right: 2px;
552
+ background: var(--white);
553
+ box-shadow: var(--ghost);
554
+ }
524
555
  .nav-example {
525
556
  display: block;
526
557
  width: 100%;
@@ -738,7 +769,17 @@
738
769
  font-size: 14px;
739
770
  line-height: 1.4;
740
771
  position: relative;
772
+ display: flex;
773
+ align-items: center;
774
+ gap: 8px;
741
775
  }
776
+ .dd-toggle .dd-toggle-label {
777
+ min-width: 0;
778
+ overflow: hidden;
779
+ text-overflow: ellipsis;
780
+ white-space: nowrap;
781
+ }
782
+ .dd-toggle .dd-count { margin-left: auto; margin-right: 4px; }
742
783
  .dd-toggle::after {
743
784
  content: "";
744
785
  position: absolute;
@@ -767,6 +808,10 @@
767
808
  }
768
809
  .dd.open .dd-menu { display: block; }
769
810
  .dd-group {
811
+ display: flex;
812
+ align-items: center;
813
+ justify-content: space-between;
814
+ gap: 8px;
770
815
  font-family: var(--mono);
771
816
  font-size: 11px;
772
817
  letter-spacing: 0.08em;
@@ -774,6 +819,9 @@
774
819
  color: var(--stone);
775
820
  padding: 10px 12px 6px;
776
821
  }
822
+ .dd-group .dd-count {
823
+ background: var(--paper, var(--inset));
824
+ }
777
825
  .dd-item {
778
826
  display: block;
779
827
  width: 100%;
@@ -2597,22 +2645,36 @@
2597
2645
  return { status: response.status, body: parsed, ok: response.ok };
2598
2646
  };
2599
2647
 
2600
- const groupedDropdown = (scenarios, selected) => {
2648
+ const groupScenarios = (scenarios) => {
2601
2649
  const groups = {};
2602
- scenarios.forEach((scenario, index) => {
2603
- (groups[scenario.group] ||= []).push({ scenario, index });
2650
+ (scenarios || []).forEach((scenario, index) => {
2651
+ const group = scenario.group || "Other";
2652
+ (groups[group] ||= []).push({ scenario, index });
2604
2653
  });
2605
- const order = GROUP_ORDER.filter((name) => groups[name]).concat(Object.keys(groups).filter((name) => !GROUP_ORDER.includes(name)));
2654
+ const order = GROUP_ORDER.filter((name) => groups[name]).concat(
2655
+ Object.keys(groups).filter((name) => !GROUP_ORDER.includes(name)),
2656
+ );
2657
+ return { groups, order };
2658
+ };
2659
+
2660
+ const groupedDropdown = (scenarios, selected) => {
2661
+ const { groups, order } = groupScenarios(scenarios);
2606
2662
  const current = scenarios[selected];
2607
- const label = current ? current.group + " · " + current.name : "Recorded call";
2663
+ const total = scenarios.length;
2664
+ const label = current
2665
+ ? current.group + " · " + current.name
2666
+ : "Recorded call";
2608
2667
  return `<div class="dd" id="scenario-dd">
2609
- <button type="button" class="dd-toggle" id="scenario-toggle">${label}</button>
2668
+ <button type="button" class="dd-toggle" id="scenario-toggle"><span class="dd-toggle-label">${escapeHtml(label)}</span><span class="dd-count" title="Total examples">${total}</span></button>
2610
2669
  <div class="dd-menu">${order
2611
2670
  .map(
2612
2671
  (group) =>
2613
- `<div class="dd-group">${group}</div>` +
2672
+ `<div class="dd-group"><span>${escapeHtml(group)}</span><span class="dd-count">${groups[group].length}</span></div>` +
2614
2673
  groups[group]
2615
- .map(({ scenario, index }) => `<button type="button" class="dd-item ${index === selected ? "on" : ""}" data-index="${index}">${scenario.status} — ${scenario.name}</button>`)
2674
+ .map(
2675
+ ({ scenario, index }) =>
2676
+ `<button type="button" class="dd-item ${index === selected ? "on" : ""}" data-index="${index}">${escapeHtml(String(scenario.status))} — ${escapeHtml(scenario.name)}</button>`,
2677
+ )
2616
2678
  .join(""),
2617
2679
  )
2618
2680
  .join("")}</div>
@@ -2633,6 +2695,23 @@
2633
2695
  return status ? status + " — " + name : name;
2634
2696
  };
2635
2697
 
2698
+ const renderNavExamples = (item) => {
2699
+ const { groups, order } = groupScenarios(item.scenarios);
2700
+ return `<div class="nav-examples">${order
2701
+ .map(
2702
+ (group) => `<div class="nav-ex-group">
2703
+ <div class="nav-ex-group-label"><span>${escapeHtml(group)}</span><span class="nav-ex-count">${groups[group].length}</span></div>
2704
+ ${groups[group]
2705
+ .map(
2706
+ ({ scenario, index }) =>
2707
+ `<button type="button" class="nav-example ${item.id === currentId && index === scenarioIndex ? "on" : ""}" data-id="${escapeAttr(item.id)}" data-scenario="${index}" title="${escapeAttr(scenarioNavLabel(scenario))}">${escapeHtml(scenarioNavLabel(scenario))}</button>`,
2708
+ )
2709
+ .join("")}
2710
+ </div>`,
2711
+ )
2712
+ .join("")}</div>`;
2713
+ };
2714
+
2636
2715
  const kv = (label, value) => {
2637
2716
  if (value === undefined || value === "") return "";
2638
2717
  const shown = typeof value === "object" ? JSON.stringify(value) : String(value);
@@ -2686,20 +2765,15 @@
2686
2765
  .map((item) => {
2687
2766
  const hasExamples = Array.isArray(item.scenarios) && item.scenarios.length > 0;
2688
2767
  const examplesOpen = expandedExampleOps.has(item.id);
2689
- const examplesHtml = hasExamples
2690
- ? `<div class="nav-examples">${item.scenarios
2691
- .map(
2692
- (scenario, index) =>
2693
- `<button type="button" class="nav-example ${item.id === currentId && index === scenarioIndex ? "on" : ""}" data-id="${escapeAttr(item.id)}" data-scenario="${index}" title="${escapeAttr(scenarioNavLabel(scenario))}">${escapeAttr(scenarioNavLabel(scenario))}</button>`,
2694
- )
2695
- .join("")}</div>`
2696
- : "";
2768
+ const examplesHtml = hasExamples ? renderNavExamples(item) : "";
2769
+ const exampleCount = hasExamples ? item.scenarios.length : 0;
2697
2770
  return `<div class="nav-op ${item.id === currentId ? "active" : ""} ${examplesOpen ? "examples-open" : ""}" data-op-id="${escapeAttr(item.id)}">
2698
2771
  <div class="nav-op-row">
2699
2772
  <button type="button" class="nav-item" data-id="${escapeAttr(item.id)}" title="${escapeAttr(item.path)}">
2700
2773
  <span class="method ${item.method}">${item.method}</span>
2701
2774
  <span class="nav-title">${escapeAttr(item.title)}</span>
2702
2775
  </button>
2776
+ ${hasExamples ? `<span class="nav-ex-total" title="${exampleCount} examples">${exampleCount}</span>` : ""}
2703
2777
  <button type="button" class="nav-expand" data-expand-id="${escapeAttr(item.id)}" title="Examples" aria-label="Toggle examples" aria-expanded="${examplesOpen ? "true" : "false"}" ${hasExamples ? "" : "disabled"}>
2704
2778
  <span class="chev"></span>
2705
2779
  </button>
@@ -1,4 +1,4 @@
1
- import type { RecordedHit } from '../recorder';
1
+ import { type RecordedHit } from '../recorder';
2
2
  export type Scenario = {
3
3
  name: string;
4
4
  group: string;
@@ -42,6 +42,7 @@ const catalog_1 = require("../catalog");
42
42
  const config_1 = require("../config");
43
43
  const dto_fields_1 = require("../fields/dto-fields");
44
44
  const path_util_1 = require("../path.util");
45
+ const recorder_1 = require("../recorder");
45
46
  const paths_1 = require("../serve/paths");
46
47
  const viewer_store_1 = require("../serve/viewer-store");
47
48
  const METHOD_RANK = { get: 0, post: 1, put: 2, patch: 3, delete: 4 };
@@ -403,33 +404,48 @@ function dedupeScenarios(scenarios) {
403
404
  return true;
404
405
  });
405
406
  }
406
- function loadHits() {
407
- if (!(0, fs_1.existsSync)((0, paths_1.getOutDir)()))
407
+ /** `hits-w{workerId}-p{pid}.json` leftover from a previous Jest run must not stack. */
408
+ function currentRunWorkerFiles(outDir) {
409
+ const names = (0, fs_1.readdirSync)(outDir).filter((fileName) => fileName.startsWith('hits-w') && fileName.endsWith('.json'));
410
+ if (!names.length)
408
411
  return [];
409
- const workerFiles = (0, fs_1.readdirSync)((0, paths_1.getOutDir)()).filter((fileName) => fileName.startsWith('hits-w') && fileName.endsWith('.json'));
410
- if (workerFiles.length) {
411
- return canonicalizeHits(workerFiles.flatMap((fileName) => {
412
- try {
413
- const parsed = JSON.parse((0, fs_1.readFileSync)(path.join((0, paths_1.getOutDir)(), fileName), 'utf8'));
414
- return Array.isArray(parsed) ? parsed : [];
415
- }
416
- catch (error) {
417
- console.error(`DAxTA: skipped corrupt hits file ${fileName}:`, error);
418
- return [];
419
- }
420
- }));
412
+ const hitsJsonPath = (0, paths_1.getHitsJsonPath)();
413
+ const watermark = (0, fs_1.existsSync)(hitsJsonPath) ? (0, fs_1.statSync)(hitsJsonPath).mtimeMs - 1000 : 0;
414
+ const fromThisRun = names.filter((fileName) => (0, fs_1.statSync)(path.join(outDir, fileName)).mtimeMs >= watermark);
415
+ const pool = fromThisRun.length ? fromThisRun : names;
416
+ const newestByWorker = new Map();
417
+ for (const fileName of pool) {
418
+ const workerId = fileName.match(/^hits-w([^-]+)-p\d+\.json$/)?.[1] ?? fileName;
419
+ const mtime = (0, fs_1.statSync)(path.join(outDir, fileName)).mtimeMs;
420
+ const prev = newestByWorker.get(workerId);
421
+ if (!prev || mtime >= prev.mtime)
422
+ newestByWorker.set(workerId, { fileName, mtime });
421
423
  }
422
- // Fallback after workers were cleaned — rebuild from last merged hits.json
424
+ return [...newestByWorker.values()].map((entry) => entry.fileName);
425
+ }
426
+ function readHitFile(filePath, label) {
423
427
  try {
424
- if (!(0, fs_1.existsSync)((0, paths_1.getHitsJsonPath)()))
425
- return [];
426
- const parsed = JSON.parse((0, fs_1.readFileSync)((0, paths_1.getHitsJsonPath)(), 'utf8'));
427
- return canonicalizeHits(Array.isArray(parsed) ? parsed : []);
428
+ const parsed = JSON.parse((0, fs_1.readFileSync)(filePath, 'utf8'));
429
+ return Array.isArray(parsed) ? parsed : [];
428
430
  }
429
431
  catch (error) {
430
- console.error('DAxTA: skipped corrupt hits.json:', error);
432
+ console.error(`DAxTA: skipped corrupt hits file ${label}:`, error);
433
+ return [];
434
+ }
435
+ }
436
+ function loadHits() {
437
+ if (!(0, fs_1.existsSync)((0, paths_1.getOutDir)()))
431
438
  return [];
439
+ const workerFiles = currentRunWorkerFiles((0, paths_1.getOutDir)());
440
+ if (workerFiles.length) {
441
+ const loaded = canonicalizeHits(workerFiles.flatMap((fileName) => readHitFile(path.join((0, paths_1.getOutDir)(), fileName), fileName)));
442
+ if (loaded.length)
443
+ return loaded;
432
444
  }
445
+ // Fallback after workers were cleaned — last completed run's hits.json
446
+ if (!(0, fs_1.existsSync)((0, paths_1.getHitsJsonPath)()))
447
+ return [];
448
+ return canonicalizeHits(readHitFile((0, paths_1.getHitsJsonPath)(), 'hits.json'));
433
449
  }
434
450
  function sortOperations(entries) {
435
451
  return [...entries].sort((left, right) => {
@@ -737,13 +753,16 @@ function buildDaxtaSpec(options = {}) {
737
753
  const refreshHtml = writeHtml && (operationsChanged || metaChanged || viewerTemplateNewerThanHtml());
738
754
  const changed = operationsChanged || hitsChanged || metaChanged || refreshHtml;
739
755
  if (changed) {
740
- if (hitsChanged)
741
- writeJson((0, paths_1.getHitsJsonPath)(), hits);
756
+ writeJson((0, paths_1.getHitsJsonPath)(), hits);
742
757
  if (operationsChanged || metaChanged)
743
758
  writeJson((0, paths_1.getSpecJsonPath)(), spec);
744
759
  if (refreshHtml)
745
760
  (0, fs_1.writeFileSync)((0, paths_1.getHtmlPath)(), toStandaloneHtml(spec));
746
761
  }
762
+ else {
763
+ writeJson((0, paths_1.getHitsJsonPath)(), hits);
764
+ }
765
+ (0, recorder_1.clearWorkerHits)();
747
766
  const operations = countOperations(spec);
748
767
  const result = {
749
768
  skipped: false,
@@ -6,7 +6,8 @@ export declare function resolveJestConfigPath(cwd: string, options: {
6
6
  }): string | null;
7
7
  /**
8
8
  * Inject DAxTA as a Jest plugin (setup + reporter). Does not wrap the test process.
9
- * Supports JSON and JS/CJS/MJS/TS config files. Leaves globalSetup / globalTeardown untouched.
9
+ * Supports JSON and JS/CJS/MJS/TS config files. Adds globalSetup only when missing
10
+ * (does not replace an existing user globalSetup).
10
11
  */
11
12
  export declare function injectJestHooks(jestConfigPath: string, dryRun?: boolean): string[];
12
13
  /** Restore package.json script if it was wrapped as `daxta test --yes`. */
@@ -43,6 +43,7 @@ const path = __importStar(require("path"));
43
43
  const discover_test_1 = require("./discover-test");
44
44
  const SETUP = '@t0.labs/daxta/setup';
45
45
  const REPORTER = '@t0.labs/daxta/jest-reporter';
46
+ const GLOBAL_SETUP = '@t0.labs/daxta/jest-global-setup';
46
47
  function loadJestJson(filePath) {
47
48
  return JSON.parse((0, fs_1.readFileSync)(filePath, 'utf8'));
48
49
  }
@@ -135,6 +136,13 @@ function injectIntoJsSource(source) {
135
136
  notes.push(`setupFilesAfterEnv = [${SETUP}]`);
136
137
  }
137
138
  }
139
+ if (!/\bglobalSetup\s*:/.test(next) && !next.includes(GLOBAL_SETUP)) {
140
+ const inserted = insertIntoObjectLiteral(next, `globalSetup: '${GLOBAL_SETUP}',`);
141
+ if (inserted) {
142
+ next = inserted;
143
+ notes.push(`globalSetup = ${GLOBAL_SETUP}`);
144
+ }
145
+ }
138
146
  if (!next.includes(REPORTER)) {
139
147
  const range = findArrayPropRange(next, 'reporters');
140
148
  if (range) {
@@ -177,6 +185,10 @@ function injectIntoJsonConfig(filePath, dryRun) {
177
185
  data.reporters = reporters;
178
186
  notes.push(`reporters += ${REPORTER}`);
179
187
  }
188
+ if (!data.globalSetup) {
189
+ data.globalSetup = GLOBAL_SETUP;
190
+ notes.push(`globalSetup = ${GLOBAL_SETUP}`);
191
+ }
180
192
  if (!dryRun && notes.length) {
181
193
  (0, fs_1.writeFileSync)(filePath, `${JSON.stringify(data, null, 2)}\n`);
182
194
  }
@@ -184,7 +196,8 @@ function injectIntoJsonConfig(filePath, dryRun) {
184
196
  }
185
197
  /**
186
198
  * Inject DAxTA as a Jest plugin (setup + reporter). Does not wrap the test process.
187
- * Supports JSON and JS/CJS/MJS/TS config files. Leaves globalSetup / globalTeardown untouched.
199
+ * Supports JSON and JS/CJS/MJS/TS config files. Adds globalSetup only when missing
200
+ * (does not replace an existing user globalSetup).
188
201
  */
189
202
  function injectJestHooks(jestConfigPath, dryRun = false) {
190
203
  if (!(0, fs_1.existsSync)(jestConfigPath))
@@ -261,6 +274,11 @@ function removeFromJsSource(source) {
261
274
  if (plain.removed)
262
275
  notes.push(`reporters -= ${REPORTER}`);
263
276
  }
277
+ const globalSetupRe = new RegExp(`\\s*globalSetup\\s*:\\s*['"]${GLOBAL_SETUP.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}['"]\\s*,?`);
278
+ if (globalSetupRe.test(next)) {
279
+ next = next.replace(globalSetupRe, '');
280
+ notes.push(`globalSetup -= ${GLOBAL_SETUP}`);
281
+ }
264
282
  return { source: next, notes };
265
283
  }
266
284
  function removeFromJsonConfig(filePath, dryRun) {
@@ -288,6 +306,10 @@ function removeFromJsonConfig(filePath, dryRun) {
288
306
  notes.push(`reporters -= ${REPORTER}`);
289
307
  }
290
308
  }
309
+ if (data.globalSetup === GLOBAL_SETUP) {
310
+ delete data.globalSetup;
311
+ notes.push(`globalSetup -= ${GLOBAL_SETUP}`);
312
+ }
291
313
  if (!dryRun && notes.length) {
292
314
  (0, fs_1.writeFileSync)(filePath, `${JSON.stringify(data, null, 2)}\n`);
293
315
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t0.labs/daxta",
3
- "version": "0.4.9",
3
+ "version": "0.4.11",
4
4
  "description": "DAxTA \u2014 docs from Nest integration traffic + DTO field CLI / Try-it",
5
5
  "license": "MIT",
6
6
  "repository": {