import { Readable } from "node:stream";

import { initializeApiCredentialClients, receiveApiCredentials } from "../apps/api/dist/supervisor-channel.js";
import { initializeWorkerCredentialClient, receiveWorkerCredentials } from "../apps/worker/dist/supervisor-channel.js";

const marker = `wp0-${Date.now().toString(36)}`;
const api = await receiveApiCredentials(Readable.from([Buffer.from(JSON.stringify({
  "Dada/P0A/admin/pepper": `${marker}-admin-pepper-value-00000000`,
  "Dada/P0A/api/amap": `${marker}-map`,
  "Dada/P0A/api/resend": `${marker}-mail`,
}))]));
const apiClients = initializeApiCredentialClients(api);
if (Object.values(api).some(Boolean)) throw new Error("API credential receive object was not cleared after client initialization.");
apiClients.adminAllowlistPepper.fill(0);

const worker = await receiveWorkerCredentials(Readable.from([Buffer.from(JSON.stringify({
  "Dada/P0A/worker/ai-gateway": `${marker}-ai`,
}))]));
initializeWorkerCredentialClient(worker);
if (Object.values(worker).some(Boolean)) throw new Error("Worker credential receive object was not cleared after client initialization.");

console.log(JSON.stringify({ api_scope: "resend_amap_admin_pepper", receive_buffer: "cleared", status: "passed", worker_scope: "ai_gateway" }));
