@open-mercato/cli 0.11.1-develop.8589.1.db1a76ea60 → 0.11.1-develop.8593.1.50784dae35

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.
Files changed (133) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/AGENTS.md +1 -0
  3. package/dist/agentic/guides/module-facts.json +164 -164
  4. package/dist/agentic/guides/module-facts.v2.json +164 -164
  5. package/dist/agentic/guides/modules/agent_orchestrator/index.md +1 -1
  6. package/dist/agentic/guides/modules/ai_assistant/index.md +1 -1
  7. package/dist/agentic/guides/modules/api_docs/index.md +1 -1
  8. package/dist/agentic/guides/modules/api_keys/index.md +1 -1
  9. package/dist/agentic/guides/modules/attachments/index.md +1 -1
  10. package/dist/agentic/guides/modules/audit_logs/index.md +1 -1
  11. package/dist/agentic/guides/modules/auth/index.md +1 -1
  12. package/dist/agentic/guides/modules/availability/index.md +1 -1
  13. package/dist/agentic/guides/modules/backups/index.md +1 -1
  14. package/dist/agentic/guides/modules/business_rules/index.md +1 -1
  15. package/dist/agentic/guides/modules/carrier_inpost/index.md +1 -1
  16. package/dist/agentic/guides/modules/cart/index.md +1 -1
  17. package/dist/agentic/guides/modules/catalog/index.md +1 -1
  18. package/dist/agentic/guides/modules/channel_apns/index.md +1 -1
  19. package/dist/agentic/guides/modules/channel_discord/index.md +1 -1
  20. package/dist/agentic/guides/modules/channel_expo/index.md +1 -1
  21. package/dist/agentic/guides/modules/channel_fcm/index.md +1 -1
  22. package/dist/agentic/guides/modules/channel_gmail/index.md +1 -1
  23. package/dist/agentic/guides/modules/channel_imap/index.md +1 -1
  24. package/dist/agentic/guides/modules/channel_resend/index.md +1 -1
  25. package/dist/agentic/guides/modules/channel_ses/index.md +1 -1
  26. package/dist/agentic/guides/modules/channel_smtp/index.md +1 -1
  27. package/dist/agentic/guides/modules/checkout/index.md +1 -1
  28. package/dist/agentic/guides/modules/communication_channels/index.md +1 -1
  29. package/dist/agentic/guides/modules/configs/index.md +1 -1
  30. package/dist/agentic/guides/modules/content/index.md +1 -1
  31. package/dist/agentic/guides/modules/currencies/index.md +1 -1
  32. package/dist/agentic/guides/modules/customer_accounts/index.md +1 -1
  33. package/dist/agentic/guides/modules/customer_groups/index.md +1 -1
  34. package/dist/agentic/guides/modules/customers/index.md +1 -1
  35. package/dist/agentic/guides/modules/dashboards/index.md +1 -1
  36. package/dist/agentic/guides/modules/data_sync/index.md +1 -1
  37. package/dist/agentic/guides/modules/design_system/index.md +1 -1
  38. package/dist/agentic/guides/modules/devices/index.md +1 -1
  39. package/dist/agentic/guides/modules/dictionaries/index.md +1 -1
  40. package/dist/agentic/guides/modules/directory/index.md +1 -1
  41. package/dist/agentic/guides/modules/documents/index.md +1 -1
  42. package/dist/agentic/guides/modules/ecommerce/index.md +1 -1
  43. package/dist/agentic/guides/modules/entities/index.md +1 -1
  44. package/dist/agentic/guides/modules/eudr/index.md +1 -1
  45. package/dist/agentic/guides/modules/events/index.md +1 -1
  46. package/dist/agentic/guides/modules/feature_toggles/index.md +1 -1
  47. package/dist/agentic/guides/modules/forms/index.md +1 -1
  48. package/dist/agentic/guides/modules/gateway_autopay/index.md +1 -1
  49. package/dist/agentic/guides/modules/gateway_stripe/index.md +1 -1
  50. package/dist/agentic/guides/modules/gateway_tpay/index.md +1 -1
  51. package/dist/agentic/guides/modules/generators/index.md +1 -1
  52. package/dist/agentic/guides/modules/inbox_ops/index.md +1 -1
  53. package/dist/agentic/guides/modules/integrations/index.md +1 -1
  54. package/dist/agentic/guides/modules/messages/index.md +1 -1
  55. package/dist/agentic/guides/modules/notifications/index.md +1 -1
  56. package/dist/agentic/guides/modules/onboarding/index.md +1 -1
  57. package/dist/agentic/guides/modules/payment_gateways/index.md +1 -1
  58. package/dist/agentic/guides/modules/perspectives/index.md +1 -1
  59. package/dist/agentic/guides/modules/phone_calls/index.md +1 -1
  60. package/dist/agentic/guides/modules/planner/index.md +1 -1
  61. package/dist/agentic/guides/modules/portal/index.md +1 -1
  62. package/dist/agentic/guides/modules/progress/index.md +1 -1
  63. package/dist/agentic/guides/modules/promotions/index.md +1 -1
  64. package/dist/agentic/guides/modules/push_notifications/index.md +1 -1
  65. package/dist/agentic/guides/modules/query_index/index.md +1 -1
  66. package/dist/agentic/guides/modules/record_locks/index.md +1 -1
  67. package/dist/agentic/guides/modules/resources/index.md +1 -1
  68. package/dist/agentic/guides/modules/sales/index.md +1 -1
  69. package/dist/agentic/guides/modules/scheduler/index.md +1 -1
  70. package/dist/agentic/guides/modules/search/index.md +1 -1
  71. package/dist/agentic/guides/modules/security/index.md +1 -1
  72. package/dist/agentic/guides/modules/seeds/index.md +1 -1
  73. package/dist/agentic/guides/modules/shipping_carriers/index.md +1 -1
  74. package/dist/agentic/guides/modules/sso/index.md +1 -1
  75. package/dist/agentic/guides/modules/staff/index.md +1 -1
  76. package/dist/agentic/guides/modules/storage_s3/index.md +1 -1
  77. package/dist/agentic/guides/modules/sync_akeneo/index.md +1 -1
  78. package/dist/agentic/guides/modules/sync_excel/index.md +1 -1
  79. package/dist/agentic/guides/modules/system_status_overlays/index.md +1 -1
  80. package/dist/agentic/guides/modules/telemetry/index.md +1 -1
  81. package/dist/agentic/guides/modules/tillio/index.md +1 -1
  82. package/dist/agentic/guides/modules/translations/index.md +1 -1
  83. package/dist/agentic/guides/modules/warranty_claims/index.md +1 -1
  84. package/dist/agentic/guides/modules/webhooks/index.md +1 -1
  85. package/dist/agentic/guides/modules/wms/index.md +1 -1
  86. package/dist/agentic/guides/modules/workflows/index.md +1 -1
  87. package/dist/agentic/guides/reference-module-facts.json +1 -1
  88. package/dist/agentic/guides/upstream/manifest.json +1 -1
  89. package/dist/lib/generate-child-process.js +21 -3
  90. package/dist/lib/generate-child-process.js.map +2 -2
  91. package/dist/lib/generate-watch-events.js +135 -19
  92. package/dist/lib/generate-watch-events.js.map +2 -2
  93. package/dist/lib/generate-watch-plan.js +166 -0
  94. package/dist/lib/generate-watch-plan.js.map +7 -0
  95. package/dist/lib/generate-watch-runner.js +53 -0
  96. package/dist/lib/generate-watch-runner.js.map +7 -0
  97. package/dist/lib/generate-watch-structure.js +576 -203
  98. package/dist/lib/generate-watch-structure.js.map +3 -3
  99. package/dist/lib/generators/extensions/agent-files.js +1 -0
  100. package/dist/lib/generators/extensions/agent-files.js.map +2 -2
  101. package/dist/lib/generators/module-registry.js +148 -191
  102. package/dist/lib/generators/module-registry.js.map +3 -3
  103. package/dist/lib/generators/openapi.js +24 -1
  104. package/dist/lib/generators/openapi.js.map +2 -2
  105. package/dist/lib/generators/web-research-adapters.js +54 -22
  106. package/dist/lib/generators/web-research-adapters.js.map +2 -2
  107. package/dist/lib/in-process-generate-watcher.js +98 -75
  108. package/dist/lib/in-process-generate-watcher.js.map +2 -2
  109. package/dist/mercato.js +74 -46
  110. package/dist/mercato.js.map +2 -2
  111. package/package.json +7 -7
  112. package/src/lib/__tests__/generate-child-process.test.ts +30 -0
  113. package/src/lib/__tests__/generate-watch-events.test.ts +377 -8
  114. package/src/lib/__tests__/generate-watch-plan.test.ts +98 -0
  115. package/src/lib/__tests__/generate-watch-runner.test.ts +126 -0
  116. package/src/lib/__tests__/generate-watch-structure.test.ts +522 -1
  117. package/src/lib/__tests__/in-process-generate-watcher.test.ts +393 -6
  118. package/src/lib/__tests__/module-inspection.test.ts +17 -11
  119. package/src/lib/generate-child-process.ts +26 -3
  120. package/src/lib/generate-watch-events.ts +189 -17
  121. package/src/lib/generate-watch-plan.ts +177 -0
  122. package/src/lib/generate-watch-runner.ts +62 -0
  123. package/src/lib/generate-watch-structure.ts +666 -227
  124. package/src/lib/generators/__tests__/incremental-registry.test.ts +262 -0
  125. package/src/lib/generators/__tests__/openapi.test.ts +91 -1
  126. package/src/lib/generators/__tests__/web-research-adapters.test.ts +298 -87
  127. package/src/lib/generators/extension.ts +2 -0
  128. package/src/lib/generators/extensions/agent-files.ts +1 -0
  129. package/src/lib/generators/module-registry.ts +168 -200
  130. package/src/lib/generators/openapi.ts +34 -0
  131. package/src/lib/generators/web-research-adapters.ts +76 -21
  132. package/src/lib/in-process-generate-watcher.ts +151 -96
  133. package/src/mercato.ts +85 -53
@@ -2,6 +2,9 @@ import {
2
2
  startInProcessGenerateWatcher,
3
3
  type GenerateWatcherChangeSignal,
4
4
  } from '../in-process-generate-watcher'
5
+ import { planGenerateWatchChanges } from '../generate-watch-plan'
6
+ import type { GenerateWatchCategory, GenerateWatchPlan, GenerateWatchSnapshot } from '../generate-watch-plan'
7
+ import { diffGenerateWatchStructureSnapshots } from '../generate-watch-structure'
5
8
 
6
9
  const silentLogger = { log: jest.fn(), error: jest.fn() }
7
10
 
@@ -246,7 +249,7 @@ describe('startInProcessGenerateWatcher', () => {
246
249
  await handle.close()
247
250
  })
248
251
 
249
- it('retries a dirty checksum after a transient checksum failure', async () => {
252
+ it('waits for another event before recapturing after a transient checksum failure', async () => {
250
253
  const runGenerators = jest.fn(async () => undefined)
251
254
  const computeStructureChecksum = jest.fn()
252
255
  .mockResolvedValueOnce('before')
@@ -267,12 +270,22 @@ describe('startInProcessGenerateWatcher', () => {
267
270
  markChanged()
268
271
  jest.advanceTimersByTime(1000)
269
272
  await flushAsync(8)
273
+ expect(computeStructureChecksum).toHaveBeenCalledTimes(2)
274
+ expect(runGenerators).not.toHaveBeenCalled()
275
+
276
+ for (let poll = 0; poll < 3; poll += 1) {
277
+ jest.advanceTimersByTime(1000)
278
+ await flushAsync(8)
279
+ }
280
+ expect(computeStructureChecksum).toHaveBeenCalledTimes(2)
270
281
  expect(runGenerators).not.toHaveBeenCalled()
271
282
 
283
+ markChanged()
272
284
  jest.advanceTimersByTime(1000)
273
285
  await flushAsync(12)
274
286
  expect(computeStructureChecksum).toHaveBeenCalledTimes(3)
275
287
  expect(runGenerators).toHaveBeenCalledTimes(1)
288
+ expect(runGenerators).toHaveBeenCalledWith('retry after uncertain structure snapshot')
276
289
  await handle.close()
277
290
  })
278
291
 
@@ -368,9 +381,7 @@ describe('startInProcessGenerateWatcher', () => {
368
381
  await flushAsync(8)
369
382
  expect(runGenerators).toHaveBeenCalledTimes(1)
370
383
 
371
- // Tick #2 and #3 must NOT spawn parallel runs while #1 is in flight —
372
- // the helper serializes via the running/pending pair plus the
373
- // single-shot finally-scheduled poll cycle.
384
+ // Single-shot scheduling must not overlap suites while the first is in flight.
374
385
  currentChecksum = 'c'
375
386
  jest.advanceTimersByTime(1000)
376
387
  await flushAsync(8)
@@ -404,7 +415,6 @@ describe('startInProcessGenerateWatcher', () => {
404
415
  expect(runGenerators).toHaveBeenCalledTimes(1)
405
416
  expect(silentLogger.error).toHaveBeenCalled()
406
417
  const message = String(silentLogger.error.mock.calls[0][0] ?? '')
407
- expect(message).toMatch(/Generation failed/)
408
418
  expect(message).toMatch(/boom/)
409
419
 
410
420
  await handle.close()
@@ -519,6 +529,383 @@ describe('startInProcessGenerateWatcher', () => {
519
529
  await handle.close()
520
530
  await handle.close()
521
531
  await handle.done
522
- expect(true).toBe(true)
532
+ jest.advanceTimersByTime(5000)
533
+ await flushAsync(12)
534
+ expect(computeStructureChecksum).toHaveBeenCalledTimes(1)
535
+ expect(runGenerators).not.toHaveBeenCalled()
536
+ })
537
+ })
538
+
539
+ function deferred<T>() {
540
+ let resolve!: (value: T) => void
541
+ const promise = new Promise<T>((fulfill) => { resolve = fulfill })
542
+ return { promise, resolve }
543
+ }
544
+
545
+ function snapshot(entries: Array<[string, GenerateWatchCategory, string]>): GenerateWatchSnapshot {
546
+ return {
547
+ checksum: JSON.stringify(entries),
548
+ records: new Map(entries.map(([key, category, fingerprint]) => [
549
+ key,
550
+ { key, category, fingerprint, path: key },
551
+ ])),
552
+ fullReasons: [],
553
+ }
554
+ }
555
+
556
+ describe('incremental generate watcher lifecycle', () => {
557
+ beforeEach(() => {
558
+ jest.useFakeTimers()
559
+ silentLogger.log.mockReset()
560
+ silentLogger.error.mockReset()
561
+ })
562
+
563
+ afterEach(() => { jest.useRealTimers() })
564
+
565
+ function harness(skipInitial = true) {
566
+ let current = snapshot([['api/item.ts', 'api-route', 'before']])
567
+ const signalHarness = createFakeChangeSignal()
568
+ const capture = jest.fn(async () => current)
569
+ const plan = jest.fn((previous: GenerateWatchSnapshot, next: GenerateWatchSnapshot) =>
570
+ planGenerateWatchChanges(diffGenerateWatchStructureSnapshots(previous, next), next.fullReasons))
571
+ const runGenerators = jest.fn<Promise<void>, [string, GenerateWatchPlan?]>(async () => undefined)
572
+ const computeStructureChecksum = jest.fn(() => { throw new Error('legacy capture must not run') })
573
+ const options = {
574
+ skipInitial,
575
+ quiet: true,
576
+ logger: silentLogger,
577
+ pollMs: 250,
578
+ computeStructureChecksum,
579
+ changeSignal: signalHarness.signal,
580
+ incremental: { capture, plan },
581
+ runGenerators,
582
+ }
583
+ return {
584
+ ...signalHarness,
585
+ options,
586
+ runGenerators,
587
+ capture,
588
+ plan,
589
+ update: (next: GenerateWatchSnapshot) => { current = next; signalHarness.markChanged() },
590
+ setSnapshot: (next: GenerateWatchSnapshot) => { current = next },
591
+ }
592
+ }
593
+
594
+ it('event-gates a failed partial suite and repairs fully after the next event', async () => {
595
+ const h = harness()
596
+ h.runGenerators.mockRejectedValueOnce(new Error('partial write'))
597
+ const handle = startInProcessGenerateWatcher(h.options)
598
+ await flushAsync(12)
599
+ h.update(snapshot([['api/item.ts', 'api-route', 'after']]))
600
+ jest.advanceTimersByTime(250)
601
+ await flushAsync(16)
602
+ expect(h.runGenerators.mock.calls[0][1]?.mode).toBe('incremental')
603
+ // The candidate must not be acknowledged. A reverted tree can still have
604
+ // partially updated outputs and therefore needs a full repair.
605
+ h.capture.mockResolvedValue(snapshot([['api/item.ts', 'api-route', 'before']]))
606
+ for (let poll = 0; poll < 3; poll += 1) {
607
+ jest.advanceTimersByTime(250)
608
+ await flushAsync(12)
609
+ }
610
+ expect(h.capture).toHaveBeenCalledTimes(2)
611
+ expect(h.runGenerators).toHaveBeenCalledTimes(1)
612
+
613
+ h.markChanged()
614
+ jest.advanceTimersByTime(250)
615
+ await flushAsync(16)
616
+ expect(h.runGenerators).toHaveBeenCalledTimes(2)
617
+ expect(h.runGenerators.mock.calls[1][1]).toMatchObject({
618
+ mode: 'full',
619
+ reasons: expect.arrayContaining(['retry after failed generation']),
620
+ })
621
+ jest.advanceTimersByTime(250)
622
+ await flushAsync(12)
623
+ expect(h.runGenerators).toHaveBeenCalledTimes(2)
624
+ await handle.close()
625
+ })
626
+
627
+ it('waits for an event before repairing a failed initial generation', async () => {
628
+ const h = harness(false)
629
+ h.runGenerators.mockRejectedValueOnce(new Error('initial failed'))
630
+ const handle = startInProcessGenerateWatcher(h.options)
631
+ await flushAsync(16)
632
+ for (let poll = 0; poll < 3; poll += 1) {
633
+ jest.advanceTimersByTime(250)
634
+ await flushAsync(12)
635
+ }
636
+ expect(h.runGenerators.mock.calls.map(([, plan]) => plan?.mode)).toEqual(['full'])
637
+
638
+ h.markChanged()
639
+ jest.advanceTimersByTime(250)
640
+ await flushAsync(16)
641
+ expect(h.runGenerators.mock.calls.map(([, plan]) => plan?.mode)).toEqual(['full', 'full'])
642
+ expect(h.runGenerators.mock.calls[1][1]?.reasons).toContain('retry after failed generation')
643
+ await handle.close()
644
+ })
645
+
646
+ it('preserves edits made during initial generation', async () => {
647
+ const h = harness(false)
648
+ const initial = deferred<void>()
649
+ h.runGenerators.mockImplementationOnce(() => initial.promise)
650
+ const handle = startInProcessGenerateWatcher(h.options)
651
+ await flushAsync(12)
652
+ h.update(snapshot([
653
+ ['api/item.ts', 'api-route', 'before'],
654
+ ['di.ts', 'di', 'new'],
655
+ ]))
656
+ jest.advanceTimersByTime(1000)
657
+ await flushAsync(12)
658
+ expect(h.runGenerators).toHaveBeenCalledTimes(1)
659
+ initial.resolve()
660
+ await flushAsync(16)
661
+ jest.advanceTimersByTime(250)
662
+ await flushAsync(16)
663
+ expect(h.runGenerators).toHaveBeenCalledTimes(2)
664
+ expect(h.runGenerators.mock.calls[1][1]?.changes).toEqual([
665
+ expect.objectContaining({ kind: 'add', category: 'di', key: 'di.ts' }),
666
+ ])
667
+ await handle.close()
668
+ })
669
+
670
+ it('unions deletions and additions during a running suite against its successful candidate', async () => {
671
+ const h = harness()
672
+ const first = deferred<void>()
673
+ h.runGenerators.mockImplementationOnce(() => first.promise)
674
+ const handle = startInProcessGenerateWatcher(h.options)
675
+ await flushAsync(12)
676
+ h.update(snapshot([['api/item.ts', 'api-route', 'after']]))
677
+ jest.advanceTimersByTime(250)
678
+ await flushAsync(12)
679
+ h.update(snapshot([]))
680
+ h.update(snapshot([['search.ts', 'search', 'new']]))
681
+ h.update(snapshot([
682
+ ['search.ts', 'search', 'new'],
683
+ ['di.ts', 'di', 'new'],
684
+ ]))
685
+ jest.advanceTimersByTime(1000)
686
+ await flushAsync(12)
687
+ expect(h.runGenerators).toHaveBeenCalledTimes(1)
688
+ first.resolve()
689
+ await flushAsync(16)
690
+ jest.advanceTimersByTime(250)
691
+ await flushAsync(16)
692
+ const nextPlan = h.runGenerators.mock.calls[1][1]
693
+ expect(nextPlan?.changes).toEqual(expect.arrayContaining([
694
+ expect.objectContaining({ kind: 'delete', category: 'api-route' }),
695
+ expect.objectContaining({ kind: 'add', category: 'search' }),
696
+ expect.objectContaining({ kind: 'add', category: 'di' }),
697
+ ]))
698
+ expect(nextPlan?.changes).toHaveLength(3)
699
+ expect(nextPlan?.groups).toEqual(expect.arrayContaining(['registry', 'di', 'openapi']))
700
+ await handle.close()
701
+ })
702
+
703
+ it('keeps events received during asynchronous capture dirty until the next capture', async () => {
704
+ const h = harness()
705
+ const capture = deferred<GenerateWatchSnapshot>()
706
+ const handle = startInProcessGenerateWatcher(h.options)
707
+ await flushAsync(12)
708
+ h.capture.mockImplementationOnce(() => capture.promise)
709
+ h.markChanged()
710
+ jest.advanceTimersByTime(250)
711
+ await flushAsync(12)
712
+ h.update(snapshot([['di.ts', 'di', 'new']]))
713
+ capture.resolve(snapshot([['api/item.ts', 'api-route', 'after']]))
714
+ await flushAsync(16)
715
+ jest.advanceTimersByTime(250)
716
+ await flushAsync(16)
717
+ expect(h.runGenerators).toHaveBeenCalledTimes(2)
718
+ expect(h.runGenerators.mock.calls[1][1]?.changes).toEqual(expect.arrayContaining([
719
+ expect.objectContaining({ kind: 'delete', category: 'api-route' }),
720
+ expect.objectContaining({ kind: 'add', category: 'di' }),
721
+ ]))
722
+ await handle.close()
723
+ })
724
+
725
+ it('keeps unchanged polling snapshots idle but uses a full plan when inputs change', async () => {
726
+ const h = harness()
727
+ h.usePollingFallback()
728
+ const handle = startInProcessGenerateWatcher(h.options)
729
+ await flushAsync(12)
730
+ h.markChanged()
731
+ jest.advanceTimersByTime(250)
732
+ await flushAsync(16)
733
+ expect(h.runGenerators).not.toHaveBeenCalled()
734
+ h.update(snapshot([['api/item.ts', 'api-route', 'after']]))
735
+ jest.advanceTimersByTime(250)
736
+ await flushAsync(16)
737
+ expect(h.runGenerators.mock.calls[0][1]?.mode).toBe('full')
738
+ await handle.close()
739
+ })
740
+
741
+ it('does not rerun a failed polling candidate until the snapshot changes', async () => {
742
+ const h = harness()
743
+ h.usePollingFallback()
744
+ h.runGenerators.mockRejectedValueOnce(new Error('failed polling suite'))
745
+ const handle = startInProcessGenerateWatcher(h.options)
746
+ await flushAsync(12)
747
+
748
+ h.setSnapshot(snapshot([['api/item.ts', 'api-route', 'after']]))
749
+ jest.advanceTimersByTime(250)
750
+ await flushAsync(16)
751
+ expect(h.runGenerators).toHaveBeenCalledTimes(1)
752
+
753
+ for (let poll = 0; poll < 3; poll += 1) {
754
+ jest.advanceTimersByTime(250)
755
+ await flushAsync(12)
756
+ }
757
+ expect(h.runGenerators).toHaveBeenCalledTimes(1)
758
+
759
+ h.setSnapshot(snapshot([['api/item.ts', 'api-route', 'later']]))
760
+ jest.advanceTimersByTime(250)
761
+ await flushAsync(16)
762
+ expect(h.runGenerators).toHaveBeenCalledTimes(2)
763
+ expect(h.runGenerators.mock.calls[1][1]).toMatchObject({
764
+ mode: 'full',
765
+ reasons: expect.arrayContaining(['retry after failed generation']),
766
+ })
767
+ await handle.close()
768
+ })
769
+
770
+ it('runs one full suite for an unattributed event even when the tracked checksum is unchanged', async () => {
771
+ const h = harness()
772
+ let unknownVersion = -1
773
+ h.options.changeSignal.fullGenerationReasonSince = (version) => unknownVersion > version ? 'unknown event' : undefined
774
+ const handle = startInProcessGenerateWatcher(h.options)
775
+ await flushAsync(12)
776
+ h.markChanged()
777
+ unknownVersion = h.signal.currentVersion()
778
+ jest.advanceTimersByTime(250)
779
+ await flushAsync(16)
780
+ expect(h.runGenerators).toHaveBeenCalledTimes(1)
781
+ expect(h.runGenerators.mock.calls[0][1]).toMatchObject({
782
+ mode: 'full',
783
+ reasons: expect.arrayContaining(['unknown event']),
784
+ })
785
+ jest.advanceTimersByTime(250)
786
+ await flushAsync(16)
787
+ expect(h.runGenerators).toHaveBeenCalledTimes(1)
788
+ await handle.close()
789
+ })
790
+
791
+ it('acknowledges conservative plugin records after successful generation', async () => {
792
+ const h = harness(false)
793
+ h.usePollingFallback()
794
+ h.capture.mockResolvedValue(snapshot([['generators.ts', 'generator-plugin', 'dynamic-conventions']]))
795
+ const handle = startInProcessGenerateWatcher(h.options)
796
+ await flushAsync(16)
797
+ jest.advanceTimersByTime(250)
798
+ await flushAsync(16)
799
+ jest.advanceTimersByTime(250)
800
+ await flushAsync(16)
801
+ expect(h.runGenerators).toHaveBeenCalledTimes(1)
802
+ await handle.close()
803
+ })
804
+
805
+ it('does not generate after close interrupts a pending capture', async () => {
806
+ const h = harness()
807
+ const capture = deferred<GenerateWatchSnapshot>()
808
+ h.capture.mockImplementationOnce(() => capture.promise)
809
+ const handle = startInProcessGenerateWatcher(h.options)
810
+ await flushAsync(12)
811
+ const closing = handle.close()
812
+ capture.resolve(snapshot([['di.ts', 'di', 'new']]))
813
+ await closing
814
+ jest.advanceTimersByTime(1000)
815
+ await flushAsync(12)
816
+ expect(h.runGenerators).not.toHaveBeenCalled()
817
+ expect(h.capture).toHaveBeenCalledTimes(1)
818
+ })
819
+
820
+ it('waits for the active suite when closing and never starts queued work', async () => {
821
+ const h = harness(false)
822
+ const generation = deferred<void>()
823
+ h.runGenerators.mockImplementationOnce(() => generation.promise)
824
+ const handle = startInProcessGenerateWatcher(h.options)
825
+ await flushAsync(12)
826
+ h.update(snapshot([['di.ts', 'di', 'new']]))
827
+ let closed = false
828
+ const closing = handle.close().then(() => { closed = true })
829
+ await flushAsync(12)
830
+ expect(closed).toBe(false)
831
+ generation.resolve()
832
+ await closing
833
+ expect(closed).toBe(true)
834
+ jest.advanceTimersByTime(1000)
835
+ await flushAsync(12)
836
+ expect(h.runGenerators).toHaveBeenCalledTimes(1)
837
+ })
838
+
839
+ it('event-gates checksum-only retries without changing callback arity', async () => {
840
+ const runGenerators = jest.fn<Promise<void>, [string]>()
841
+ .mockRejectedValueOnce(new Error('failed full suite'))
842
+ .mockResolvedValue(undefined)
843
+ const { signal, markChanged } = createFakeChangeSignal()
844
+ const handle = startInProcessGenerateWatcher({
845
+ pollMs: 250,
846
+ logger: silentLogger,
847
+ quiet: true,
848
+ changeSignal: signal,
849
+ computeStructureChecksum: () => 'same-input',
850
+ runGenerators,
851
+ })
852
+ await flushAsync(16)
853
+ for (let poll = 0; poll < 3; poll += 1) {
854
+ jest.advanceTimersByTime(250)
855
+ await flushAsync(12)
856
+ }
857
+ expect(runGenerators.mock.calls).toEqual([
858
+ ['initial'],
859
+ ])
860
+
861
+ markChanged()
862
+ jest.advanceTimersByTime(250)
863
+ await flushAsync(16)
864
+ expect(runGenerators.mock.calls).toEqual([
865
+ ['initial'],
866
+ ['retry after failed generation'],
867
+ ])
868
+ await handle.close()
869
+ })
870
+
871
+ it('event-gates uncertain snapshots and fully repairs each new version', async () => {
872
+ const h = harness(false)
873
+ const uncertain = { ...snapshot([]), fullReasons: ['unreadable source'] }
874
+ h.capture.mockResolvedValue(uncertain)
875
+ const handle = startInProcessGenerateWatcher(h.options)
876
+ await flushAsync(16)
877
+ expect(h.runGenerators).toHaveBeenCalledTimes(1)
878
+
879
+ for (let poll = 0; poll < 3; poll += 1) {
880
+ jest.advanceTimersByTime(250)
881
+ await flushAsync(12)
882
+ }
883
+ expect(h.capture).toHaveBeenCalledTimes(1)
884
+ expect(h.runGenerators).toHaveBeenCalledTimes(1)
885
+
886
+ h.markChanged()
887
+ jest.advanceTimersByTime(250)
888
+ await flushAsync(16)
889
+ expect(h.runGenerators).toHaveBeenCalledTimes(2)
890
+ expect(h.runGenerators.mock.calls[1][1]).toMatchObject({
891
+ mode: 'full',
892
+ reasons: expect.arrayContaining([
893
+ 'retry after uncertain structure snapshot',
894
+ 'unreadable source',
895
+ ]),
896
+ })
897
+ jest.advanceTimersByTime(250)
898
+ await flushAsync(12)
899
+ expect(h.runGenerators).toHaveBeenCalledTimes(2)
900
+
901
+ h.capture.mockResolvedValue(snapshot([]))
902
+ h.markChanged()
903
+ jest.advanceTimersByTime(250)
904
+ await flushAsync(16)
905
+ expect(h.runGenerators).toHaveBeenCalledTimes(3)
906
+ jest.advanceTimersByTime(250)
907
+ await flushAsync(12)
908
+ expect(h.runGenerators).toHaveBeenCalledTimes(3)
909
+ await handle.close()
523
910
  })
524
911
  })
@@ -7,7 +7,13 @@ import { inspectNpmModuleTarget } from '../module-inspection'
7
7
 
8
8
  describe('module artifact inspection', () => {
9
9
  let root: string
10
- beforeEach(() => { root = fs.mkdtempSync(path.join(os.tmpdir(), 'module-inspection-test-')) })
10
+ beforeEach(() => {
11
+ root = fs.mkdtempSync(path.join(os.tmpdir(), 'module-inspection-test-'))
12
+ jest.replaceProperty(process, 'env', {
13
+ ...process.env,
14
+ NPM_CONFIG_USERCONFIG: path.join(root, 'npm-user-config'),
15
+ })
16
+ })
11
17
  afterEach(() => { jest.restoreAllMocks(); fs.rmSync(root, { recursive: true, force: true }) })
12
18
 
13
19
  it('inspects large packages whose artifact indexes exceed the subprocess default buffer', () => {
@@ -150,8 +156,8 @@ describe('module artifact inspection', () => {
150
156
 
151
157
  expect(() => inspectNpmModuleTarget('@vendor/example@next', '@vendor/example', root)).toThrow('while trying to validate the packed module artifact')
152
158
  try { inspectNpmModuleTarget('@vendor/example@next', '@vendor/example', root) } catch (error) {
153
- expect(String(error)).toContain('@vendor/example/modules/example/index')
154
- expect(String(error)).not.toContain('private registry token')
159
+ expect(String(error).includes('@vendor/example/modules/example/index')).toBe(true)
160
+ expect(String(error).includes('private registry token')).toBe(false)
155
161
  }
156
162
  })
157
163
 
@@ -173,9 +179,9 @@ describe('module artifact inspection', () => {
173
179
  const values = args ?? []
174
180
  temporaryConfig = String(values[values.indexOf('--userconfig') + 1])
175
181
  expect(fs.statSync(temporaryConfig).mode & 0o777).toBe(0o600)
176
- expect(fs.readFileSync(temporaryConfig, 'utf8')).toContain('//private.example.test/npm/:_authToken="test-token-do-not-print"')
177
- expect(values).toContain('--@vendor:registry=https://private.example.test/npm/')
178
- expect(values.join(' ')).not.toContain('test-token-do-not-print')
182
+ expect(fs.readFileSync(temporaryConfig, 'utf8').includes('//private.example.test/npm/:_authToken="test-token-do-not-print"')).toBe(true)
183
+ expect(values.includes('--@vendor:registry=https://private.example.test/npm/')).toBe(true)
184
+ expect(values.some((value) => value.includes('test-token-do-not-print'))).toBe(false)
179
185
  return { status, stdout: JSON.stringify([{ name: '@vendor/example', version: '1.2.3', filename: 'candidate.tgz' }]), stderr: 'test-token-do-not-print', pid: 1, output: [], signal: null }
180
186
  })
181
187
  if (status === 0) expect(inspectNpmModuleTarget('@vendor/example@next', '@vendor/example', root).version).toBe('1.2.3')
@@ -212,8 +218,8 @@ describe('module artifact inspection', () => {
212
218
  }, (args, config) => { packArgs = args; npmrc = config })
213
219
  expect(inspectNpmModuleTarget('@vendor/example@1.2.3', '@vendor/example', root)).toMatchObject({ version: '1.2.3', moduleIds: ['example'] })
214
220
  expect(packArgs).toEqual(expect.arrayContaining(['--registry', 'https://registry.yarnpkg.com', '--@vendor:registry=https://registry.yarnpkg.com']))
215
- expect(npmrc).toContain('registry="https://registry.yarnpkg.com"')
216
- expect(npmrc).not.toContain('_auth')
221
+ expect(npmrc.includes('registry="https://registry.yarnpkg.com"')).toBe(true)
222
+ expect(npmrc.includes('_auth')).toBe(false)
217
223
  })
218
224
 
219
225
  it('keeps top-level Yarn authentication when npmScopes and npmRegistries are unset', () => {
@@ -227,7 +233,7 @@ describe('module artifact inspection', () => {
227
233
  npmAuthIdent: 'null\n',
228
234
  }, (_args, config) => { npmrc = config })
229
235
  expect(inspectNpmModuleTarget('@vendor/example@1.2.3', '@vendor/example', root).version).toBe('1.2.3')
230
- expect(npmrc).toContain('//registry.yarnpkg.com/:_authToken="test-token-do-not-print"')
236
+ expect(npmrc.includes('//registry.yarnpkg.com/:_authToken="test-token-do-not-print"')).toBe(true)
231
237
  })
232
238
 
233
239
  it('still fails closed on malformed Yarn configuration output', () => {
@@ -241,7 +247,7 @@ describe('module artifact inspection', () => {
241
247
  }, () => { throw new Error('npm pack must not run') })
242
248
  expect(() => inspectNpmModuleTarget('@vendor/example@1.2.3', '@vendor/example', root)).toThrow('while trying to read the project registry configuration. Yarn setting npmScopes returned output that is not JSON.')
243
249
  try { inspectNpmModuleTarget('@vendor/example@1.2.3', '@vendor/example', root) } catch (error) {
244
- expect(String(error)).not.toContain('test-token-do-not-print')
250
+ expect(String(error).includes('test-token-do-not-print')).toBe(false)
245
251
  }
246
252
  })
247
253
 
@@ -252,7 +258,7 @@ describe('module artifact inspection', () => {
252
258
  : { status: 0, stdout: 'undefined\n', stderr: '', pid: 1, output: [], signal: null })
253
259
  expect(() => inspectNpmModuleTarget('@vendor/example@1.2.3', '@vendor/example', root)).toThrow('Yarn setting npmRegistries could not be read (exit 1).')
254
260
  try { inspectNpmModuleTarget('@vendor/example@1.2.3', '@vendor/example', root) } catch (error) {
255
- expect(String(error)).not.toContain('test-token-do-not-print')
261
+ expect(String(error).includes('test-token-do-not-print')).toBe(false)
256
262
  }
257
263
  })
258
264
 
@@ -1,4 +1,17 @@
1
1
  import { spawn } from 'node:child_process'
2
+ import fs from 'node:fs'
3
+ import type { GenerateWatchPlan } from './generate-watch-plan'
4
+
5
+ type GenerateWatchRequest = {
6
+ plan?: GenerateWatchPlan
7
+ logCascade: boolean
8
+ }
9
+
10
+ export function readGenerateWatchRequest(): GenerateWatchRequest | undefined {
11
+ if (process.env.OM_INTERNAL_GENERATE_WATCH_REQUEST !== '1') return undefined
12
+ delete process.env.OM_INTERNAL_GENERATE_WATCH_REQUEST
13
+ return JSON.parse(fs.readFileSync(0, 'utf8')) as GenerateWatchRequest
14
+ }
2
15
 
3
16
  /**
4
17
  * Evaluate application modules in a disposable process, never in the watcher.
@@ -10,21 +23,31 @@ export async function runGenerateChildProcess(options: {
10
23
  cwd: string
11
24
  env?: NodeJS.ProcessEnv
12
25
  quiet?: boolean
26
+ watchRequest?: GenerateWatchRequest
13
27
  }): Promise<void> {
14
28
  const args = [options.mercatoBin, 'generate', 'all']
15
29
  if (options.quiet) args.push('--quiet')
16
30
 
17
31
  await new Promise<void>((resolve, reject) => {
32
+ let inputError: Error | undefined
18
33
  const child = spawn(process.execPath, args, {
19
34
  cwd: options.cwd,
20
- env: options.env ?? process.env,
21
- stdio: ['ignore', 'inherit', 'inherit'],
35
+ env: {
36
+ ...(options.env ?? process.env),
37
+ OM_INTERNAL_GENERATE_WATCH_REQUEST: options.watchRequest ? '1' : undefined,
38
+ },
39
+ stdio: [options.watchRequest ? 'pipe' : 'ignore', 'inherit', 'inherit'],
22
40
  })
23
41
  child.once('error', reject)
42
+ if (options.watchRequest) {
43
+ child.stdin?.on('error', (error: Error) => { inputError = error })
44
+ child.stdin?.end(JSON.stringify(options.watchRequest))
45
+ }
24
46
  // Wait for close, not just exit: generation and inherited stream cleanup
25
47
  // must finish before another cycle or supervisor restart can proceed.
26
48
  child.once('close', (code, signal) => {
27
- if (code === 0) resolve()
49
+ if (inputError) reject(inputError)
50
+ else if (code === 0) resolve()
28
51
  else reject(new Error(
29
52
  signal
30
53
  ? `Generator process terminated by ${signal}`