|
1 | | -import { describe, expect, it } from "vitest" |
2 | | -import { formatOutput } from "../src/output.js" |
| 1 | +import { afterEach, describe, expect, it } from "vitest" |
| 2 | +import { formatOutput, setOutputOptions } from "../src/output.js" |
3 | 3 |
|
4 | 4 | describe("formatOutput", () => { |
| 5 | + afterEach(() => { |
| 6 | + setOutputOptions({}) |
| 7 | + }) |
| 8 | + |
5 | 9 | describe("json format", () => { |
6 | 10 | it("formats data as pretty JSON", () => { |
7 | 11 | const data = { name: "test", value: 42 } |
@@ -70,4 +74,134 @@ describe("formatOutput", () => { |
70 | 74 | expect(formatOutput(42, "table")).toBe("42") |
71 | 75 | }) |
72 | 76 | }) |
| 77 | + |
| 78 | + describe("--fields option", () => { |
| 79 | + it("filters top-level fields on a plain object", () => { |
| 80 | + setOutputOptions({ fields: ["name", "collection"] }) |
| 81 | + const data = { |
| 82 | + name: "Cool NFT", |
| 83 | + collection: "cool-cats", |
| 84 | + description: "A cool cat", |
| 85 | + image_url: "https://example.com/img.png", |
| 86 | + } |
| 87 | + const result = JSON.parse(formatOutput(data, "json")) |
| 88 | + expect(result).toEqual({ name: "Cool NFT", collection: "cool-cats" }) |
| 89 | + }) |
| 90 | + |
| 91 | + it("filters fields on items inside wrapped arrays", () => { |
| 92 | + setOutputOptions({ fields: ["identifier", "name"] }) |
| 93 | + const data = { |
| 94 | + nfts: [ |
| 95 | + { |
| 96 | + identifier: "1", |
| 97 | + name: "NFT #1", |
| 98 | + image_url: "https://example.com/1.png", |
| 99 | + }, |
| 100 | + { |
| 101 | + identifier: "2", |
| 102 | + name: "NFT #2", |
| 103 | + image_url: "https://example.com/2.png", |
| 104 | + }, |
| 105 | + ], |
| 106 | + next: "cursor123", |
| 107 | + } |
| 108 | + const result = JSON.parse(formatOutput(data, "json")) |
| 109 | + expect(result).toEqual({ |
| 110 | + nfts: [ |
| 111 | + { identifier: "1", name: "NFT #1" }, |
| 112 | + { identifier: "2", name: "NFT #2" }, |
| 113 | + ], |
| 114 | + next: "cursor123", |
| 115 | + }) |
| 116 | + }) |
| 117 | + |
| 118 | + it("filters fields on a bare array", () => { |
| 119 | + setOutputOptions({ fields: ["name"] }) |
| 120 | + const data = [ |
| 121 | + { name: "Alice", age: 30 }, |
| 122 | + { name: "Bob", age: 25 }, |
| 123 | + ] |
| 124 | + const result = JSON.parse(formatOutput(data, "json")) |
| 125 | + expect(result).toEqual([{ name: "Alice" }, { name: "Bob" }]) |
| 126 | + }) |
| 127 | + |
| 128 | + it("ignores fields that do not exist", () => { |
| 129 | + setOutputOptions({ fields: ["name", "nonexistent"] }) |
| 130 | + const data = { name: "test", value: 42 } |
| 131 | + const result = JSON.parse(formatOutput(data, "json")) |
| 132 | + expect(result).toEqual({ name: "test" }) |
| 133 | + }) |
| 134 | + |
| 135 | + it("returns primitives unchanged", () => { |
| 136 | + setOutputOptions({ fields: ["name"] }) |
| 137 | + expect(formatOutput("hello", "json")).toBe('"hello"') |
| 138 | + }) |
| 139 | + |
| 140 | + it("works with table format", () => { |
| 141 | + setOutputOptions({ fields: ["name"] }) |
| 142 | + const data = [ |
| 143 | + { name: "Alice", age: 30 }, |
| 144 | + { name: "Bob", age: 25 }, |
| 145 | + ] |
| 146 | + const result = formatOutput(data, "table") |
| 147 | + expect(result).toContain("name") |
| 148 | + expect(result).not.toContain("age") |
| 149 | + }) |
| 150 | + }) |
| 151 | + |
| 152 | + describe("--max-lines option", () => { |
| 153 | + it("truncates output exceeding max lines", () => { |
| 154 | + setOutputOptions({ maxLines: 3 }) |
| 155 | + const data = { a: 1, b: 2, c: 3, d: 4, e: 5 } |
| 156 | + const result = formatOutput(data, "json") |
| 157 | + const lines = result.split("\n") |
| 158 | + expect(lines).toHaveLength(4) |
| 159 | + expect(lines[3]).toBe("... (4 more lines)") |
| 160 | + }) |
| 161 | + |
| 162 | + it("does not truncate when output fits within max lines", () => { |
| 163 | + setOutputOptions({ maxLines: 100 }) |
| 164 | + const data = { a: 1 } |
| 165 | + const result = formatOutput(data, "json") |
| 166 | + expect(result).not.toContain("...") |
| 167 | + expect(result).toBe(JSON.stringify(data, null, 2)) |
| 168 | + }) |
| 169 | + |
| 170 | + it("uses singular 'line' for exactly one omitted line", () => { |
| 171 | + setOutputOptions({ maxLines: 2 }) |
| 172 | + const data = { a: 1 } |
| 173 | + const result = formatOutput(data, "json") |
| 174 | + const lines = result.split("\n") |
| 175 | + expect(lines[lines.length - 1]).toBe("... (1 more line)") |
| 176 | + }) |
| 177 | + |
| 178 | + it("works with table format", () => { |
| 179 | + setOutputOptions({ maxLines: 2 }) |
| 180 | + const data = [{ name: "Alice" }, { name: "Bob" }, { name: "Charlie" }] |
| 181 | + const result = formatOutput(data, "table") |
| 182 | + const lines = result.split("\n") |
| 183 | + expect(lines).toHaveLength(3) |
| 184 | + expect(lines[2]).toMatch(/\.\.\. \(\d+ more lines?\)/) |
| 185 | + }) |
| 186 | + }) |
| 187 | + |
| 188 | + describe("--fields and --max-lines combined", () => { |
| 189 | + it("applies field filtering then truncation", () => { |
| 190 | + setOutputOptions({ fields: ["name"], maxLines: 3 }) |
| 191 | + const data = { |
| 192 | + nfts: [ |
| 193 | + { name: "A", id: 1 }, |
| 194 | + { name: "B", id: 2 }, |
| 195 | + { name: "C", id: 3 }, |
| 196 | + { name: "D", id: 4 }, |
| 197 | + ], |
| 198 | + next: "cursor", |
| 199 | + } |
| 200 | + const result = formatOutput(data, "json") |
| 201 | + expect(result).not.toContain("id") |
| 202 | + const lines = result.split("\n") |
| 203 | + expect(lines).toHaveLength(4) |
| 204 | + expect(lines[3]).toMatch(/\.\.\. \(\d+ more lines?\)/) |
| 205 | + }) |
| 206 | + }) |
73 | 207 | }) |
0 commit comments