|
| 1 | +import type { CallTrace } from "@ignored/edr-optimism"; |
| 2 | + |
| 3 | +import assert from "node:assert/strict"; |
| 4 | +import { describe, it } from "node:test"; |
| 5 | + |
| 6 | +import chalk from "chalk"; |
| 7 | + |
| 8 | +import { |
| 9 | + formatLogs, |
| 10 | + formatTraces, |
| 11 | +} from "../../../../src/internal/builtin-plugins/solidity-test/formatters.js"; |
| 12 | + |
| 13 | +describe("formatLogs", () => { |
| 14 | + it("should format logs correctly", async () => { |
| 15 | + const lines = ["a", "b", "c"]; |
| 16 | + |
| 17 | + const actual = ` a |
| 18 | + b |
| 19 | + c`; |
| 20 | + |
| 21 | + const expected = formatLogs(lines, 2); |
| 22 | + |
| 23 | + assert.equal(expected, chalk.grey(actual)); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should return an empty string for empty logs", async () => { |
| 27 | + const lines: string[] = []; |
| 28 | + |
| 29 | + const expected = ""; |
| 30 | + |
| 31 | + const actual = formatLogs(lines, 2); |
| 32 | + |
| 33 | + assert.equal(expected, actual); |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +describe("formatTraces", () => { |
| 38 | + it("should format traces correctly", async () => { |
| 39 | + const traces = [ |
| 40 | + { |
| 41 | + kind: 0, |
| 42 | + success: true, |
| 43 | + isCheatcode: false, |
| 44 | + gasUsed: 127552n, |
| 45 | + value: 0n, |
| 46 | + contract: "FailingCounterTest", |
| 47 | + inputs: { name: "setUp", arguments: [] }, |
| 48 | + outputs: new Uint8Array(), |
| 49 | + children: [ |
| 50 | + { |
| 51 | + kind: 3, |
| 52 | + success: true, |
| 53 | + isCheatcode: false, |
| 54 | + gasUsed: 0n, |
| 55 | + value: 0n, |
| 56 | + contract: "console", |
| 57 | + inputs: { name: "log", arguments: ['"Setting up"'] }, |
| 58 | + outputs: new Uint8Array(), |
| 59 | + children: [], |
| 60 | + }, |
| 61 | + { |
| 62 | + kind: 4, |
| 63 | + success: true, |
| 64 | + isCheatcode: false, |
| 65 | + gasUsed: 68915n, |
| 66 | + value: 0n, |
| 67 | + contract: "Counter", |
| 68 | + inputs: new Uint8Array([1, 2, 3]), |
| 69 | + outputs: "344 bytes of code", |
| 70 | + children: [], |
| 71 | + }, |
| 72 | + { |
| 73 | + kind: 3, |
| 74 | + success: true, |
| 75 | + isCheatcode: false, |
| 76 | + gasUsed: 0n, |
| 77 | + value: 0n, |
| 78 | + contract: "console", |
| 79 | + inputs: { name: "log", arguments: ['"Counter set up"'] }, |
| 80 | + outputs: new Uint8Array(), |
| 81 | + children: [], |
| 82 | + }, |
| 83 | + ], |
| 84 | + }, |
| 85 | + { |
| 86 | + kind: 0, |
| 87 | + success: true, |
| 88 | + isCheatcode: false, |
| 89 | + gasUsed: 32272n, |
| 90 | + value: 0n, |
| 91 | + contract: "FailingCounterTest", |
| 92 | + inputs: { name: "testFailFuzzInc", arguments: ["1"] }, |
| 93 | + outputs: new Uint8Array(), |
| 94 | + children: [ |
| 95 | + { |
| 96 | + kind: 3, |
| 97 | + success: true, |
| 98 | + isCheatcode: false, |
| 99 | + gasUsed: 0n, |
| 100 | + value: 0n, |
| 101 | + contract: "console", |
| 102 | + inputs: { name: "log", arguments: ['"Fuzz testing inc fail"'] }, |
| 103 | + outputs: new Uint8Array(), |
| 104 | + children: [], |
| 105 | + }, |
| 106 | + { |
| 107 | + kind: 0, |
| 108 | + success: true, |
| 109 | + isCheatcode: false, |
| 110 | + gasUsed: 22397n, |
| 111 | + value: 0n, |
| 112 | + contract: "Counter", |
| 113 | + inputs: { name: "inc", arguments: [] }, |
| 114 | + outputs: new Uint8Array(), |
| 115 | + children: [], |
| 116 | + }, |
| 117 | + { |
| 118 | + kind: 3, |
| 119 | + success: true, |
| 120 | + isCheatcode: false, |
| 121 | + gasUsed: 402n, |
| 122 | + value: 0n, |
| 123 | + contract: "Counter", |
| 124 | + inputs: { name: "x", arguments: [] }, |
| 125 | + outputs: "1", |
| 126 | + children: [], |
| 127 | + }, |
| 128 | + ], |
| 129 | + }, |
| 130 | + ]; |
| 131 | + |
| 132 | + const expected = ` [127552] ${chalk.green("FailingCounterTest")}::${chalk.green("setUp")}() |
| 133 | + ├─ [0] ${chalk.green("console")}::${chalk.green("log")}("Setting up") ${chalk.yellow("[staticcall]")} |
| 134 | + ├─ [68915] ${chalk.yellow("→ new")} Counter@0x010203 |
| 135 | + | └─ ${chalk.green("←")} 344 bytes of code |
| 136 | + └─ [0] ${chalk.green("console")}::${chalk.green("log")}("Counter set up") ${chalk.yellow("[staticcall]")} |
| 137 | + [32272] ${chalk.green("FailingCounterTest")}::${chalk.green("testFailFuzzInc")}(1) |
| 138 | + ├─ [0] ${chalk.green("console")}::${chalk.green("log")}("Fuzz testing inc fail") ${chalk.yellow("[staticcall]")} |
| 139 | + ├─ [22397] ${chalk.green("Counter")}::${chalk.green("inc")}() |
| 140 | + └─ [402] ${chalk.green("Counter")}::${chalk.green("x")}() ${chalk.yellow("[staticcall]")} |
| 141 | + └─ ${chalk.green("←")} 1`; |
| 142 | + |
| 143 | + const actual = formatTraces(traces, 2); |
| 144 | + |
| 145 | + assert.equal(expected, actual); |
| 146 | + }); |
| 147 | + |
| 148 | + it("should return an empty string for empty traces", async () => { |
| 149 | + const traces: CallTrace[] = []; |
| 150 | + |
| 151 | + const expected = ""; |
| 152 | + |
| 153 | + const actual = formatTraces(traces, 2); |
| 154 | + |
| 155 | + assert.equal(expected, actual); |
| 156 | + }); |
| 157 | +}); |
0 commit comments