|
| 1 | +#!/usr/bin/env node |
| 2 | +// Sandbox integration check: a read pass plus a create/delete write round-trip. |
| 3 | +// Hard-guarded to the sandbox so it can never write to a real account. |
| 4 | +import { YandexDirectClient } from "./client.js"; |
| 5 | +import { loadConfig } from "./config.js"; |
| 6 | + |
| 7 | +interface ObjectResult { |
| 8 | + Id?: number; |
| 9 | + Errors?: { Message?: string }[]; |
| 10 | +} |
| 11 | + |
| 12 | +function today(): string { |
| 13 | + return new Date().toISOString().slice(0, 10); |
| 14 | +} |
| 15 | + |
| 16 | +function firstError(result?: ObjectResult): string { |
| 17 | + return result?.Errors?.map((e) => e.Message).join("; ") ?? "unknown error"; |
| 18 | +} |
| 19 | + |
| 20 | +async function main(): Promise<void> { |
| 21 | + const config = loadConfig(); |
| 22 | + if (!config.sandbox) { |
| 23 | + console.error("Refusing to run: integration writes require the sandbox (set YANDEX_DIRECT_SANDBOX=true)."); |
| 24 | + process.exit(1); |
| 25 | + } |
| 26 | + const client = new YandexDirectClient(config); |
| 27 | + console.log("Yandex Direct sandbox integration check\n"); |
| 28 | + |
| 29 | + // 1. Read: account info (also carries the Units quota header). |
| 30 | + const account = await client.call<{ Clients?: { Login?: string; Currency?: string }[] }>( |
| 31 | + "clients", |
| 32 | + "get", |
| 33 | + { FieldNames: ["Login", "Currency"] }, |
| 34 | + ); |
| 35 | + const c = account.Clients?.[0]; |
| 36 | + console.log(`account: ${c?.Login ?? "?"} (${c?.Currency ?? "?"})`); |
| 37 | + const units = client.units; |
| 38 | + if (units) console.log(`quota: ${units.spent} spent / ${units.rest} left / ${units.limit} limit`); |
| 39 | + |
| 40 | + // 2. Write round-trip: create a campaign, then delete it (leaves the sandbox clean). |
| 41 | + const name = `ci-healthcheck-${Date.now()}`; |
| 42 | + const add = await client.call<{ AddResults?: ObjectResult[] }>("campaigns", "add", { |
| 43 | + Campaigns: [ |
| 44 | + { |
| 45 | + Name: name, |
| 46 | + StartDate: today(), |
| 47 | + TextCampaign: { |
| 48 | + BiddingStrategy: { |
| 49 | + Search: { BiddingStrategyType: "HIGHEST_POSITION" }, |
| 50 | + Network: { BiddingStrategyType: "SERVING_OFF" }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + ], |
| 55 | + }); |
| 56 | + const created = add.AddResults?.[0]; |
| 57 | + if (!created?.Id) { |
| 58 | + throw new Error(`campaign create failed: ${firstError(created)}`); |
| 59 | + } |
| 60 | + console.log(`created campaign ${created.Id}`); |
| 61 | + |
| 62 | + const del = await client.call<{ DeleteResults?: ObjectResult[] }>("campaigns", "delete", { |
| 63 | + SelectionCriteria: { Ids: [created.Id] }, |
| 64 | + }); |
| 65 | + const deleted = del.DeleteResults?.[0]; |
| 66 | + if (deleted?.Errors?.length) { |
| 67 | + throw new Error(`campaign delete failed: ${firstError(deleted)}`); |
| 68 | + } |
| 69 | + console.log(`deleted campaign ${created.Id}`); |
| 70 | + |
| 71 | + console.log("\nIntegration check passed."); |
| 72 | +} |
| 73 | + |
| 74 | +main().catch((err) => { |
| 75 | + console.error(`\nIntegration check FAILED: ${err instanceof Error ? err.message : String(err)}`); |
| 76 | + process.exit(1); |
| 77 | +}); |
0 commit comments