|
1 | 1 | import { |
| 2 | + Agent, |
2 | 3 | convertSubgraphBasedRulesToDeploymentBased, |
3 | 4 | consolidateAllocationDecisions, |
4 | 5 | resolveTargetDeployments, |
5 | 6 | } from '../agent' |
6 | 7 | import { |
| 8 | + ActivationCriteria, |
| 9 | + Allocation, |
| 10 | + AllocationDecision, |
| 11 | + AllocationStatus, |
7 | 12 | INDEXING_RULE_GLOBAL, |
8 | 13 | IndexingDecisionBasis, |
9 | 14 | IndexingRuleAttributes, |
@@ -328,3 +333,142 @@ describe('resolveTargetDeployments function', () => { |
328 | 333 | ) |
329 | 334 | }) |
330 | 335 | }) |
| 336 | + |
| 337 | +describe('reconcileDeploymentAllocationAction', () => { |
| 338 | + const deployment = new SubgraphDeploymentID( |
| 339 | + 'QmXZiV6S13ha6QXq4dmaM3TB4CHcDxBMvGexSNu9Kc28EH', |
| 340 | + ) |
| 341 | + |
| 342 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 343 | + const mockLogger: any = { |
| 344 | + child: jest.fn().mockReturnThis(), |
| 345 | + info: jest.fn(), |
| 346 | + warn: jest.fn(), |
| 347 | + error: jest.fn(), |
| 348 | + debug: jest.fn(), |
| 349 | + trace: jest.fn(), |
| 350 | + } |
| 351 | + |
| 352 | + const activeAllocations: Allocation[] = [ |
| 353 | + { |
| 354 | + id: '0x0000000000000000000000000000000000000001', |
| 355 | + status: AllocationStatus.ACTIVE, |
| 356 | + isLegacy: false, |
| 357 | + subgraphDeployment: { |
| 358 | + id: deployment, |
| 359 | + ipfsHash: deployment.ipfsHash, |
| 360 | + }, |
| 361 | + indexer: '0x0000000000000000000000000000000000000000', |
| 362 | + allocatedTokens: BigInt(1000), |
| 363 | + createdAt: 0, |
| 364 | + createdAtEpoch: 1, |
| 365 | + createdAtBlockHash: '0x0', |
| 366 | + closedAt: 0, |
| 367 | + closedAtEpoch: 0, |
| 368 | + closedAtEpochStartBlockHash: undefined, |
| 369 | + previousEpochStartBlockHash: undefined, |
| 370 | + closedAtBlockHash: '0x0', |
| 371 | + poi: undefined, |
| 372 | + queryFeeRebates: undefined, |
| 373 | + queryFeesCollected: undefined, |
| 374 | + } as unknown as Allocation, |
| 375 | + ] |
| 376 | + |
| 377 | + const decision = new AllocationDecision( |
| 378 | + deployment, |
| 379 | + { |
| 380 | + identifier: deployment.ipfsHash, |
| 381 | + identifierType: SubgraphIdentifierType.DEPLOYMENT, |
| 382 | + allocationAmount: '1000', |
| 383 | + decisionBasis: IndexingDecisionBasis.RULES, |
| 384 | + } as IndexingRuleAttributes, |
| 385 | + true, |
| 386 | + ActivationCriteria.SIGNAL_THRESHOLD, |
| 387 | + 'eip155:42161', |
| 388 | + ) |
| 389 | + |
| 390 | + function createAgent() { |
| 391 | + const agent = Object.create(Agent.prototype) |
| 392 | + agent.logger = mockLogger |
| 393 | + agent.graphNode = { |
| 394 | + indexingStatus: jest.fn().mockResolvedValue([ |
| 395 | + { |
| 396 | + subgraphDeployment: { ipfsHash: deployment.ipfsHash }, |
| 397 | + health: 'healthy', |
| 398 | + }, |
| 399 | + ]), |
| 400 | + } |
| 401 | + agent.identifyExpiringAllocations = jest |
| 402 | + .fn() |
| 403 | + .mockResolvedValue([activeAllocations[0]]) |
| 404 | + return agent |
| 405 | + } |
| 406 | + |
| 407 | + function createOperator() { |
| 408 | + return { |
| 409 | + closeEligibleAllocations: jest.fn(), |
| 410 | + createAllocation: jest.fn(), |
| 411 | + refreshExpiredAllocations: jest.fn(), |
| 412 | + presentPOIForAllocations: jest.fn(), |
| 413 | + } |
| 414 | + } |
| 415 | + |
| 416 | + function createNetwork(isHorizon: boolean) { |
| 417 | + return { |
| 418 | + isHorizon: { value: jest.fn().mockResolvedValue(isHorizon) }, |
| 419 | + specification: { networkIdentifier: 'eip155:42161' }, |
| 420 | + networkMonitor: { |
| 421 | + closedAllocations: jest.fn().mockResolvedValue([]), |
| 422 | + }, |
| 423 | + } |
| 424 | + } |
| 425 | + |
| 426 | + it('should call presentPOIForAllocations instead of refreshExpiredAllocations for Horizon allocations', async () => { |
| 427 | + const agent = createAgent() |
| 428 | + const operator = createOperator() |
| 429 | + const network = createNetwork(true) |
| 430 | + |
| 431 | + await agent.reconcileDeploymentAllocationAction( |
| 432 | + decision, |
| 433 | + activeAllocations, |
| 434 | + 10, |
| 435 | + { value: jest.fn().mockResolvedValue(28) }, |
| 436 | + network, |
| 437 | + operator, |
| 438 | + false, |
| 439 | + ) |
| 440 | + |
| 441 | + expect(agent.identifyExpiringAllocations).toHaveBeenCalled() |
| 442 | + expect(operator.refreshExpiredAllocations).not.toHaveBeenCalled() |
| 443 | + expect(operator.presentPOIForAllocations).toHaveBeenCalledWith( |
| 444 | + expect.anything(), |
| 445 | + [activeAllocations[0]], |
| 446 | + network, |
| 447 | + ) |
| 448 | + }) |
| 449 | + |
| 450 | + it('should call refreshExpiredAllocations for legacy allocations', async () => { |
| 451 | + const agent = createAgent() |
| 452 | + const operator = createOperator() |
| 453 | + const network = createNetwork(false) |
| 454 | + |
| 455 | + await agent.reconcileDeploymentAllocationAction( |
| 456 | + decision, |
| 457 | + activeAllocations, |
| 458 | + 10, |
| 459 | + { value: jest.fn().mockResolvedValue(28) }, |
| 460 | + network, |
| 461 | + operator, |
| 462 | + false, |
| 463 | + ) |
| 464 | + |
| 465 | + expect(agent.identifyExpiringAllocations).toHaveBeenCalled() |
| 466 | + expect(operator.refreshExpiredAllocations).toHaveBeenCalledWith( |
| 467 | + expect.anything(), |
| 468 | + decision, |
| 469 | + [activeAllocations[0]], |
| 470 | + false, |
| 471 | + ) |
| 472 | + expect(operator.presentPOIForAllocations).not.toHaveBeenCalled() |
| 473 | + }) |
| 474 | +}) |
0 commit comments