|
| 1 | +import { describe, it } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { createReplConsole, type ConsoleOutput } from "../src/index.js"; |
| 4 | + |
| 5 | +function collect() { |
| 6 | + const outputs: ConsoleOutput[] = []; |
| 7 | + const replConsole = createReplConsole((output) => outputs.push(output)); |
| 8 | + return { outputs, replConsole }; |
| 9 | +} |
| 10 | + |
| 11 | +describe("createReplConsole", () => { |
| 12 | + describe("log", () => { |
| 13 | + it("emits stdout with joined, formatted arguments", () => { |
| 14 | + const { outputs, replConsole } = collect(); |
| 15 | + replConsole.log("hello", 42, { a: 1 }); |
| 16 | + assert.deepStrictEqual(outputs, [ |
| 17 | + { type: "stdout", message: 'hello 42 { a: 1 }' }, |
| 18 | + ]); |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + describe("error", () => { |
| 23 | + it("emits stderr", () => { |
| 24 | + const { outputs, replConsole } = collect(); |
| 25 | + replConsole.error("boom"); |
| 26 | + assert.deepStrictEqual(outputs, [{ type: "stderr", message: "boom" }]); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe("warn", () => { |
| 31 | + it("emits stderr", () => { |
| 32 | + const { outputs, replConsole } = collect(); |
| 33 | + replConsole.warn("careful"); |
| 34 | + assert.deepStrictEqual(outputs, [ |
| 35 | + { type: "stderr", message: "careful" }, |
| 36 | + ]); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe("info", () => { |
| 41 | + it("emits stdout", () => { |
| 42 | + const { outputs, replConsole } = collect(); |
| 43 | + replConsole.info("fyi"); |
| 44 | + assert.deepStrictEqual(outputs, [{ type: "stdout", message: "fyi" }]); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe("time / timeEnd", () => { |
| 49 | + it("emits elapsed time on stdout for a matching label", () => { |
| 50 | + const { outputs, replConsole } = collect(); |
| 51 | + replConsole.time("t"); |
| 52 | + replConsole.timeEnd("t"); |
| 53 | + assert.strictEqual(outputs.length, 1); |
| 54 | + assert.strictEqual(outputs[0]?.type, "stdout"); |
| 55 | + assert.match(outputs[0]!.message, /^t: \d+(\.\d+)?m?s$/); |
| 56 | + }); |
| 57 | + |
| 58 | + it("defaults the label to 'default'", () => { |
| 59 | + const { outputs, replConsole } = collect(); |
| 60 | + replConsole.time(); |
| 61 | + replConsole.timeEnd(); |
| 62 | + assert.strictEqual(outputs.length, 1); |
| 63 | + assert.match(outputs[0]!.message, /^default: \d+(\.\d+)?m?s$/); |
| 64 | + }); |
| 65 | + |
| 66 | + it("warns on stderr when starting a timer with a label already in use", () => { |
| 67 | + const { outputs, replConsole } = collect(); |
| 68 | + replConsole.time("t"); |
| 69 | + replConsole.time("t"); |
| 70 | + assert.deepStrictEqual(outputs, [ |
| 71 | + { |
| 72 | + type: "stderr", |
| 73 | + message: "Warning: Label 't' already exists for console.time()", |
| 74 | + }, |
| 75 | + ]); |
| 76 | + }); |
| 77 | + |
| 78 | + it("warns on stderr when ending a timer with no matching label", () => { |
| 79 | + const { outputs, replConsole } = collect(); |
| 80 | + replConsole.timeEnd("missing"); |
| 81 | + assert.deepStrictEqual(outputs, [ |
| 82 | + { |
| 83 | + type: "stderr", |
| 84 | + message: "Warning: No such label 'missing' for console.timeEnd()", |
| 85 | + }, |
| 86 | + ]); |
| 87 | + }); |
| 88 | + |
| 89 | + it("allows reusing a label after it has been ended", () => { |
| 90 | + const { outputs, replConsole } = collect(); |
| 91 | + replConsole.time("t"); |
| 92 | + replConsole.timeEnd("t"); |
| 93 | + replConsole.time("t"); |
| 94 | + replConsole.timeEnd("t"); |
| 95 | + assert.strictEqual(outputs.length, 2); |
| 96 | + assert.ok(outputs.every((o) => o.type === "stdout")); |
| 97 | + }); |
| 98 | + }); |
| 99 | +}); |
0 commit comments