|
| 1 | +import { expect, test } from "vite-plus/test"; |
| 2 | +import type { Client } from "bailian-cli-core"; |
| 3 | +import { applyProfileWatermarkToStepInput, type PipelineEnv } from "../src/pipeline/bl-config.ts"; |
| 4 | +import { imageEdit, imageGenerate, videoGenerate } from "../src/pipeline/steps/bl-api.ts"; |
| 5 | +import type { StepContext } from "../src/pipeline/types.ts"; |
| 6 | + |
| 7 | +type CapturedRequest = { |
| 8 | + path?: string; |
| 9 | + method?: string; |
| 10 | + body?: { |
| 11 | + parameters?: { watermark?: boolean }; |
| 12 | + }; |
| 13 | + async?: boolean; |
| 14 | +}; |
| 15 | + |
| 16 | +function makeEnv(watermark: boolean): { |
| 17 | + env: PipelineEnv; |
| 18 | + captured: CapturedRequest[]; |
| 19 | +} { |
| 20 | + const captured: CapturedRequest[] = []; |
| 21 | + const client = { |
| 22 | + uploadFile: async (source: string) => source, |
| 23 | + requestJson: async (opts: CapturedRequest) => { |
| 24 | + captured.push(opts); |
| 25 | + if (opts.async) { |
| 26 | + return { output: { task_id: "task-wm", task_status: "PENDING" } }; |
| 27 | + } |
| 28 | + // Sync image response shape (qwen-image / wan2.7-image). |
| 29 | + return { |
| 30 | + request_id: "req-wm", |
| 31 | + output: { |
| 32 | + choices: [{ message: { content: [{ image: "https://example.com/out.png" }] } }], |
| 33 | + }, |
| 34 | + }; |
| 35 | + }, |
| 36 | + } as unknown as Client; |
| 37 | + |
| 38 | + return { |
| 39 | + env: { |
| 40 | + client, |
| 41 | + settings: { |
| 42 | + quiet: true, |
| 43 | + output: "json", |
| 44 | + outputExplicit: true, |
| 45 | + timeout: 300, |
| 46 | + watermark, |
| 47 | + verbose: false, |
| 48 | + dryRun: false, |
| 49 | + telemetry: false, |
| 50 | + } as PipelineEnv["settings"], |
| 51 | + }, |
| 52 | + captured, |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +function makeCtx(): StepContext { |
| 57 | + return { dryRun: false, signal: new AbortController().signal, timeoutSeconds: 1 }; |
| 58 | +} |
| 59 | + |
| 60 | +test("pipeline imageGenerate inherits Profile watermark=false when step omits it", async () => { |
| 61 | + const { env, captured } = makeEnv(false); |
| 62 | + await imageGenerate(env, { prompt: "A cat" }, makeCtx()); |
| 63 | + expect(captured[0]?.body?.parameters?.watermark).toBe(false); |
| 64 | +}); |
| 65 | + |
| 66 | +test("pipeline imageGenerate keeps step watermark over Profile", async () => { |
| 67 | + const { env, captured } = makeEnv(false); |
| 68 | + await imageGenerate(env, { prompt: "A cat", watermark: true }, makeCtx()); |
| 69 | + expect(captured[0]?.body?.parameters?.watermark).toBe(true); |
| 70 | +}); |
| 71 | + |
| 72 | +test("pipeline imageEdit inherits Profile watermark=false when step omits it", async () => { |
| 73 | + const { env, captured } = makeEnv(false); |
| 74 | + await imageEdit(env, { prompt: "Blue sky", image: "https://example.com/in.png" }, makeCtx()); |
| 75 | + expect(captured[0]?.body?.parameters?.watermark).toBe(false); |
| 76 | +}); |
| 77 | + |
| 78 | +test("pipeline videoGenerate inherits Profile watermark=false when step omits it", async () => { |
| 79 | + const { env, captured } = makeEnv(false); |
| 80 | + // First call submits async task; pollTaskWithOptions will keep polling — mock SUCCEEDED quickly. |
| 81 | + const client = env.client as unknown as { |
| 82 | + requestJson: (opts: CapturedRequest) => Promise<unknown>; |
| 83 | + }; |
| 84 | + let calls = 0; |
| 85 | + client.requestJson = async (opts: CapturedRequest) => { |
| 86 | + captured.push(opts); |
| 87 | + calls += 1; |
| 88 | + if (calls === 1) { |
| 89 | + return { output: { task_id: "task-wm", task_status: "PENDING" } }; |
| 90 | + } |
| 91 | + return { |
| 92 | + output: { |
| 93 | + task_id: "task-wm", |
| 94 | + task_status: "SUCCEEDED", |
| 95 | + video_url: "https://example.com/out.mp4", |
| 96 | + }, |
| 97 | + }; |
| 98 | + }; |
| 99 | + |
| 100 | + await videoGenerate( |
| 101 | + env, |
| 102 | + { prompt: "A cat walks", "poll-interval": 0 }, |
| 103 | + { ...makeCtx(), timeoutSeconds: 5 }, |
| 104 | + ); |
| 105 | + expect(captured[0]?.body?.parameters?.watermark).toBe(false); |
| 106 | +}); |
| 107 | + |
| 108 | +test("pipeline defaults watermark to true when Profile leaves the compliance default", async () => { |
| 109 | + const { env, captured } = makeEnv(true); |
| 110 | + await imageGenerate(env, { prompt: "A cat" }, makeCtx()); |
| 111 | + expect(captured[0]?.body?.parameters?.watermark).toBe(true); |
| 112 | +}); |
| 113 | + |
| 114 | +test("applyProfileWatermarkToStepInput fills omitted watermark from Settings", () => { |
| 115 | + const settings = { watermark: false } as PipelineEnv["settings"]; |
| 116 | + expect( |
| 117 | + applyProfileWatermarkToStepInput("image/generate", { prompt: "A cat" }, settings).watermark, |
| 118 | + ).toBe(false); |
| 119 | + expect( |
| 120 | + applyProfileWatermarkToStepInput( |
| 121 | + "image/generate", |
| 122 | + { prompt: "A cat", watermark: true }, |
| 123 | + settings, |
| 124 | + ).watermark, |
| 125 | + ).toBe(true); |
| 126 | + expect( |
| 127 | + applyProfileWatermarkToStepInput("text/chat", { message: "hi" }, settings).watermark, |
| 128 | + ).toBeUndefined(); |
| 129 | +}); |
0 commit comments