@holoscript/framework 6.0.3
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/ALL-test-results.json +1 -0
- package/CHANGELOG.md +8 -0
- package/LICENSE +21 -0
- package/ROADMAP.md +175 -0
- package/dist/AgentManifest-CB4xM-Ma.d.cts +704 -0
- package/dist/AgentManifest-CB4xM-Ma.d.ts +704 -0
- package/dist/BehaviorTree-BrBFECv5.d.cts +103 -0
- package/dist/BehaviorTree-BrBFECv5.d.ts +103 -0
- package/dist/InvisibleWallet-BB6tFvRA.d.cts +1732 -0
- package/dist/InvisibleWallet-rtRrBOA8.d.ts +1732 -0
- package/dist/OrchestratorAgent-BvWgf9uw.d.cts +798 -0
- package/dist/OrchestratorAgent-Q_CbVTmO.d.ts +798 -0
- package/dist/agents/index.cjs +4790 -0
- package/dist/agents/index.d.cts +1788 -0
- package/dist/agents/index.d.ts +1788 -0
- package/dist/agents/index.js +4695 -0
- package/dist/ai/index.cjs +5347 -0
- package/dist/ai/index.d.cts +1753 -0
- package/dist/ai/index.d.ts +1753 -0
- package/dist/ai/index.js +5244 -0
- package/dist/behavior.cjs +449 -0
- package/dist/behavior.d.cts +130 -0
- package/dist/behavior.d.ts +130 -0
- package/dist/behavior.js +407 -0
- package/dist/economy/index.cjs +3659 -0
- package/dist/economy/index.d.cts +747 -0
- package/dist/economy/index.d.ts +747 -0
- package/dist/economy/index.js +3617 -0
- package/dist/implementations-D9T3un9D.d.cts +236 -0
- package/dist/implementations-D9T3un9D.d.ts +236 -0
- package/dist/index.cjs +24550 -0
- package/dist/index.d.cts +1729 -0
- package/dist/index.d.ts +1729 -0
- package/dist/index.js +24277 -0
- package/dist/learning/index.cjs +219 -0
- package/dist/learning/index.d.cts +104 -0
- package/dist/learning/index.d.ts +104 -0
- package/dist/learning/index.js +189 -0
- package/dist/negotiation/index.cjs +970 -0
- package/dist/negotiation/index.d.cts +610 -0
- package/dist/negotiation/index.d.ts +610 -0
- package/dist/negotiation/index.js +931 -0
- package/dist/skills/index.cjs +1118 -0
- package/dist/skills/index.d.cts +289 -0
- package/dist/skills/index.d.ts +289 -0
- package/dist/skills/index.js +1079 -0
- package/dist/swarm/index.cjs +5268 -0
- package/dist/swarm/index.d.cts +2433 -0
- package/dist/swarm/index.d.ts +2433 -0
- package/dist/swarm/index.js +5221 -0
- package/dist/training/index.cjs +2745 -0
- package/dist/training/index.d.cts +1734 -0
- package/dist/training/index.d.ts +1734 -0
- package/dist/training/index.js +2687 -0
- package/extract-failures.js +10 -0
- package/package.json +82 -0
- package/src/__tests__/bounty-marketplace.test.ts +374 -0
- package/src/__tests__/delegation.test.ts +144 -0
- package/src/__tests__/distributed-claimer.test.ts +147 -0
- package/src/__tests__/done-log-audit.test.ts +342 -0
- package/src/__tests__/framework.test.ts +865 -0
- package/src/__tests__/goal-synthesizer.test.ts +236 -0
- package/src/__tests__/presence.test.ts +223 -0
- package/src/__tests__/protocol-agent.test.ts +254 -0
- package/src/__tests__/revenue-splitter.test.ts +114 -0
- package/src/__tests__/scenario-driven-todo.test.ts +197 -0
- package/src/__tests__/self-improve.test.ts +349 -0
- package/src/__tests__/service-lifecycle.test.ts +237 -0
- package/src/__tests__/skill-router.test.ts +121 -0
- package/src/agents/AgentManifest.ts +493 -0
- package/src/agents/AgentRegistry.ts +475 -0
- package/src/agents/AgentTypes.ts +585 -0
- package/src/agents/AgentWalletRegistry.ts +83 -0
- package/src/agents/AuthenticatedCRDT.ts +388 -0
- package/src/agents/CapabilityMatcher.ts +453 -0
- package/src/agents/CrossRealityHandoff.ts +305 -0
- package/src/agents/CulturalMemory.ts +454 -0
- package/src/agents/FederatedRegistryAdapter.ts +429 -0
- package/src/agents/NormEngine.ts +450 -0
- package/src/agents/OrchestratorAgent.ts +414 -0
- package/src/agents/SkillWorkflowEngine.ts +472 -0
- package/src/agents/TaskDelegationService.ts +551 -0
- package/src/agents/__tests__/AgentManifest.prod.test.ts +134 -0
- package/src/agents/__tests__/AgentManifest.test.ts +182 -0
- package/src/agents/__tests__/AgentModule.test.ts +864 -0
- package/src/agents/__tests__/AgentRegistry.prod.test.ts +125 -0
- package/src/agents/__tests__/AgentRegistry.test.ts +148 -0
- package/src/agents/__tests__/AgentTypes.test.ts +534 -0
- package/src/agents/__tests__/AgentWalletRegistry.test.ts +152 -0
- package/src/agents/__tests__/AuthenticatedCRDT.test.ts +558 -0
- package/src/agents/__tests__/CapabilityMatcher.prod.test.ts +117 -0
- package/src/agents/__tests__/CapabilityMatcher.test.ts +178 -0
- package/src/agents/__tests__/CrossRealityHandoff.test.ts +402 -0
- package/src/agents/__tests__/CulturalMemory.test.ts +200 -0
- package/src/agents/__tests__/FederatedRegistryAdapter.test.ts +409 -0
- package/src/agents/__tests__/NormEngine.test.ts +276 -0
- package/src/agents/__tests__/OrchestratorAgent.test.ts +182 -0
- package/src/agents/__tests__/SkillWorkflowEngine.test.ts +357 -0
- package/src/agents/__tests__/TaskDelegationService.test.ts +446 -0
- package/src/agents/index.ts +107 -0
- package/src/agents/spatial-comms/Layer1RealTime.ts +621 -0
- package/src/agents/spatial-comms/Layer2A2A.ts +661 -0
- package/src/agents/spatial-comms/Layer3MCP.ts +651 -0
- package/src/agents/spatial-comms/ProtocolTypes.ts +543 -0
- package/src/agents/spatial-comms/SpatialCommClient.ts +483 -0
- package/src/agents/spatial-comms/__tests__/performance-benchmark.test.ts +465 -0
- package/src/agents/spatial-comms/examples/multi-agent-world-creation.ts +409 -0
- package/src/agents/spatial-comms/index.ts +66 -0
- package/src/ai/AIAdapter.ts +313 -0
- package/src/ai/AICopilot.ts +331 -0
- package/src/ai/AIOutputValidator.ts +203 -0
- package/src/ai/BTNodes.ts +239 -0
- package/src/ai/BehaviorSelector.ts +135 -0
- package/src/ai/BehaviorTree.ts +153 -0
- package/src/ai/Blackboard.ts +165 -0
- package/src/ai/GenerationAnalytics.ts +461 -0
- package/src/ai/GenerationCache.ts +265 -0
- package/src/ai/GoalPlanner.ts +165 -0
- package/src/ai/HoloScriptGenerator.ts +580 -0
- package/src/ai/InfluenceMap.ts +180 -0
- package/src/ai/NavMesh.ts +168 -0
- package/src/ai/PerceptionSystem.ts +178 -0
- package/src/ai/PromptTemplates.ts +453 -0
- package/src/ai/SemanticSearchService.ts +80 -0
- package/src/ai/StateMachine.ts +196 -0
- package/src/ai/SteeringBehavior.ts +150 -0
- package/src/ai/SteeringBehaviors.ts +244 -0
- package/src/ai/TrainingDataGenerator.ts +1082 -0
- package/src/ai/UtilityAI.ts +145 -0
- package/src/ai/__tests__/AIAdapter.prod.test.ts +259 -0
- package/src/ai/__tests__/AIAdapter.test.ts +109 -0
- package/src/ai/__tests__/AICopilot.prod.test.ts +341 -0
- package/src/ai/__tests__/AICopilot.test.ts +178 -0
- package/src/ai/__tests__/AIOutputValidator.prod.test.ts +226 -0
- package/src/ai/__tests__/AIOutputValidator.test.ts +138 -0
- package/src/ai/__tests__/BTNodes.prod.test.ts +391 -0
- package/src/ai/__tests__/BTNodes.test.ts +263 -0
- package/src/ai/__tests__/BehaviorSelector.prod.test.ts +129 -0
- package/src/ai/__tests__/BehaviorSelector.test.ts +132 -0
- package/src/ai/__tests__/BehaviorTree.prod.test.ts +266 -0
- package/src/ai/__tests__/BehaviorTree.test.ts +216 -0
- package/src/ai/__tests__/Blackboard.prod.test.ts +339 -0
- package/src/ai/__tests__/Blackboard.test.ts +183 -0
- package/src/ai/__tests__/GenerationAnalytics.prod.test.ts +141 -0
- package/src/ai/__tests__/GenerationAnalytics.test.ts +165 -0
- package/src/ai/__tests__/GenerationCache.prod.test.ts +144 -0
- package/src/ai/__tests__/GenerationCache.test.ts +171 -0
- package/src/ai/__tests__/GoalPlanner.prod.test.ts +189 -0
- package/src/ai/__tests__/GoalPlanner.test.ts +137 -0
- package/src/ai/__tests__/GoalPlannerDepth.prod.test.ts +217 -0
- package/src/ai/__tests__/HoloScriptGenerator.test.ts +125 -0
- package/src/ai/__tests__/InfluenceMap.prod.test.ts +146 -0
- package/src/ai/__tests__/InfluenceMap.test.ts +149 -0
- package/src/ai/__tests__/NavMesh.prod.test.ts +141 -0
- package/src/ai/__tests__/NavMesh.test.ts +159 -0
- package/src/ai/__tests__/PerceptionSystem.prod.test.ts +135 -0
- package/src/ai/__tests__/PerceptionSystem.test.ts +250 -0
- package/src/ai/__tests__/PromptTemplates.prod.test.ts +313 -0
- package/src/ai/__tests__/PromptTemplates.test.ts +146 -0
- package/src/ai/__tests__/SemanticSearch.test.ts +37 -0
- package/src/ai/__tests__/StateMachine.prod.test.ts +162 -0
- package/src/ai/__tests__/StateMachine.test.ts +163 -0
- package/src/ai/__tests__/SteeringBehavior.prod.test.ts +251 -0
- package/src/ai/__tests__/SteeringBehavior.test.ts +135 -0
- package/src/ai/__tests__/SteeringBehaviors.prod.test.ts +133 -0
- package/src/ai/__tests__/SteeringBehaviors.test.ts +151 -0
- package/src/ai/__tests__/TrainingDataGenerator.prod.test.ts +286 -0
- package/src/ai/__tests__/TrainingDataGenerator.test.ts +286 -0
- package/src/ai/__tests__/UtilityAI.prod.test.ts +207 -0
- package/src/ai/__tests__/UtilityAI.test.ts +155 -0
- package/src/ai/__tests__/adapters.prod.test.ts +263 -0
- package/src/ai/__tests__/adapters.test.ts +320 -0
- package/src/ai/adapters.ts +1585 -0
- package/src/ai/index.ts +130 -0
- package/src/behavior/BehaviorPresets.ts +140 -0
- package/src/behavior/BehaviorTree.ts +236 -0
- package/src/behavior/StateMachine.ts +176 -0
- package/src/behavior/StateTrait.ts +67 -0
- package/src/behavior/index.ts +8 -0
- package/src/behavior.ts +8 -0
- package/src/board/audit.ts +284 -0
- package/src/board/board-ops.ts +336 -0
- package/src/board/board-types.ts +302 -0
- package/src/board/index.ts +69 -0
- package/src/define-agent.ts +46 -0
- package/src/define-team.ts +33 -0
- package/src/delegation.ts +265 -0
- package/src/distributed-claimer.ts +228 -0
- package/src/economy/AgentBudgetEnforcer.ts +464 -0
- package/src/economy/BountyManager.ts +185 -0
- package/src/economy/CreatorRevenueAggregator.ts +460 -0
- package/src/economy/InvisibleWallet.ts +82 -0
- package/src/economy/KnowledgeMarketplace.ts +193 -0
- package/src/economy/PaymentWebhookService.ts +512 -0
- package/src/economy/RevenueSplitter.ts +156 -0
- package/src/economy/SubscriptionManager.ts +546 -0
- package/src/economy/UnifiedBudgetOptimizer.ts +635 -0
- package/src/economy/UsageMeter.ts +440 -0
- package/src/economy/_core-stubs.ts +219 -0
- package/src/economy/index.ts +100 -0
- package/src/economy/x402-facilitator.ts +1978 -0
- package/src/index.ts +348 -0
- package/src/knowledge/__tests__/knowledge-consolidator.test.ts +444 -0
- package/src/knowledge/__tests__/knowledge-store-vector.test.ts +291 -0
- package/src/knowledge/brain.ts +167 -0
- package/src/knowledge/consolidation.ts +581 -0
- package/src/knowledge/knowledge-consolidator.ts +510 -0
- package/src/knowledge/knowledge-store.ts +616 -0
- package/src/learning/MemoryConsolidator.ts +102 -0
- package/src/learning/MemoryScorer.ts +69 -0
- package/src/learning/ProceduralCompiler.ts +45 -0
- package/src/learning/SemanticClusterer.ts +66 -0
- package/src/learning/index.ts +8 -0
- package/src/llm/llm-adapter.ts +159 -0
- package/src/mesh/index.ts +309 -0
- package/src/negotiation/NegotiationProtocol.ts +694 -0
- package/src/negotiation/NegotiationTypes.ts +473 -0
- package/src/negotiation/VotingMechanisms.ts +691 -0
- package/src/negotiation/index.ts +49 -0
- package/src/protocol/goal-synthesizer.ts +317 -0
- package/src/protocol/implementations.ts +474 -0
- package/src/protocol/micro-phase-decomposer.ts +299 -0
- package/src/protocol/micro-step-decomposer.test.ts +306 -0
- package/src/protocol-agent.test.ts +353 -0
- package/src/protocol-agent.ts +670 -0
- package/src/self-improve/absorb-scanner.ts +252 -0
- package/src/self-improve/evolution-engine.ts +149 -0
- package/src/self-improve/framework-absorber.ts +214 -0
- package/src/self-improve/index.ts +50 -0
- package/src/self-improve/prompt-optimizer.ts +212 -0
- package/src/self-improve/test-generator.ts +175 -0
- package/src/skill-router.ts +186 -0
- package/src/skills/index.ts +5 -0
- package/src/skills/skill-md-bridge.ts +1699 -0
- package/src/swarm/ACOEngine.ts +261 -0
- package/src/swarm/CollectiveIntelligence.ts +383 -0
- package/src/swarm/ContributionSynthesizer.ts +481 -0
- package/src/swarm/LeaderElection.ts +393 -0
- package/src/swarm/PSOEngine.ts +206 -0
- package/src/swarm/QuorumPolicy.ts +173 -0
- package/src/swarm/SwarmCoordinator.ts +335 -0
- package/src/swarm/SwarmManager.ts +442 -0
- package/src/swarm/SwarmMembership.ts +456 -0
- package/src/swarm/VotingRound.ts +255 -0
- package/src/swarm/__tests__/ACOEngine.prod.test.ts +164 -0
- package/src/swarm/__tests__/ACOEngine.test.ts +117 -0
- package/src/swarm/__tests__/CollectiveIntelligence.prod.test.ts +296 -0
- package/src/swarm/__tests__/CollectiveIntelligence.test.ts +457 -0
- package/src/swarm/__tests__/ContributionSynthesizer.prod.test.ts +269 -0
- package/src/swarm/__tests__/ContributionSynthesizer.test.ts +254 -0
- package/src/swarm/__tests__/LeaderElection.prod.test.ts +196 -0
- package/src/swarm/__tests__/LeaderElection.test.ts +151 -0
- package/src/swarm/__tests__/PSOEngine.prod.test.ts +162 -0
- package/src/swarm/__tests__/PSOEngine.test.ts +106 -0
- package/src/swarm/__tests__/QuorumPolicy.prod.test.ts +216 -0
- package/src/swarm/__tests__/QuorumPolicy.test.ts +177 -0
- package/src/swarm/__tests__/SwarmCoordinator.prod.test.ts +186 -0
- package/src/swarm/__tests__/SwarmCoordinator.test.ts +167 -0
- package/src/swarm/__tests__/SwarmManager.prod.test.ts +308 -0
- package/src/swarm/__tests__/SwarmManager.test.ts +373 -0
- package/src/swarm/__tests__/SwarmMembership.prod.test.ts +273 -0
- package/src/swarm/__tests__/SwarmMembership.test.ts +264 -0
- package/src/swarm/__tests__/VotingRound.prod.test.ts +233 -0
- package/src/swarm/__tests__/VotingRound.test.ts +174 -0
- package/src/swarm/analytics/SwarmInspector.ts +476 -0
- package/src/swarm/analytics/SwarmMetrics.ts +449 -0
- package/src/swarm/analytics/__tests__/SwarmInspector.prod.test.ts +366 -0
- package/src/swarm/analytics/__tests__/SwarmInspector.test.ts +454 -0
- package/src/swarm/analytics/__tests__/SwarmMetrics.prod.test.ts +254 -0
- package/src/swarm/analytics/__tests__/SwarmMetrics.test.ts +370 -0
- package/src/swarm/analytics/index.ts +7 -0
- package/src/swarm/index.ts +69 -0
- package/src/swarm/messaging/BroadcastChannel.ts +509 -0
- package/src/swarm/messaging/GossipProtocol.ts +565 -0
- package/src/swarm/messaging/SwarmEventBus.ts +443 -0
- package/src/swarm/messaging/__tests__/BroadcastChannel.prod.test.ts +331 -0
- package/src/swarm/messaging/__tests__/BroadcastChannel.test.ts +333 -0
- package/src/swarm/messaging/__tests__/GossipProtocol.prod.test.ts +356 -0
- package/src/swarm/messaging/__tests__/GossipProtocol.test.ts +437 -0
- package/src/swarm/messaging/__tests__/SwarmEventBus.prod.test.ts +191 -0
- package/src/swarm/messaging/__tests__/SwarmEventBus.test.ts +247 -0
- package/src/swarm/messaging/index.ts +8 -0
- package/src/swarm/spatial/FlockingBehavior.ts +462 -0
- package/src/swarm/spatial/FormationController.ts +500 -0
- package/src/swarm/spatial/Vector3.ts +170 -0
- package/src/swarm/spatial/ZoneClaiming.ts +509 -0
- package/src/swarm/spatial/__tests__/FlockingBehavior.prod.test.ts +239 -0
- package/src/swarm/spatial/__tests__/FlockingBehavior.test.ts +298 -0
- package/src/swarm/spatial/__tests__/FormationController.prod.test.ts +240 -0
- package/src/swarm/spatial/__tests__/FormationController.test.ts +297 -0
- package/src/swarm/spatial/__tests__/Vector3.prod.test.ts +283 -0
- package/src/swarm/spatial/__tests__/Vector3.test.ts +224 -0
- package/src/swarm/spatial/__tests__/ZoneClaiming.prod.test.ts +246 -0
- package/src/swarm/spatial/__tests__/ZoneClaiming.test.ts +374 -0
- package/src/swarm/spatial/index.ts +28 -0
- package/src/team.ts +1245 -0
- package/src/training/LRScheduler.ts +377 -0
- package/src/training/QualityScoringPipeline.ts +139 -0
- package/src/training/SoftDedup.ts +461 -0
- package/src/training/SparsityMonitor.ts +685 -0
- package/src/training/SparsityMonitorTypes.ts +209 -0
- package/src/training/SpatialTrainingDataGenerator.ts +1526 -0
- package/src/training/SpatialTrainingDataTypes.ts +216 -0
- package/src/training/TrainingPipelineConfig.ts +215 -0
- package/src/training/constants.ts +94 -0
- package/src/training/index.ts +138 -0
- package/src/training/schema.ts +147 -0
- package/src/training/scripts/generate-novel-use-cases-dataset.ts +272 -0
- package/src/training/scripts/generate-spatial-dataset.ts +521 -0
- package/src/training/training/data/novel-use-cases.jsonl +153 -0
- package/src/training/training/data/spatial-reasoning-10k.jsonl +9354 -0
- package/src/training/trainingmonkey/TrainingMonkeyIntegration.ts +477 -0
- package/src/training/trainingmonkey/TrainingMonkeyTypes.ts +230 -0
- package/src/training/trainingmonkey/index.ts +26 -0
- package/src/training/trait-mappings.ts +157 -0
- package/src/types/core-stubs.d.ts +113 -0
- package/src/types.ts +304 -0
- package/test-output.txt +0 -0
- package/test-result.json +1 -0
- package/tsc-errors.txt +4 -0
- package/tsc_output.txt +0 -0
- package/tsconfig.json +14 -0
- package/tsup-learning-esm.config.ts +12 -0
- package/tsup.config.ts +21 -0
- package/typescript-errors-2.txt +0 -0
- package/typescript-errors.txt +22 -0
- package/vitest-log-utf8.txt +268 -0
- package/vitest-log.txt +0 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GossipProtocol Tests
|
|
3
|
+
* HoloScript v3.2 - Autonomous Agent Swarms
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
|
|
7
|
+
import { GossipProtocol, AntiEntropySync } from '../GossipProtocol';
|
|
8
|
+
|
|
9
|
+
describe('GossipProtocol', () => {
|
|
10
|
+
let protocol: GossipProtocol;
|
|
11
|
+
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
protocol = new GossipProtocol('node-1');
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
protocol.stop();
|
|
18
|
+
vi.restoreAllMocks();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('lifecycle', () => {
|
|
22
|
+
it('should start the protocol', () => {
|
|
23
|
+
protocol.start();
|
|
24
|
+
expect(protocol.isRunning()).toBe(true);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should stop the protocol', () => {
|
|
28
|
+
protocol.start();
|
|
29
|
+
protocol.stop();
|
|
30
|
+
expect(protocol.isRunning()).toBe(false);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should not start twice', () => {
|
|
34
|
+
protocol.start();
|
|
35
|
+
protocol.start(); // Should not throw
|
|
36
|
+
expect(protocol.isRunning()).toBe(true);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe('peer management', () => {
|
|
41
|
+
it('should add a peer', () => {
|
|
42
|
+
protocol.addPeer('peer-1', 'localhost:3001');
|
|
43
|
+
|
|
44
|
+
const peer = protocol.getPeer('peer-1');
|
|
45
|
+
expect(peer).toBeDefined();
|
|
46
|
+
expect(peer?.address).toBe('localhost:3001');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('should not add self as peer', () => {
|
|
50
|
+
protocol.addPeer('node-1', 'localhost:3001');
|
|
51
|
+
|
|
52
|
+
expect(protocol.getAllPeers().length).toBe(0);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should remove a peer', () => {
|
|
56
|
+
protocol.addPeer('peer-1', 'localhost:3001');
|
|
57
|
+
|
|
58
|
+
const removed = protocol.removePeer('peer-1');
|
|
59
|
+
|
|
60
|
+
expect(removed).toBe(true);
|
|
61
|
+
expect(protocol.getPeer('peer-1')).toBeUndefined();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('should get active peers', () => {
|
|
65
|
+
protocol.addPeer('peer-1', 'addr1');
|
|
66
|
+
protocol.addPeer('peer-2', 'addr2');
|
|
67
|
+
|
|
68
|
+
const peer2 = protocol.getPeer('peer-2')!;
|
|
69
|
+
peer2.isActive = false;
|
|
70
|
+
|
|
71
|
+
const active = protocol.getActivePeers();
|
|
72
|
+
expect(active.length).toBe(1);
|
|
73
|
+
expect(active[0].id).toBe('peer-1');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('should store peer metadata', () => {
|
|
77
|
+
protocol.addPeer('peer-1', 'addr1', { region: 'us-west' });
|
|
78
|
+
|
|
79
|
+
const peer = protocol.getPeer('peer-1');
|
|
80
|
+
expect(peer?.metadata?.region).toBe('us-west');
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe('publishing', () => {
|
|
85
|
+
it('should publish a message', () => {
|
|
86
|
+
const msgId = protocol.publish({ data: 'test' });
|
|
87
|
+
|
|
88
|
+
expect(msgId).toContain('gossip-');
|
|
89
|
+
expect(msgId).toContain('node-1');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('should emit locally on publish', () => {
|
|
93
|
+
const handler = vi.fn();
|
|
94
|
+
protocol.subscribe('data', handler);
|
|
95
|
+
|
|
96
|
+
protocol.publish({ data: 'test' });
|
|
97
|
+
|
|
98
|
+
expect(handler).toHaveBeenCalledOnce();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('should publish with custom type', () => {
|
|
102
|
+
const handler = vi.fn();
|
|
103
|
+
protocol.subscribe('custom', handler);
|
|
104
|
+
|
|
105
|
+
protocol.publish({ x: 1 }, 'custom');
|
|
106
|
+
|
|
107
|
+
expect(handler).toHaveBeenCalled();
|
|
108
|
+
expect(handler.mock.calls[0][0].type).toBe('custom');
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('should track sent messages', () => {
|
|
112
|
+
protocol.publish({ data: 1 });
|
|
113
|
+
protocol.publish({ data: 2 });
|
|
114
|
+
|
|
115
|
+
const stats = protocol.getStats();
|
|
116
|
+
expect(stats.messagesSent).toBe(2);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe('receiving', () => {
|
|
121
|
+
it('should receive a message', async () => {
|
|
122
|
+
const handler = vi.fn();
|
|
123
|
+
protocol.subscribe('data', handler);
|
|
124
|
+
protocol.addPeer('peer-1', 'addr');
|
|
125
|
+
|
|
126
|
+
const received = await protocol.receive(
|
|
127
|
+
{
|
|
128
|
+
id: 'msg-1',
|
|
129
|
+
originId: 'peer-1',
|
|
130
|
+
content: { data: 'incoming' },
|
|
131
|
+
type: 'data',
|
|
132
|
+
version: 1,
|
|
133
|
+
createdAt: Date.now(),
|
|
134
|
+
ttl: 30000,
|
|
135
|
+
hops: 0,
|
|
136
|
+
path: ['peer-1'],
|
|
137
|
+
},
|
|
138
|
+
'peer-1'
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
expect(received).toBe(true);
|
|
142
|
+
expect(handler).toHaveBeenCalledOnce();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('should ignore duplicate messages', async () => {
|
|
146
|
+
const handler = vi.fn();
|
|
147
|
+
protocol.subscribe('data', handler);
|
|
148
|
+
protocol.addPeer('peer-1', 'addr');
|
|
149
|
+
|
|
150
|
+
const msg = {
|
|
151
|
+
id: 'msg-dup',
|
|
152
|
+
originId: 'peer-1',
|
|
153
|
+
content: { data: 'x' },
|
|
154
|
+
type: 'data' as const,
|
|
155
|
+
version: 1,
|
|
156
|
+
createdAt: Date.now(),
|
|
157
|
+
ttl: 30000,
|
|
158
|
+
hops: 0,
|
|
159
|
+
path: ['peer-1'],
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
await protocol.receive(msg, 'peer-1');
|
|
163
|
+
const secondReceive = await protocol.receive(msg, 'peer-1');
|
|
164
|
+
|
|
165
|
+
expect(secondReceive).toBe(false);
|
|
166
|
+
expect(handler).toHaveBeenCalledOnce();
|
|
167
|
+
|
|
168
|
+
const stats = protocol.getStats();
|
|
169
|
+
expect(stats.duplicatesIgnored).toBe(1);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('should drop expired messages', async () => {
|
|
173
|
+
const handler = vi.fn();
|
|
174
|
+
protocol.subscribe('data', handler);
|
|
175
|
+
|
|
176
|
+
const received = await protocol.receive(
|
|
177
|
+
{
|
|
178
|
+
id: 'msg-exp',
|
|
179
|
+
originId: 'peer-1',
|
|
180
|
+
content: {},
|
|
181
|
+
type: 'data',
|
|
182
|
+
version: 1,
|
|
183
|
+
createdAt: Date.now() - 60000,
|
|
184
|
+
ttl: 30000, // Already expired
|
|
185
|
+
hops: 0,
|
|
186
|
+
path: ['peer-1'],
|
|
187
|
+
},
|
|
188
|
+
'peer-1'
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
expect(received).toBe(false);
|
|
192
|
+
expect(handler).not.toHaveBeenCalled();
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('should drop messages over hop limit', async () => {
|
|
196
|
+
const protocol10 = new GossipProtocol('node', { maxHops: 10 });
|
|
197
|
+
const handler = vi.fn();
|
|
198
|
+
protocol10.subscribe('data', handler);
|
|
199
|
+
|
|
200
|
+
const received = await protocol10.receive(
|
|
201
|
+
{
|
|
202
|
+
id: 'msg-hops',
|
|
203
|
+
originId: 'origin',
|
|
204
|
+
content: {},
|
|
205
|
+
type: 'data',
|
|
206
|
+
version: 1,
|
|
207
|
+
createdAt: Date.now(),
|
|
208
|
+
ttl: 30000,
|
|
209
|
+
hops: 15, // Over limit
|
|
210
|
+
path: Array(15).fill('node'),
|
|
211
|
+
},
|
|
212
|
+
'peer-1'
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
expect(received).toBe(false);
|
|
216
|
+
expect(handler).not.toHaveBeenCalled();
|
|
217
|
+
protocol10.stop();
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('should update peer last seen', async () => {
|
|
221
|
+
protocol.addPeer('peer-1', 'addr');
|
|
222
|
+
const before = protocol.getPeer('peer-1')!.lastSeen;
|
|
223
|
+
|
|
224
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
225
|
+
|
|
226
|
+
await protocol.receive(
|
|
227
|
+
{
|
|
228
|
+
id: 'msg-update',
|
|
229
|
+
originId: 'peer-1',
|
|
230
|
+
content: {},
|
|
231
|
+
type: 'data',
|
|
232
|
+
version: 1,
|
|
233
|
+
createdAt: Date.now(),
|
|
234
|
+
ttl: 30000,
|
|
235
|
+
hops: 0,
|
|
236
|
+
path: ['peer-1'],
|
|
237
|
+
},
|
|
238
|
+
'peer-1'
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
const after = protocol.getPeer('peer-1')!.lastSeen;
|
|
242
|
+
expect(after).toBeGreaterThan(before);
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
describe('subscription', () => {
|
|
247
|
+
it('should subscribe to specific type', () => {
|
|
248
|
+
const handler = vi.fn();
|
|
249
|
+
protocol.subscribe('heartbeat', handler);
|
|
250
|
+
|
|
251
|
+
protocol.publish({}, 'data');
|
|
252
|
+
protocol.publish({}, 'heartbeat');
|
|
253
|
+
|
|
254
|
+
expect(handler).toHaveBeenCalledOnce();
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it('should support wildcard subscription', () => {
|
|
258
|
+
const handler = vi.fn();
|
|
259
|
+
protocol.subscribe('*', handler);
|
|
260
|
+
|
|
261
|
+
protocol.publish({}, 'data');
|
|
262
|
+
protocol.publish({}, 'heartbeat');
|
|
263
|
+
|
|
264
|
+
expect(handler).toHaveBeenCalledTimes(2);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it('should unsubscribe', () => {
|
|
268
|
+
const handler = vi.fn();
|
|
269
|
+
const unsub = protocol.subscribe('data', handler);
|
|
270
|
+
|
|
271
|
+
unsub();
|
|
272
|
+
protocol.publish({}, 'data');
|
|
273
|
+
|
|
274
|
+
expect(handler).not.toHaveBeenCalled();
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
describe('heartbeat and membership', () => {
|
|
279
|
+
it('should publish heartbeat', () => {
|
|
280
|
+
const handler = vi.fn();
|
|
281
|
+
protocol.subscribe('heartbeat', handler);
|
|
282
|
+
|
|
283
|
+
const msgId = protocol.publishHeartbeat({ status: 'ok' });
|
|
284
|
+
|
|
285
|
+
expect(msgId).toBeDefined();
|
|
286
|
+
expect(handler).toHaveBeenCalledOnce();
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it('should publish membership change', () => {
|
|
290
|
+
const handler = vi.fn();
|
|
291
|
+
protocol.subscribe('membership', handler);
|
|
292
|
+
|
|
293
|
+
protocol.publishMembership('join', { group: 'swarm-1' });
|
|
294
|
+
|
|
295
|
+
expect(handler).toHaveBeenCalledOnce();
|
|
296
|
+
const msg = handler.mock.calls[0][0];
|
|
297
|
+
expect(msg.content.action).toBe('join');
|
|
298
|
+
});
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
describe('gossip round', () => {
|
|
302
|
+
it('should perform gossip round', async () => {
|
|
303
|
+
protocol.start(); // Must be running
|
|
304
|
+
protocol.addPeer('peer-1', 'addr1');
|
|
305
|
+
protocol.addPeer('peer-2', 'addr2');
|
|
306
|
+
|
|
307
|
+
protocol.publish({ data: 'test' });
|
|
308
|
+
|
|
309
|
+
await protocol.gossipRound();
|
|
310
|
+
|
|
311
|
+
const stats = protocol.getStats();
|
|
312
|
+
expect(stats.gossipRounds).toBe(1);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it('should not gossip when stopped', async () => {
|
|
316
|
+
protocol.publish({ data: 'test' });
|
|
317
|
+
|
|
318
|
+
await protocol.gossipRound();
|
|
319
|
+
|
|
320
|
+
// Gossip round should be skipped
|
|
321
|
+
expect(protocol.isRunning()).toBe(false);
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
describe('peer selector', () => {
|
|
326
|
+
it('should use custom peer selector', () => {
|
|
327
|
+
const customSelector = vi.fn().mockImplementation((peers) => [peers[0]]);
|
|
328
|
+
protocol.setPeerSelector(customSelector);
|
|
329
|
+
|
|
330
|
+
protocol.addPeer('peer-1', 'addr1');
|
|
331
|
+
protocol.addPeer('peer-2', 'addr2');
|
|
332
|
+
protocol.start();
|
|
333
|
+
protocol.publish({ data: 'x' });
|
|
334
|
+
|
|
335
|
+
// Will be called on gossip round
|
|
336
|
+
protocol.gossipRound();
|
|
337
|
+
|
|
338
|
+
expect(customSelector).toHaveBeenCalled();
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
describe('statistics', () => {
|
|
343
|
+
it('should return stats', () => {
|
|
344
|
+
protocol.addPeer('peer-1', 'addr');
|
|
345
|
+
|
|
346
|
+
const stats = protocol.getStats();
|
|
347
|
+
|
|
348
|
+
expect(stats.peerCount).toBe(1);
|
|
349
|
+
expect(stats.messagesSent).toBe(0);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
it('should reset stats', () => {
|
|
353
|
+
protocol.publish({ data: 'x' });
|
|
354
|
+
protocol.resetStats();
|
|
355
|
+
|
|
356
|
+
const stats = protocol.getStats();
|
|
357
|
+
expect(stats.messagesSent).toBe(0);
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
describe('configuration', () => {
|
|
362
|
+
it('should return config', () => {
|
|
363
|
+
const config = protocol.getConfig();
|
|
364
|
+
|
|
365
|
+
expect(config.fanout).toBeDefined();
|
|
366
|
+
expect(config.maxTTL).toBeDefined();
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
describe('AntiEntropySync', () => {
|
|
372
|
+
let protocol: GossipProtocol;
|
|
373
|
+
let sync: AntiEntropySync;
|
|
374
|
+
|
|
375
|
+
beforeEach(() => {
|
|
376
|
+
protocol = new GossipProtocol('node-sync');
|
|
377
|
+
sync = new AntiEntropySync('node-sync', protocol);
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
afterEach(() => {
|
|
381
|
+
protocol.stop();
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
describe('set and get', () => {
|
|
385
|
+
it('should set and get a value', () => {
|
|
386
|
+
sync.set('key1', { value: 42 });
|
|
387
|
+
|
|
388
|
+
const result = sync.get('key1');
|
|
389
|
+
|
|
390
|
+
expect(result).toEqual({ value: 42 });
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it('should return undefined for unknown keys', () => {
|
|
394
|
+
expect(sync.get('unknown')).toBeUndefined();
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
it('should list all keys', () => {
|
|
398
|
+
sync.set('a', 1);
|
|
399
|
+
sync.set('b', 2);
|
|
400
|
+
|
|
401
|
+
const keys = sync.keys();
|
|
402
|
+
|
|
403
|
+
expect(keys).toContain('a');
|
|
404
|
+
expect(keys).toContain('b');
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
describe('gossip sync', () => {
|
|
409
|
+
it('should publish on set', async () => {
|
|
410
|
+
const handler = vi.fn();
|
|
411
|
+
protocol.subscribe('data', handler);
|
|
412
|
+
|
|
413
|
+
sync.set('key1', 'value');
|
|
414
|
+
|
|
415
|
+
// Wait for async handlers to complete
|
|
416
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
417
|
+
|
|
418
|
+
// GossipProtocol.publish calls emitToHandlers which calls handler(message, from)
|
|
419
|
+
expect(handler).toHaveBeenCalledOnce();
|
|
420
|
+
const msg = handler.mock.calls[0][0];
|
|
421
|
+
expect(msg.content.key).toBe('key1');
|
|
422
|
+
expect(msg.content.value).toBe('value');
|
|
423
|
+
});
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
describe('snapshot', () => {
|
|
427
|
+
it('should get snapshot of all data', () => {
|
|
428
|
+
sync.set('x', 1);
|
|
429
|
+
sync.set('y', 2);
|
|
430
|
+
|
|
431
|
+
const snapshot = sync.getSnapshot();
|
|
432
|
+
|
|
433
|
+
expect(snapshot.get('x')).toBe(1);
|
|
434
|
+
expect(snapshot.get('y')).toBe(2);
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
});
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SwarmEventBus — Production Tests
|
|
3
|
+
*/
|
|
4
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
5
|
+
import { SwarmEventBus } from '../SwarmEventBus';
|
|
6
|
+
|
|
7
|
+
function make(cfg = {}) {
|
|
8
|
+
return new SwarmEventBus(cfg);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
describe('SwarmEventBus — construction', () => {
|
|
12
|
+
it('constructs without args', () => expect(() => make()).not.toThrow());
|
|
13
|
+
it('default maxQueueSize=10000', () => expect(make().getConfig().maxQueueSize).toBe(10000));
|
|
14
|
+
it('default asyncProcessing=true', () => expect(make().getConfig().asyncProcessing).toBe(true));
|
|
15
|
+
it('initial stats all zero', () => {
|
|
16
|
+
const s = make().getStats();
|
|
17
|
+
expect(s.eventsPublished).toBe(0);
|
|
18
|
+
expect(s.eventsDelivered).toBe(0);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('SwarmEventBus — publish', () => {
|
|
23
|
+
it('returns event id string', () => {
|
|
24
|
+
const bus = make();
|
|
25
|
+
const id = bus.publish('test', 'src', {});
|
|
26
|
+
expect(typeof id).toBe('string');
|
|
27
|
+
expect(id.startsWith('evt-')).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
it('increments eventsPublished', () => {
|
|
30
|
+
const bus = make();
|
|
31
|
+
bus.publish('test', 'src', {});
|
|
32
|
+
expect(bus.getStats().eventsPublished).toBe(1);
|
|
33
|
+
});
|
|
34
|
+
it('drops to dead letter when queue full', () => {
|
|
35
|
+
const bus = make({ maxQueueSize: 1, asyncProcessing: false });
|
|
36
|
+
bus.publish('a', 'src', {}); // fills queue
|
|
37
|
+
bus.publish('b', 'src', {}); // dropped to dead letter
|
|
38
|
+
expect(bus.getDeadLetterQueue()).toHaveLength(1);
|
|
39
|
+
expect(bus.getStats().eventsDropped).toBe(1);
|
|
40
|
+
});
|
|
41
|
+
it('priority ordering: critical before normal', () => {
|
|
42
|
+
const bus = make({ asyncProcessing: false });
|
|
43
|
+
bus.publish('normal', 'src', {}, { priority: 'normal' });
|
|
44
|
+
bus.publish('critical', 'src', {}, { priority: 'critical' });
|
|
45
|
+
const pending = bus.getPendingEvents();
|
|
46
|
+
expect(pending[0].priority).toBe('critical');
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('SwarmEventBus — subscribe / unsubscribe', () => {
|
|
51
|
+
it('subscribe returns subscription id', () => {
|
|
52
|
+
const bus = make();
|
|
53
|
+
const id = bus.subscribe('test', () => {});
|
|
54
|
+
expect(id.startsWith('sub-')).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
it('unsubscribe removes subscription', () => {
|
|
57
|
+
const bus = make();
|
|
58
|
+
const id = bus.subscribe('test', () => {});
|
|
59
|
+
expect(bus.unsubscribe(id)).toBe(true);
|
|
60
|
+
expect(bus.getStats().subscriptions).toBe(0);
|
|
61
|
+
});
|
|
62
|
+
it('unsubscribe unknown returns false', () => {
|
|
63
|
+
expect(make().unsubscribe('ghost')).toBe(false);
|
|
64
|
+
});
|
|
65
|
+
it('clearSubscriptions removes all', () => {
|
|
66
|
+
const bus = make();
|
|
67
|
+
bus.subscribe('a', () => {});
|
|
68
|
+
bus.subscribe('b', () => {});
|
|
69
|
+
bus.clearSubscriptions();
|
|
70
|
+
expect(bus.getStats().subscriptions).toBe(0);
|
|
71
|
+
});
|
|
72
|
+
it('once subscription auto-removed after delivery', async () => {
|
|
73
|
+
const bus = make({ asyncProcessing: false });
|
|
74
|
+
bus.once('test', () => {});
|
|
75
|
+
bus.publish('test', 'src', {});
|
|
76
|
+
await bus.processQueue();
|
|
77
|
+
expect(bus.getStats().subscriptions).toBe(0);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe('SwarmEventBus — emit (synchronous)', () => {
|
|
82
|
+
it('delivers synchronously to matching subscriber', () => {
|
|
83
|
+
let received: unknown[] = [];
|
|
84
|
+
const bus = make();
|
|
85
|
+
bus.subscribe('evt.ping', (e) => { received.push(e.payload); });
|
|
86
|
+
bus.emit('evt.ping', 'src', { val: 42 });
|
|
87
|
+
expect(received).toHaveLength(1);
|
|
88
|
+
expect((received[0] as any).val).toBe(42);
|
|
89
|
+
});
|
|
90
|
+
it('does not deliver to non-matching subscriber', () => {
|
|
91
|
+
let received: unknown[] = [];
|
|
92
|
+
const bus = make();
|
|
93
|
+
bus.subscribe('other', (e) => { received.push(e.payload); });
|
|
94
|
+
bus.emit('evt.ping', 'src', {});
|
|
95
|
+
expect(received).toHaveLength(0);
|
|
96
|
+
});
|
|
97
|
+
it('wildcard pattern matches', () => {
|
|
98
|
+
let received: unknown[] = [];
|
|
99
|
+
const bus = make();
|
|
100
|
+
bus.subscribe('swarm.*', (e) => { received.push(e.payload); });
|
|
101
|
+
bus.emit('swarm.joined', 'src', {});
|
|
102
|
+
bus.emit('swarm.left', 'src', {});
|
|
103
|
+
expect(received).toHaveLength(2);
|
|
104
|
+
});
|
|
105
|
+
it('regex pattern matches', () => {
|
|
106
|
+
let received: unknown[] = [];
|
|
107
|
+
const bus = make();
|
|
108
|
+
bus.subscribe(/^agent\./, (e) => { received.push(e.payload); });
|
|
109
|
+
bus.emit('agent.spawn', 'src', {});
|
|
110
|
+
bus.emit('agent.destroy', 'src', {});
|
|
111
|
+
bus.emit('other.event', 'src', {});
|
|
112
|
+
expect(received).toHaveLength(2);
|
|
113
|
+
});
|
|
114
|
+
it('exact match only', () => {
|
|
115
|
+
let received: unknown[] = [];
|
|
116
|
+
const bus = make();
|
|
117
|
+
bus.subscribe('exact', (e) => { received.push(e.payload); });
|
|
118
|
+
bus.emit('exact.extended', 'src', {});
|
|
119
|
+
expect(received).toHaveLength(0);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe('SwarmEventBus — processQueue', () => {
|
|
124
|
+
it('delivers events to subscribers', async () => {
|
|
125
|
+
let received: unknown[] = [];
|
|
126
|
+
const bus = make({ asyncProcessing: false });
|
|
127
|
+
bus.subscribe('greet', (e) => { received.push(e.payload); });
|
|
128
|
+
bus.publish('greet', 'src', 'hello');
|
|
129
|
+
await bus.processQueue();
|
|
130
|
+
expect(received).toHaveLength(1);
|
|
131
|
+
});
|
|
132
|
+
it('skips expired TTL events', async () => {
|
|
133
|
+
let received: unknown[] = [];
|
|
134
|
+
const bus = make({ asyncProcessing: false, defaultTTL: 1 });
|
|
135
|
+
bus.subscribe('late', (e) => { received.push(e.payload); });
|
|
136
|
+
bus.publish('late', 'src', {}, { ttl: 1 });
|
|
137
|
+
await new Promise((r) => setTimeout(r, 5)); // let TTL expire
|
|
138
|
+
await bus.processQueue();
|
|
139
|
+
expect(received).toHaveLength(0);
|
|
140
|
+
expect(bus.getStats().eventsDropped).toBeGreaterThan(0);
|
|
141
|
+
});
|
|
142
|
+
it('increments eventsDelivered', async () => {
|
|
143
|
+
const bus = make({ asyncProcessing: false });
|
|
144
|
+
bus.subscribe('x', () => {});
|
|
145
|
+
bus.publish('x', 'src', {});
|
|
146
|
+
await bus.processQueue();
|
|
147
|
+
expect(bus.getStats().eventsDelivered).toBe(1);
|
|
148
|
+
});
|
|
149
|
+
it('unmatched event goes to dead letter', async () => {
|
|
150
|
+
const bus = make({ asyncProcessing: false, enableDeadLetter: true });
|
|
151
|
+
bus.publish('unmatched', 'src', {});
|
|
152
|
+
await bus.processQueue();
|
|
153
|
+
expect(bus.getDeadLetterQueue()).toHaveLength(1);
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe('SwarmEventBus — dead letter / replay', () => {
|
|
158
|
+
it('clearDeadLetterQueue returns and empties', async () => {
|
|
159
|
+
const bus = make({ asyncProcessing: false });
|
|
160
|
+
bus.publish('x', 'src', {});
|
|
161
|
+
await bus.processQueue(); // unmatched → DLQ
|
|
162
|
+
const dead = bus.clearDeadLetterQueue();
|
|
163
|
+
expect(dead).toHaveLength(1);
|
|
164
|
+
expect(bus.getDeadLetterQueue()).toHaveLength(0);
|
|
165
|
+
});
|
|
166
|
+
it('replayDeadLetters re-enqueues and delivers', async () => {
|
|
167
|
+
let received: unknown[] = [];
|
|
168
|
+
const bus = make({ asyncProcessing: false });
|
|
169
|
+
bus.publish('retry', 'src', 'payload');
|
|
170
|
+
await bus.processQueue(); // to DLQ
|
|
171
|
+
bus.subscribe('retry', (e) => { received.push(e.payload); });
|
|
172
|
+
bus.replayDeadLetters();
|
|
173
|
+
await bus.processQueue();
|
|
174
|
+
expect(received).toContain('payload');
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
describe('SwarmEventBus — stats / resetStats / setConfig', () => {
|
|
179
|
+
it('resetStats zeroes published/delivered/dropped', () => {
|
|
180
|
+
const bus = make({ asyncProcessing: false });
|
|
181
|
+
bus.publish('x', 'src', {});
|
|
182
|
+
bus.resetStats();
|
|
183
|
+
expect(bus.getStats().eventsPublished).toBe(0);
|
|
184
|
+
expect(bus.getStats().eventsDelivered).toBe(0);
|
|
185
|
+
});
|
|
186
|
+
it('setConfig partial update', () => {
|
|
187
|
+
const bus = make({ maxQueueSize: 100 });
|
|
188
|
+
bus.setConfig({ maxQueueSize: 500 });
|
|
189
|
+
expect(bus.getConfig().maxQueueSize).toBe(500);
|
|
190
|
+
});
|
|
191
|
+
});
|