|
| 1 | +import { readFile } from "node:fs/promises"; |
| 2 | +import { spawnSync } from "node:child_process"; |
| 3 | +import path from "node:path"; |
| 4 | +import process from "node:process"; |
| 5 | +import { setTimeout as delay } from "node:timers/promises"; |
| 6 | + |
| 7 | +const root = process.cwd(); |
| 8 | +const backendImage = process.env.BACKEND_IMAGE; |
| 9 | +const frontendImage = process.env.FRONTEND_IMAGE; |
| 10 | + |
| 11 | +if (!backendImage || !frontendImage) { |
| 12 | + console.error("Set BACKEND_IMAGE and FRONTEND_IMAGE before running the release smoke check."); |
| 13 | + process.exit(1); |
| 14 | +} |
| 15 | + |
| 16 | +const backendUrl = process.env.BACKEND_SMOKE_URL ?? "http://127.0.0.1:8000"; |
| 17 | +const frontendUrl = process.env.FRONTEND_SMOKE_URL ?? "http://127.0.0.1:3000"; |
| 18 | +const backendContainer = `cv-kit-backend-smoke-${Date.now()}`; |
| 19 | +const frontendContainer = `cv-kit-frontend-smoke-${Date.now()}`; |
| 20 | +const networkName = `cv-kit-smoke-${Date.now()}`; |
| 21 | +const fixturePath = path.join(root, "backend", "tests", "fixtures", "detection-scene.png"); |
| 22 | + |
| 23 | +let cleanedUp = false; |
| 24 | + |
| 25 | +function run(command, args, options = {}) { |
| 26 | + const result = spawnSync(command, args, { |
| 27 | + encoding: "utf8", |
| 28 | + shell: process.platform === "win32", |
| 29 | + stdio: options.capture ? "pipe" : "inherit", |
| 30 | + }); |
| 31 | + |
| 32 | + if (result.error) { |
| 33 | + throw result.error; |
| 34 | + } |
| 35 | + |
| 36 | + if (!options.allowFailure && result.status !== 0) { |
| 37 | + const details = [result.stdout, result.stderr].filter(Boolean).join("\n").trim(); |
| 38 | + throw new Error( |
| 39 | + details |
| 40 | + ? `Command failed: ${command} ${args.join(" ")}\n${details}` |
| 41 | + : `Command failed: ${command} ${args.join(" ")}`, |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + return result; |
| 46 | +} |
| 47 | + |
| 48 | +async function waitFor(label, action, options = {}) { |
| 49 | + const attempts = options.attempts ?? 30; |
| 50 | + const intervalMs = options.intervalMs ?? 2000; |
| 51 | + let lastError = new Error(`${label} did not finish.`); |
| 52 | + |
| 53 | + for (let attempt = 1; attempt <= attempts; attempt += 1) { |
| 54 | + try { |
| 55 | + return await action(); |
| 56 | + } catch (error) { |
| 57 | + lastError = error instanceof Error ? error : new Error(String(error)); |
| 58 | + if (attempt === attempts) { |
| 59 | + break; |
| 60 | + } |
| 61 | + |
| 62 | + console.log(`${label} not ready yet (${attempt}/${attempts}). Retrying...`); |
| 63 | + await delay(intervalMs); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + throw lastError; |
| 68 | +} |
| 69 | + |
| 70 | +async function expectText(url, snippet) { |
| 71 | + const response = await fetch(url); |
| 72 | + const text = await response.text(); |
| 73 | + |
| 74 | + if (!response.ok) { |
| 75 | + throw new Error(`Expected ${url} to return 2xx, received ${response.status}.`); |
| 76 | + } |
| 77 | + |
| 78 | + if (!text.includes(snippet)) { |
| 79 | + throw new Error(`Expected ${url} to include "${snippet}".`); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +async function expectJson(url, predicate, label) { |
| 84 | + const response = await fetch(url); |
| 85 | + const body = await response.text(); |
| 86 | + |
| 87 | + if (!response.ok) { |
| 88 | + throw new Error(`Expected ${url} to return 2xx, received ${response.status}.`); |
| 89 | + } |
| 90 | + |
| 91 | + const payload = JSON.parse(body); |
| 92 | + if (!predicate(payload)) { |
| 93 | + throw new Error(`Unexpected payload for ${label}.`); |
| 94 | + } |
| 95 | + |
| 96 | + return payload; |
| 97 | +} |
| 98 | + |
| 99 | +async function runInferenceSmoke() { |
| 100 | + const bytes = await readFile(fixturePath); |
| 101 | + const formData = new FormData(); |
| 102 | + formData.set("file", new Blob([bytes], { type: "image/png" }), "detection-scene.png"); |
| 103 | + formData.set("pipeline_id", "starter-detection"); |
| 104 | + |
| 105 | + const response = await fetch(`${backendUrl}/api/v1/analyze`, { |
| 106 | + method: "POST", |
| 107 | + body: formData, |
| 108 | + }); |
| 109 | + const body = await response.text(); |
| 110 | + |
| 111 | + if (!response.ok) { |
| 112 | + throw new Error(`Inference smoke request failed with ${response.status}.\n${body}`); |
| 113 | + } |
| 114 | + |
| 115 | + const payload = JSON.parse(body); |
| 116 | + if (payload?.pipeline?.id !== "starter-detection") { |
| 117 | + throw new Error("Smoke inference returned the wrong pipeline id."); |
| 118 | + } |
| 119 | + |
| 120 | + if (!Array.isArray(payload?.detections) || payload.detections.length === 0) { |
| 121 | + throw new Error("Smoke inference returned no detections."); |
| 122 | + } |
| 123 | + |
| 124 | + if (!payload?.image?.width || !payload?.image?.height) { |
| 125 | + throw new Error("Smoke inference returned invalid image dimensions."); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +function cleanup() { |
| 130 | + if (cleanedUp) { |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + cleanedUp = true; |
| 135 | + |
| 136 | + run("docker", ["rm", "-f", frontendContainer], { allowFailure: true }); |
| 137 | + run("docker", ["rm", "-f", backendContainer], { allowFailure: true }); |
| 138 | + run("docker", ["network", "rm", networkName], { allowFailure: true }); |
| 139 | +} |
| 140 | + |
| 141 | +function printContainerLogs() { |
| 142 | + console.log("\nBackend container logs:"); |
| 143 | + run("docker", ["logs", backendContainer], { allowFailure: true }); |
| 144 | + |
| 145 | + console.log("\nFrontend container logs:"); |
| 146 | + run("docker", ["logs", frontendContainer], { allowFailure: true }); |
| 147 | +} |
| 148 | + |
| 149 | +process.on("SIGINT", () => { |
| 150 | + cleanup(); |
| 151 | + process.exit(130); |
| 152 | +}); |
| 153 | + |
| 154 | +process.on("SIGTERM", () => { |
| 155 | + cleanup(); |
| 156 | + process.exit(143); |
| 157 | +}); |
| 158 | + |
| 159 | +try { |
| 160 | + run("docker", ["network", "create", networkName]); |
| 161 | + |
| 162 | + await waitFor( |
| 163 | + "Backend image pull", |
| 164 | + async () => { |
| 165 | + run("docker", ["pull", backendImage], { capture: true }); |
| 166 | + }, |
| 167 | + { attempts: 12, intervalMs: 10000 }, |
| 168 | + ); |
| 169 | + |
| 170 | + await waitFor( |
| 171 | + "Frontend image pull", |
| 172 | + async () => { |
| 173 | + run("docker", ["pull", frontendImage], { capture: true }); |
| 174 | + }, |
| 175 | + { attempts: 12, intervalMs: 10000 }, |
| 176 | + ); |
| 177 | + |
| 178 | + run("docker", [ |
| 179 | + "run", |
| 180 | + "--detach", |
| 181 | + "--rm", |
| 182 | + "--name", |
| 183 | + backendContainer, |
| 184 | + "--network", |
| 185 | + networkName, |
| 186 | + "--publish", |
| 187 | + "8000:8000", |
| 188 | + backendImage, |
| 189 | + ]); |
| 190 | + |
| 191 | + await waitFor( |
| 192 | + "Backend health", |
| 193 | + async () => |
| 194 | + expectJson( |
| 195 | + `${backendUrl}/health`, |
| 196 | + (payload) => payload?.status === "ok", |
| 197 | + "backend health", |
| 198 | + ), |
| 199 | + ); |
| 200 | + |
| 201 | + await expectJson( |
| 202 | + `${backendUrl}/api/v1/pipelines`, |
| 203 | + (payload) => |
| 204 | + Array.isArray(payload?.pipelines) && |
| 205 | + payload.pipelines.some((item) => item?.id === "starter-detection"), |
| 206 | + "pipeline catalog", |
| 207 | + ); |
| 208 | + |
| 209 | + await runInferenceSmoke(); |
| 210 | + |
| 211 | + run("docker", [ |
| 212 | + "run", |
| 213 | + "--detach", |
| 214 | + "--rm", |
| 215 | + "--name", |
| 216 | + frontendContainer, |
| 217 | + "--network", |
| 218 | + networkName, |
| 219 | + "--publish", |
| 220 | + "3000:3000", |
| 221 | + "--env", |
| 222 | + `NEXT_PUBLIC_API_BASE_URL=http://${backendContainer}:8000/api/v1`, |
| 223 | + frontendImage, |
| 224 | + ]); |
| 225 | + |
| 226 | + await waitFor( |
| 227 | + "Frontend home page", |
| 228 | + async () => |
| 229 | + expectText( |
| 230 | + frontendUrl, |
| 231 | + "A detection-first computer vision kit with room to grow.", |
| 232 | + ), |
| 233 | + ); |
| 234 | + |
| 235 | + await expectText( |
| 236 | + `${frontendUrl}/webcam`, |
| 237 | + "Webcam mode is an extension, not the template main story.", |
| 238 | + ); |
| 239 | + |
| 240 | + console.log("Release smoke check passed."); |
| 241 | +} catch (error) { |
| 242 | + console.error(error instanceof Error ? error.message : String(error)); |
| 243 | + printContainerLogs(); |
| 244 | + process.exitCode = 1; |
| 245 | +} finally { |
| 246 | + cleanup(); |
| 247 | +} |
0 commit comments