|
| 1 | +import { existsSync } from "node:fs"; |
| 2 | +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { dirname, join } from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | +import { |
| 7 | + NodeFileSystem, |
| 8 | + createKernel, |
| 9 | + createWasmVmRuntime, |
| 10 | + describeIf, |
| 11 | +} from "@agentos/test-harness"; |
| 12 | +import type { Kernel } from "@agentos/test-harness"; |
| 13 | +import { afterEach, expect, it } from "vitest"; |
| 14 | + |
| 15 | +const YQ_COMMAND_DIR = fileURLToPath(new URL("../bin", import.meta.url)); |
| 16 | +const hasYqPackageBinary = existsSync(join(YQ_COMMAND_DIR, "yq")); |
| 17 | + |
| 18 | +let tempRoot: string | undefined; |
| 19 | + |
| 20 | +async function writeFixture(path: string, contents: string): Promise<void> { |
| 21 | + if (!tempRoot) throw new Error("fixture root not initialized"); |
| 22 | + const hostPath = join(tempRoot, path.replace(/^\/+/, "")); |
| 23 | + await mkdir(dirname(hostPath), { recursive: true }); |
| 24 | + await writeFile(hostPath, contents); |
| 25 | +} |
| 26 | + |
| 27 | +async function createTestVFS(): Promise<NodeFileSystem> { |
| 28 | + tempRoot = await mkdtemp(join(tmpdir(), "agentos-yq-")); |
| 29 | + await writeFixture( |
| 30 | + "/project/services.yaml", |
| 31 | + [ |
| 32 | + "services:", |
| 33 | + " - name: api", |
| 34 | + " enabled: true", |
| 35 | + " port: 8080", |
| 36 | + " - name: worker", |
| 37 | + " enabled: false", |
| 38 | + " port: 9090", |
| 39 | + ].join("\n") + "\n", |
| 40 | + ); |
| 41 | + await writeFixture( |
| 42 | + "/project/services.json", |
| 43 | + JSON.stringify({ services: [{ name: "api" }, { name: "worker" }] }) + "\n", |
| 44 | + ); |
| 45 | + await writeFixture( |
| 46 | + "/project/config.toml", |
| 47 | + ["[server]", 'name = "agentos"', "port = 7331", "enabled = true"].join("\n") + |
| 48 | + "\n", |
| 49 | + ); |
| 50 | + await writeFixture( |
| 51 | + "/project/inventory.xml", |
| 52 | + '<inventory><item id="a">hammer</item><item id="b">nail</item></inventory>\n', |
| 53 | + ); |
| 54 | + await writeFixture("/project/broken.yaml", "services:\n - name: ok\n bad"); |
| 55 | + return new NodeFileSystem({ root: tempRoot }); |
| 56 | +} |
| 57 | + |
| 58 | +function lines(stdout: string): string[] { |
| 59 | + return stdout.split("\n").filter((line) => line.length > 0); |
| 60 | +} |
| 61 | + |
| 62 | +describeIf(hasYqPackageBinary, "yq command", { timeout: 10_000 }, () => { |
| 63 | + let kernel: Kernel | undefined; |
| 64 | + |
| 65 | + afterEach(async () => { |
| 66 | + await kernel?.dispose(); |
| 67 | + kernel = undefined; |
| 68 | + if (tempRoot) { |
| 69 | + await rm(tempRoot, { recursive: true, force: true }); |
| 70 | + tempRoot = undefined; |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + async function mountFixture(): Promise<void> { |
| 75 | + const vfs = await createTestVFS(); |
| 76 | + kernel = createKernel({ filesystem: vfs }); |
| 77 | + await kernel.mount(createWasmVmRuntime({ commandDirs: [YQ_COMMAND_DIR] })); |
| 78 | + } |
| 79 | + |
| 80 | + async function runYq(args: string[]) { |
| 81 | + if (!kernel) throw new Error("kernel not mounted"); |
| 82 | + let stdout = ""; |
| 83 | + let stderr = ""; |
| 84 | + const proc = kernel.spawn("yq", args, { |
| 85 | + onStdout: (chunk) => { |
| 86 | + stdout += Buffer.from(chunk).toString("utf8"); |
| 87 | + }, |
| 88 | + onStderr: (chunk) => { |
| 89 | + stderr += Buffer.from(chunk).toString("utf8"); |
| 90 | + }, |
| 91 | + }); |
| 92 | + const exitCode = await proc.wait(); |
| 93 | + await new Promise<void>((resolve) => setTimeout(resolve, 0)); |
| 94 | + return { stdout, stderr, exitCode }; |
| 95 | + } |
| 96 | + |
| 97 | + it("filters YAML files and emits raw strings", async () => { |
| 98 | + await mountFixture(); |
| 99 | + |
| 100 | + const result = await runYq([ |
| 101 | + "-r", |
| 102 | + ".services[] | select(.enabled) | .name", |
| 103 | + "/project/services.yaml", |
| 104 | + ]); |
| 105 | + expect(result.exitCode, result.stderr || result.stdout).toBe(0); |
| 106 | + expect(lines(result.stdout)).toEqual(["api"]); |
| 107 | + }); |
| 108 | + |
| 109 | + it("converts YAML query results to compact JSON", async () => { |
| 110 | + await mountFixture(); |
| 111 | + |
| 112 | + const result = await runYq([ |
| 113 | + "-o", |
| 114 | + "json", |
| 115 | + "-c", |
| 116 | + "{names: [.services[].name], ports: [.services[].port]}", |
| 117 | + "/project/services.yaml", |
| 118 | + ]); |
| 119 | + expect(result.exitCode, result.stderr || result.stdout).toBe(0); |
| 120 | + expect(JSON.parse(result.stdout.trim())).toEqual({ |
| 121 | + names: ["api", "worker"], |
| 122 | + ports: [8080, 9090], |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it("reads JSON files explicitly", async () => { |
| 127 | + await mountFixture(); |
| 128 | + |
| 129 | + const result = await runYq([ |
| 130 | + "-p", |
| 131 | + "json", |
| 132 | + "-r", |
| 133 | + ".services[].name", |
| 134 | + "/project/services.json", |
| 135 | + ]); |
| 136 | + expect(result.exitCode, result.stderr || result.stdout).toBe(0); |
| 137 | + expect(lines(result.stdout)).toEqual(["api", "worker"]); |
| 138 | + }); |
| 139 | + |
| 140 | + it("reads TOML files explicitly", async () => { |
| 141 | + await mountFixture(); |
| 142 | + |
| 143 | + const result = await runYq([ |
| 144 | + "-p", |
| 145 | + "toml", |
| 146 | + "-o", |
| 147 | + "json", |
| 148 | + "-c", |
| 149 | + ".server", |
| 150 | + "/project/config.toml", |
| 151 | + ]); |
| 152 | + expect(result.exitCode, result.stderr || result.stdout).toBe(0); |
| 153 | + expect(JSON.parse(result.stdout.trim())).toEqual({ |
| 154 | + name: "agentos", |
| 155 | + port: 7331, |
| 156 | + enabled: true, |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + it("reads XML files explicitly", async () => { |
| 161 | + await mountFixture(); |
| 162 | + |
| 163 | + const result = await runYq([ |
| 164 | + "-p", |
| 165 | + "xml", |
| 166 | + "-r", |
| 167 | + '.inventory.item[]["#text"]', |
| 168 | + "/project/inventory.xml", |
| 169 | + ]); |
| 170 | + expect(result.exitCode, result.stderr || result.stdout).toBe(0); |
| 171 | + expect(lines(result.stdout)).toEqual(["hammer", "nail"]); |
| 172 | + }); |
| 173 | + |
| 174 | + it("fails with a parse error for invalid YAML", async () => { |
| 175 | + await mountFixture(); |
| 176 | + |
| 177 | + const result = await runYq([".", "/project/broken.yaml"]); |
| 178 | + expect(result.exitCode).not.toBe(0); |
| 179 | + expect(result.stderr).toContain("invalid YAML"); |
| 180 | + }); |
| 181 | +}); |
0 commit comments