|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +vi.mock("../../src/client.js", () => ({ |
| 4 | + get: vi.fn(), |
| 5 | + post: vi.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +import { contentCommand } from "../../src/commands/content.js"; |
| 9 | +import { get, post } from "../../src/client.js"; |
| 10 | + |
| 11 | +let logSpy: ReturnType<typeof vi.spyOn>; |
| 12 | +let errorSpy: ReturnType<typeof vi.spyOn>; |
| 13 | +let exitSpy: ReturnType<typeof vi.spyOn>; |
| 14 | + |
| 15 | +beforeEach(() => { |
| 16 | + logSpy = vi.spyOn(console, "log").mockImplementation(() => {}); |
| 17 | + errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 18 | + exitSpy = vi.spyOn(process, "exit").mockImplementation(() => undefined as never); |
| 19 | +}); |
| 20 | + |
| 21 | +afterEach(() => { |
| 22 | + vi.restoreAllMocks(); |
| 23 | +}); |
| 24 | + |
| 25 | +describe("content command", () => { |
| 26 | + it("lists templates", async () => { |
| 27 | + vi.mocked(get).mockResolvedValue({ |
| 28 | + status: "success", |
| 29 | + templates: [ |
| 30 | + { name: "artist-caption-bedroom", description: "Moody purple bedroom setting" }, |
| 31 | + ], |
| 32 | + }); |
| 33 | + |
| 34 | + await contentCommand.parseAsync(["templates"], { from: "user" }); |
| 35 | + |
| 36 | + expect(get).toHaveBeenCalledWith("/api/content/templates"); |
| 37 | + expect(logSpy).toHaveBeenCalledWith( |
| 38 | + "- artist-caption-bedroom: Moody purple bedroom setting", |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + it("validates an artist", async () => { |
| 43 | + vi.mocked(get).mockResolvedValue({ |
| 44 | + status: "success", |
| 45 | + artist_account_id: "550e8400-e29b-41d4-a716-446655440000", |
| 46 | + ready: true, |
| 47 | + missing: [], |
| 48 | + }); |
| 49 | + |
| 50 | + await contentCommand.parseAsync(["validate", "--artist", "550e8400-e29b-41d4-a716-446655440000"], { from: "user" }); |
| 51 | + |
| 52 | + expect(get).toHaveBeenCalledWith("/api/content/validate", { |
| 53 | + artist_account_id: "550e8400-e29b-41d4-a716-446655440000", |
| 54 | + }); |
| 55 | + expect(logSpy).toHaveBeenCalledWith("Ready: yes"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("estimates content cost", async () => { |
| 59 | + vi.mocked(get).mockResolvedValue({ |
| 60 | + status: "success", |
| 61 | + per_video_estimate_usd: 0.82, |
| 62 | + total_estimate_usd: 1.64, |
| 63 | + }); |
| 64 | + |
| 65 | + await contentCommand.parseAsync(["estimate", "--batch", "2"], { from: "user" }); |
| 66 | + |
| 67 | + expect(get).toHaveBeenCalledWith("/api/content/estimate", { |
| 68 | + lipsync: "false", |
| 69 | + batch: "2", |
| 70 | + compare: "false", |
| 71 | + }); |
| 72 | + expect(logSpy).toHaveBeenCalledWith("Per video: $0.82"); |
| 73 | + }); |
| 74 | + |
| 75 | + it("creates content run", async () => { |
| 76 | + vi.mocked(post).mockResolvedValue({ |
| 77 | + runIds: ["run_abc123"], |
| 78 | + status: "triggered", |
| 79 | + }); |
| 80 | + |
| 81 | + await contentCommand.parseAsync( |
| 82 | + ["create", "--artist", "550e8400-e29b-41d4-a716-446655440000", "--template", "artist-caption-bedroom"], |
| 83 | + { from: "user" }, |
| 84 | + ); |
| 85 | + |
| 86 | + expect(post).toHaveBeenCalledWith("/api/content/create", { |
| 87 | + artist_account_id: "550e8400-e29b-41d4-a716-446655440000", |
| 88 | + template: "artist-caption-bedroom", |
| 89 | + lipsync: false, |
| 90 | + caption_length: "short", |
| 91 | + upscale: false, |
| 92 | + batch: 1, |
| 93 | + }); |
| 94 | + expect(logSpy).toHaveBeenCalledWith(`Run started: run_abc123`); |
| 95 | + }); |
| 96 | + |
| 97 | + it("shows tasks status hint after create", async () => { |
| 98 | + vi.mocked(post).mockResolvedValue({ |
| 99 | + runIds: ["run_abc123"], |
| 100 | + status: "triggered", |
| 101 | + }); |
| 102 | + |
| 103 | + await contentCommand.parseAsync( |
| 104 | + ["create", "--artist", "550e8400-e29b-41d4-a716-446655440000"], |
| 105 | + { from: "user" }, |
| 106 | + ); |
| 107 | + |
| 108 | + expect(logSpy).toHaveBeenCalledWith( |
| 109 | + "Use `recoup tasks status --run <runId>` to check progress.", |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + it("handles non-Error thrown values gracefully", async () => { |
| 114 | + vi.mocked(get).mockRejectedValue("plain string error"); |
| 115 | + |
| 116 | + await contentCommand.parseAsync(["templates"], { from: "user" }); |
| 117 | + |
| 118 | + expect(errorSpy).toHaveBeenCalledWith("Error: plain string error"); |
| 119 | + expect(exitSpy).toHaveBeenCalledWith(1); |
| 120 | + }); |
| 121 | + |
| 122 | + it("prints error when API call fails", async () => { |
| 123 | + vi.mocked(get).mockRejectedValue(new Error("Request failed")); |
| 124 | + |
| 125 | + await contentCommand.parseAsync(["templates"], { from: "user" }); |
| 126 | + |
| 127 | + expect(errorSpy).toHaveBeenCalledWith("Error: Request failed"); |
| 128 | + expect(exitSpy).toHaveBeenCalledWith(1); |
| 129 | + }); |
| 130 | + |
| 131 | + it("errors when --batch is not a positive integer", async () => { |
| 132 | + await contentCommand.parseAsync( |
| 133 | + ["create", "--artist", "test-artist", "--batch", "abc"], |
| 134 | + { from: "user" }, |
| 135 | + ); |
| 136 | + |
| 137 | + expect(errorSpy).toHaveBeenCalledWith("Error: --batch must be a positive integer"); |
| 138 | + expect(exitSpy).toHaveBeenCalledWith(1); |
| 139 | + }); |
| 140 | + |
| 141 | + it("errors when --caption-length is invalid", async () => { |
| 142 | + await contentCommand.parseAsync( |
| 143 | + ["create", "--artist", "test-artist", "--caption-length", "huge"], |
| 144 | + { from: "user" }, |
| 145 | + ); |
| 146 | + |
| 147 | + expect(errorSpy).toHaveBeenCalledWith( |
| 148 | + "Error: --caption-length must be one of: short, medium, long", |
| 149 | + ); |
| 150 | + expect(exitSpy).toHaveBeenCalledWith(1); |
| 151 | + }); |
| 152 | + |
| 153 | + it("errors when API returns no runIds", async () => { |
| 154 | + vi.mocked(post).mockResolvedValue({ |
| 155 | + status: "error", |
| 156 | + }); |
| 157 | + |
| 158 | + await contentCommand.parseAsync( |
| 159 | + ["create", "--artist", "test-artist"], |
| 160 | + { from: "user" }, |
| 161 | + ); |
| 162 | + |
| 163 | + expect(errorSpy).toHaveBeenCalledWith( |
| 164 | + "Error: Response did not include any run IDs", |
| 165 | + ); |
| 166 | + expect(exitSpy).toHaveBeenCalledWith(1); |
| 167 | + }); |
| 168 | + |
| 169 | + it("creates content run with batch flag and shows batch output", async () => { |
| 170 | + vi.mocked(post).mockResolvedValue({ |
| 171 | + runIds: ["run_1", "run_2", "run_3"], |
| 172 | + status: "triggered", |
| 173 | + }); |
| 174 | + |
| 175 | + await contentCommand.parseAsync( |
| 176 | + ["create", "--artist", "test-artist", "--caption-length", "long", "--upscale", "--batch", "3"], |
| 177 | + { from: "user" }, |
| 178 | + ); |
| 179 | + |
| 180 | + expect(post).toHaveBeenCalledWith("/api/content/create", { |
| 181 | + artist_account_id: "test-artist", |
| 182 | + template: "artist-caption-bedroom", |
| 183 | + lipsync: false, |
| 184 | + caption_length: "long", |
| 185 | + upscale: true, |
| 186 | + batch: 3, |
| 187 | + }); |
| 188 | + expect(logSpy).toHaveBeenCalledWith("Batch started: 3 videos"); |
| 189 | + }); |
| 190 | +}); |
| 191 | + |
0 commit comments