|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { Bash } from "../../Bash.js"; |
| 3 | + |
| 4 | +describe("jq basic", () => { |
| 5 | + describe("identity filter", () => { |
| 6 | + it("should pass through JSON with .", async () => { |
| 7 | + const env = new Bash(); |
| 8 | + const result = await env.exec("echo '{\"a\":1}' | jq '.'"); |
| 9 | + expect(result.stdout).toBe('{\n "a": 1\n}\n'); |
| 10 | + expect(result.stderr).toBe(""); |
| 11 | + expect(result.exitCode).toBe(0); |
| 12 | + }); |
| 13 | + |
| 14 | + it("should pretty print arrays", async () => { |
| 15 | + const env = new Bash(); |
| 16 | + const result = await env.exec("echo '[1,2,3]' | jq '.'"); |
| 17 | + expect(result.stdout).toBe("[\n 1,\n 2,\n 3\n]\n"); |
| 18 | + expect(result.exitCode).toBe(0); |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + describe("object access", () => { |
| 23 | + it("should access object key with .key", async () => { |
| 24 | + const env = new Bash(); |
| 25 | + const result = await env.exec("echo '{\"name\":\"test\"}' | jq '.name'"); |
| 26 | + expect(result.stdout).toBe('"test"\n'); |
| 27 | + expect(result.exitCode).toBe(0); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should access nested key with .a.b", async () => { |
| 31 | + const env = new Bash(); |
| 32 | + const result = await env.exec( |
| 33 | + 'echo \'{"a":{"b":"nested"}}\' | jq \'.a.b\'', |
| 34 | + ); |
| 35 | + expect(result.stdout).toBe('"nested"\n'); |
| 36 | + expect(result.exitCode).toBe(0); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should return null for missing key", async () => { |
| 40 | + const env = new Bash(); |
| 41 | + const result = await env.exec("echo '{\"a\":1}' | jq '.missing'"); |
| 42 | + expect(result.stdout).toBe("null\n"); |
| 43 | + expect(result.exitCode).toBe(0); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should access numeric values", async () => { |
| 47 | + const env = new Bash(); |
| 48 | + const result = await env.exec("echo '{\"count\":42}' | jq '.count'"); |
| 49 | + expect(result.stdout).toBe("42\n"); |
| 50 | + expect(result.exitCode).toBe(0); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should access boolean values", async () => { |
| 54 | + const env = new Bash(); |
| 55 | + const result = await env.exec("echo '{\"active\":true}' | jq '.active'"); |
| 56 | + expect(result.stdout).toBe("true\n"); |
| 57 | + expect(result.exitCode).toBe(0); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe("array access", () => { |
| 62 | + it("should access array element with .[0]", async () => { |
| 63 | + const env = new Bash(); |
| 64 | + const result = await env.exec('echo \'["a","b","c"]\' | jq \'.[0]\''); |
| 65 | + expect(result.stdout).toBe('"a"\n'); |
| 66 | + expect(result.exitCode).toBe(0); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should access last element with .[-1]", async () => { |
| 70 | + const env = new Bash(); |
| 71 | + const result = await env.exec('echo \'["a","b","c"]\' | jq \'.[-1]\''); |
| 72 | + expect(result.stdout).toBe('"c"\n'); |
| 73 | + expect(result.exitCode).toBe(0); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should return null for out of bounds index", async () => { |
| 77 | + const env = new Bash(); |
| 78 | + const result = await env.exec("echo '[1,2]' | jq '.[99]'"); |
| 79 | + expect(result.stdout).toBe("null\n"); |
| 80 | + expect(result.exitCode).toBe(0); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe("array iteration", () => { |
| 85 | + it("should iterate array with .[]", async () => { |
| 86 | + const env = new Bash(); |
| 87 | + const result = await env.exec("echo '[1,2,3]' | jq '.[]'"); |
| 88 | + expect(result.stdout).toBe("1\n2\n3\n"); |
| 89 | + expect(result.exitCode).toBe(0); |
| 90 | + }); |
| 91 | + |
| 92 | + it("should iterate object values with .[]", async () => { |
| 93 | + const env = new Bash(); |
| 94 | + const result = await env.exec("echo '{\"a\":1,\"b\":2}' | jq '.[]'"); |
| 95 | + expect(result.stdout).toBe("1\n2\n"); |
| 96 | + expect(result.exitCode).toBe(0); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should iterate nested array with .items[]", async () => { |
| 100 | + const env = new Bash(); |
| 101 | + const result = await env.exec( |
| 102 | + "echo '{\"items\":[1,2,3]}' | jq '.items[]'", |
| 103 | + ); |
| 104 | + expect(result.stdout).toBe("1\n2\n3\n"); |
| 105 | + expect(result.exitCode).toBe(0); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe("pipes", () => { |
| 110 | + it("should pipe filters with |", async () => { |
| 111 | + const env = new Bash(); |
| 112 | + const result = await env.exec( |
| 113 | + "echo '{\"data\":{\"value\":42}}' | jq '.data | .value'", |
| 114 | + ); |
| 115 | + expect(result.stdout).toBe("42\n"); |
| 116 | + expect(result.exitCode).toBe(0); |
| 117 | + }); |
| 118 | + |
| 119 | + it("should chain multiple pipes", async () => { |
| 120 | + const env = new Bash(); |
| 121 | + const result = await env.exec( |
| 122 | + 'echo \'{"a":{"b":{"c":"deep"}}}\' | jq \'.a | .b | .c\'', |
| 123 | + ); |
| 124 | + expect(result.stdout).toBe('"deep"\n'); |
| 125 | + expect(result.exitCode).toBe(0); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + describe("array slicing", () => { |
| 130 | + it("should slice with start and end", async () => { |
| 131 | + const env = new Bash(); |
| 132 | + const result = await env.exec("echo '[0,1,2,3,4,5]' | jq '.[2:4]'"); |
| 133 | + expect(result.stdout).toBe("[\n 2,\n 3\n]\n"); |
| 134 | + }); |
| 135 | + |
| 136 | + it("should slice from start", async () => { |
| 137 | + const env = new Bash(); |
| 138 | + const result = await env.exec("echo '[0,1,2,3,4]' | jq '.[:3]'"); |
| 139 | + expect(result.stdout).toBe("[\n 0,\n 1,\n 2\n]\n"); |
| 140 | + }); |
| 141 | + |
| 142 | + it("should slice to end", async () => { |
| 143 | + const env = new Bash(); |
| 144 | + const result = await env.exec("echo '[0,1,2,3,4]' | jq '.[3:]'"); |
| 145 | + expect(result.stdout).toBe("[\n 3,\n 4\n]\n"); |
| 146 | + }); |
| 147 | + |
| 148 | + it("should slice strings", async () => { |
| 149 | + const env = new Bash(); |
| 150 | + const result = await env.exec("echo '\"hello\"' | jq '.[1:4]'"); |
| 151 | + expect(result.stdout).toBe('"ell"\n'); |
| 152 | + }); |
| 153 | + |
| 154 | + it("should access with negative index in slice", async () => { |
| 155 | + const env = new Bash(); |
| 156 | + const result = await env.exec("echo '[0,1,2,3,4]' | jq '.[-2:]'"); |
| 157 | + expect(result.stdout).toBe("[\n 3,\n 4\n]\n"); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + describe("comma operator", () => { |
| 162 | + it("should output multiple values", async () => { |
| 163 | + const env = new Bash(); |
| 164 | + const result = await env.exec("echo '{\"a\":1,\"b\":2}' | jq '.a, .b'"); |
| 165 | + expect(result.stdout).toBe("1\n2\n"); |
| 166 | + }); |
| 167 | + |
| 168 | + it("should work with three values", async () => { |
| 169 | + const env = new Bash(); |
| 170 | + const result = await env.exec( |
| 171 | + 'echo \'{"x":1,"y":2,"z":3}\' | jq \'.x, .y, .z\'', |
| 172 | + ); |
| 173 | + expect(result.stdout).toBe("1\n2\n3\n"); |
| 174 | + }); |
| 175 | + }); |
| 176 | +}); |
0 commit comments