|
| 1 | +import { describe, it } from 'mocha'; |
| 2 | +import { expect } from 'chai'; |
| 3 | + |
| 4 | +import PublishController from '../../../src/controllers/http-api/v1/publish-http-api-controller-v1.js'; |
| 5 | +import { PUBLISH_MIN_NUM_OF_NODE_REPLICATIONS } from '../../../src/constants/constants.js'; |
| 6 | + |
| 7 | +const createRes = () => { |
| 8 | + const res = { |
| 9 | + statusCode: null, |
| 10 | + body: null, |
| 11 | + status(code) { |
| 12 | + this.statusCode = code; |
| 13 | + return this; |
| 14 | + }, |
| 15 | + json(payload) { |
| 16 | + this.body = payload; |
| 17 | + return this; |
| 18 | + }, |
| 19 | + send(payload) { |
| 20 | + this.body = payload; |
| 21 | + return this; |
| 22 | + }, |
| 23 | + }; |
| 24 | + return res; |
| 25 | +}; |
| 26 | + |
| 27 | +describe('publish-http-api-controller-v1', () => { |
| 28 | + const baseCtx = () => { |
| 29 | + const addedCommands = []; |
| 30 | + return { |
| 31 | + commandExecutor: { |
| 32 | + add: async (cmd) => { |
| 33 | + addedCommands.push(cmd); |
| 34 | + }, |
| 35 | + _added: addedCommands, |
| 36 | + }, |
| 37 | + publishService: { |
| 38 | + getOperationName: () => 'publish', |
| 39 | + }, |
| 40 | + operationIdService: { |
| 41 | + generateOperationId: async () => 'op-id-123', |
| 42 | + emitChangeEvent: () => {}, |
| 43 | + updateOperationIdStatus: async () => {}, |
| 44 | + cacheOperationIdDataToMemory: async () => {}, |
| 45 | + cacheOperationIdDataToFile: async () => {}, |
| 46 | + }, |
| 47 | + repositoryModuleManager: { |
| 48 | + createOperationRecord: async () => {}, |
| 49 | + }, |
| 50 | + pendingStorageService: { |
| 51 | + cacheDataset: async () => {}, |
| 52 | + }, |
| 53 | + networkModuleManager: { |
| 54 | + getPeerId: () => ({ toB58String: () => 'peer-self' }), |
| 55 | + }, |
| 56 | + blockchainModuleManager: { |
| 57 | + getMinimumRequiredSignatures: async () => PUBLISH_MIN_NUM_OF_NODE_REPLICATIONS, |
| 58 | + }, |
| 59 | + logger: { |
| 60 | + info: () => {}, |
| 61 | + warn: () => {}, |
| 62 | + error: () => {}, |
| 63 | + }, |
| 64 | + }; |
| 65 | + }; |
| 66 | + |
| 67 | + it('clamps minimumNumberOfNodeReplications to on-chain minimum', async () => { |
| 68 | + const ctx = baseCtx(); |
| 69 | + ctx.blockchainModuleManager.getMinimumRequiredSignatures = async () => 5; // on-chain min |
| 70 | + const controller = new PublishController(ctx); |
| 71 | + |
| 72 | + const req = { |
| 73 | + body: { |
| 74 | + dataset: { public: {} }, |
| 75 | + datasetRoot: '0xroot', |
| 76 | + blockchain: 'hardhat', |
| 77 | + minimumNumberOfNodeReplications: 2, // below chain min |
| 78 | + }, |
| 79 | + }; |
| 80 | + const res = createRes(); |
| 81 | + |
| 82 | + await controller.handleRequest(req, res); |
| 83 | + |
| 84 | + expect(res.statusCode).to.equal(202); |
| 85 | + const added = ctx.commandExecutor._added[0]; |
| 86 | + expect(added.data.minimumNumberOfNodeReplications).to.equal(5); |
| 87 | + }); |
| 88 | + |
| 89 | + it('allows higher user override than on-chain minimum', async () => { |
| 90 | + const ctx = baseCtx(); |
| 91 | + ctx.blockchainModuleManager.getMinimumRequiredSignatures = async () => 3; // on-chain min |
| 92 | + const controller = new PublishController(ctx); |
| 93 | + |
| 94 | + const req = { |
| 95 | + body: { |
| 96 | + dataset: { public: {} }, |
| 97 | + datasetRoot: '0xroot', |
| 98 | + blockchain: 'hardhat', |
| 99 | + minimumNumberOfNodeReplications: 7, // above chain min |
| 100 | + }, |
| 101 | + }; |
| 102 | + const res = createRes(); |
| 103 | + |
| 104 | + await controller.handleRequest(req, res); |
| 105 | + |
| 106 | + expect(res.statusCode).to.equal(202); |
| 107 | + const added = ctx.commandExecutor._added[0]; |
| 108 | + expect(added.data.minimumNumberOfNodeReplications).to.equal(7); |
| 109 | + }); |
| 110 | + |
| 111 | + it('falls back to on-chain minimum when user value is zero or invalid', async () => { |
| 112 | + const ctx = baseCtx(); |
| 113 | + ctx.blockchainModuleManager.getMinimumRequiredSignatures = async () => 4; // on-chain min |
| 114 | + const controller = new PublishController(ctx); |
| 115 | + |
| 116 | + const req = { |
| 117 | + body: { |
| 118 | + dataset: { public: {} }, |
| 119 | + datasetRoot: '0xroot', |
| 120 | + blockchain: 'hardhat', |
| 121 | + minimumNumberOfNodeReplications: 0, // invalid/zero |
| 122 | + }, |
| 123 | + }; |
| 124 | + const res = createRes(); |
| 125 | + |
| 126 | + await controller.handleRequest(req, res); |
| 127 | + |
| 128 | + expect(res.statusCode).to.equal(202); |
| 129 | + const added = ctx.commandExecutor._added[0]; |
| 130 | + expect(added.data.minimumNumberOfNodeReplications).to.equal(4); |
| 131 | + }); |
| 132 | +}); |
0 commit comments