|
| 1 | +import assert from 'assert'; |
| 2 | + |
| 3 | +import { |
| 4 | + ComparableOutput, |
| 5 | + equalOutput, |
| 6 | + outputDifference, |
| 7 | + outputDifferencesWithExpected, |
| 8 | +} from '../../../src/transaction/descriptor/outputDifference'; |
| 9 | + |
| 10 | +describe('outputDifference', function () { |
| 11 | + function output(script: string, value: bigint | number) { |
| 12 | + const scriptBuffer = Buffer.from(script, 'hex'); |
| 13 | + if (scriptBuffer.toString('hex') !== script) { |
| 14 | + throw new Error('invalid script'); |
| 15 | + } |
| 16 | + return { |
| 17 | + script: Buffer.from(script, 'hex'), |
| 18 | + value: BigInt(value), |
| 19 | + }; |
| 20 | + } |
| 21 | + |
| 22 | + const a = output('aa', 1); |
| 23 | + const a2 = output('aa', 2); |
| 24 | + const b = output('bb', 2); |
| 25 | + const c = output('cc', 3); |
| 26 | + |
| 27 | + describe('equalOutput', function () { |
| 28 | + it('has expected result', function () { |
| 29 | + assert.deepStrictEqual(equalOutput(a, a), true); |
| 30 | + assert.deepStrictEqual(equalOutput(a, a2), false); |
| 31 | + assert.deepStrictEqual(equalOutput(a, b), false); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('outputDifference', function () { |
| 36 | + assert.deepStrictEqual(outputDifference([], []), []); |
| 37 | + assert.deepStrictEqual(outputDifference([a], []), [a]); |
| 38 | + assert.deepStrictEqual(outputDifference([], [a]), []); |
| 39 | + assert.deepStrictEqual(outputDifference([a], [a]), []); |
| 40 | + assert.deepStrictEqual(outputDifference([a, a], [a]), [a]); |
| 41 | + assert.deepStrictEqual(outputDifference([a, a, a], [a]), [a, a]); |
| 42 | + assert.deepStrictEqual(outputDifference([a, b, c], [a, b]), [c]); |
| 43 | + assert.deepStrictEqual(outputDifference([a, b, c, a], [a, b]), [c, a]); |
| 44 | + |
| 45 | + assert.deepStrictEqual(outputDifference([a], [a2]), [a]); |
| 46 | + assert.deepStrictEqual(outputDifference([a2], [a]), [a2]); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('outputDifferencesWithExpected', function () { |
| 50 | + function test( |
| 51 | + outputs: ComparableOutput[], |
| 52 | + recipients: ComparableOutput[], |
| 53 | + expected: { |
| 54 | + missing: ComparableOutput[]; |
| 55 | + explicit: ComparableOutput[]; |
| 56 | + implicit: ComparableOutput[]; |
| 57 | + } |
| 58 | + ) { |
| 59 | + const result = outputDifferencesWithExpected(outputs, recipients); |
| 60 | + assert.deepStrictEqual(result, { |
| 61 | + explicitExternalOutputs: expected.explicit, |
| 62 | + implicitExternalOutputs: expected.implicit, |
| 63 | + missingOutputs: expected.missing, |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + test([a], [], { missing: [], explicit: [], implicit: [a] }); |
| 68 | + test([], [a], { missing: [a], explicit: [], implicit: [] }); |
| 69 | + test([a], [a], { missing: [], explicit: [a], implicit: [] }); |
| 70 | + test([a], [a2], { missing: [a2], explicit: [], implicit: [a] }); |
| 71 | + test([b], [a], { missing: [a], explicit: [], implicit: [b] }); |
| 72 | + test([a, a], [a], { missing: [], explicit: [a], implicit: [a] }); |
| 73 | + test([a, b], [a], { missing: [], explicit: [a], implicit: [b] }); |
| 74 | + }); |
| 75 | +}); |
0 commit comments