|
| 1 | +/** |
| 2 | + * Tests for `sentry schema` command func body. |
| 3 | + * |
| 4 | + * The schema command browses the in-memory Sentry API schema. |
| 5 | + * All queries are synchronous (no network calls), making this easy to test. |
| 6 | + */ |
| 7 | + |
| 8 | +import { describe, expect, mock, test } from "bun:test"; |
| 9 | +import { schemaCommand } from "../../src/commands/schema.js"; |
| 10 | +import { OutputError } from "../../src/lib/errors.js"; |
| 11 | + |
| 12 | +function createMockContext() { |
| 13 | + const output: unknown[] = []; |
| 14 | + return { |
| 15 | + context: { |
| 16 | + stdout: { |
| 17 | + write: mock((s: string) => { |
| 18 | + output.push(s); |
| 19 | + }), |
| 20 | + }, |
| 21 | + stderr: { |
| 22 | + write: mock((_s: string) => { |
| 23 | + /* suppress */ |
| 24 | + }), |
| 25 | + }, |
| 26 | + cwd: "/tmp", |
| 27 | + }, |
| 28 | + getOutput: () => output.join(""), |
| 29 | + output, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +describe("schemaCommand.func", () => { |
| 34 | + test("no args shows resource summary", async () => { |
| 35 | + const { context, getOutput } = createMockContext(); |
| 36 | + const func = await schemaCommand.loader(); |
| 37 | + await func.call(context, { all: false, json: false }); |
| 38 | + // Should render a resource list |
| 39 | + expect(getOutput()).not.toBe(""); |
| 40 | + }); |
| 41 | + |
| 42 | + test("--all shows flat endpoint list", async () => { |
| 43 | + const { context, getOutput } = createMockContext(); |
| 44 | + const func = await schemaCommand.loader(); |
| 45 | + await func.call(context, { all: true, json: false }); |
| 46 | + const output = getOutput(); |
| 47 | + expect(output).not.toBe(""); |
| 48 | + // All endpoints mode renders more content than resource summary |
| 49 | + expect(output.length).toBeGreaterThan(10); |
| 50 | + }); |
| 51 | + |
| 52 | + test("--search with matches returns endpoints", async () => { |
| 53 | + const { context, getOutput } = createMockContext(); |
| 54 | + const func = await schemaCommand.loader(); |
| 55 | + // "issues" is a common resource — should match many endpoints |
| 56 | + await func.call(context, { all: false, json: false, search: "issues" }); |
| 57 | + const output = getOutput(); |
| 58 | + expect(output).not.toBe(""); |
| 59 | + }); |
| 60 | + |
| 61 | + test("--search with no matches throws OutputError", async () => { |
| 62 | + const { context } = createMockContext(); |
| 63 | + const func = await schemaCommand.loader(); |
| 64 | + const err = await func |
| 65 | + .call(context, { all: false, json: false, search: "nonexistent-xyz-abc-never" }) |
| 66 | + .catch((e: Error) => e); |
| 67 | + expect(err).toBeInstanceOf(OutputError); |
| 68 | + }); |
| 69 | + |
| 70 | + test("resource arg shows endpoints for that resource", async () => { |
| 71 | + const { context, getOutput } = createMockContext(); |
| 72 | + const func = await schemaCommand.loader(); |
| 73 | + // Pass a known resource name |
| 74 | + await func.call(context, { all: false, json: false }, "issues"); |
| 75 | + expect(getOutput()).not.toBe(""); |
| 76 | + }); |
| 77 | + |
| 78 | + test("glob pattern resource matches resources", async () => { |
| 79 | + const { context, getOutput } = createMockContext(); |
| 80 | + const func = await schemaCommand.loader(); |
| 81 | + await func.call(context, { all: false, json: false }, "*issue*"); |
| 82 | + expect(getOutput()).not.toBe(""); |
| 83 | + }); |
| 84 | + |
| 85 | + test("glob pattern resource with no match throws OutputError", async () => { |
| 86 | + const { context } = createMockContext(); |
| 87 | + const func = await schemaCommand.loader(); |
| 88 | + const err = await func |
| 89 | + .call(context, { all: false, json: false }, "*zzz-neverexists-xyz*") |
| 90 | + .catch((e: Error) => e); |
| 91 | + expect(err).toBeInstanceOf(OutputError); |
| 92 | + }); |
| 93 | + |
| 94 | + test("resource + operation shows single endpoint", async () => { |
| 95 | + const { context, getOutput } = createMockContext(); |
| 96 | + const func = await schemaCommand.loader(); |
| 97 | + // Try a resource with a known operation format |
| 98 | + // If not found, it falls back to showing resource endpoints |
| 99 | + await func.call(context, { all: false, json: false }, "issues", "list"); |
| 100 | + expect(getOutput()).not.toBe(""); |
| 101 | + }); |
| 102 | + |
| 103 | + test("unknown resource shows resource summary", async () => { |
| 104 | + const { context } = createMockContext(); |
| 105 | + const func = await schemaCommand.loader(); |
| 106 | + const err = await func |
| 107 | + .call(context, { all: false, json: false }, "nonexistent-resource-xyz") |
| 108 | + .catch((e: Error) => e); |
| 109 | + // Falls through to show all resources via OutputError |
| 110 | + expect(err).toBeInstanceOf(OutputError); |
| 111 | + }); |
| 112 | + |
| 113 | + test("JSON output for --all renders machine-readable", async () => { |
| 114 | + const { context, getOutput } = createMockContext(); |
| 115 | + const func = await schemaCommand.loader(); |
| 116 | + await func.call(context, { all: true, json: true }); |
| 117 | + const output = getOutput(); |
| 118 | + // JSON mode — should be parseable |
| 119 | + expect(() => JSON.parse(output)).not.toThrow(); |
| 120 | + }); |
| 121 | +}); |
0 commit comments