import nock from "nock";
import settings from "../../common/settings.js";
export const TEST_AUTH_TOKEN = "test-auth-token-123";
export const TEST_ACCOUNT_NAME = "test-account";
export const TEST_PROJECT_NAME = "test-project";
export const TEST_BRANCH_NAME = "main";
export const TEST_DEPLOYMENT_NAME = "test-deployment";
export const TEST_API_BASE = settings.ZUPLO_DEVELOPER_API_ENDPOINT;
export function setupAuthenticatedNock(scope) {
    return scope.matchHeader("authorization", `Bearer ${TEST_AUTH_TOKEN}`);
}
export function setupTestEnvironment() {
    process.env.ZUPLO_DEVELOPER_API_ENDPOINT =
        settings.ZUPLO_DEVELOPER_API_ENDPOINT;
    process.env.ZUPLO_API_KEY = TEST_AUTH_TOKEN;
    setupAuthenticatedNock(nock(TEST_API_BASE))
        .get("/v1/who-am-i")
        .reply(200, { user: "test-user@example.com" })
        .persist();
}
export function cleanupTest() {
    nock.cleanAll();
    delete process.env.ZUPLO_DEVELOPER_API_ENDPOINT;
    delete process.env.ZUPLO_API_KEY;
}
export class RequestCapture {
    requests = [];
    capture(req, _interceptor, body) {
        const captured = {
            method: req.method || "GET",
            path: req.path || "",
            headers: this.normalizeHeaders(req.headers || {}),
        };
        if (body) {
            if (typeof body === "string") {
                try {
                    captured.body = JSON.parse(body);
                }
                catch {
                    captured.body = "[FormData or non-JSON body]";
                }
            }
            else {
                captured.body = body;
            }
        }
        this.requests.push(captured);
    }
    getRequests() {
        return [...this.requests];
    }
    clear() {
        this.requests = [];
    }
    normalizeHeaders(headers) {
        const normalized = {};
        for (const [key, value] of Object.entries(headers)) {
            const normalizedKey = key.toLowerCase();
            if (normalizedKey === "authorization") {
                normalized[normalizedKey] = "[REDACTED]";
            }
            else if (normalizedKey === "user-agent") {
                normalized[normalizedKey] = "[USER_AGENT]";
            }
            else if (normalizedKey === "content-length") {
                normalized[normalizedKey] = "[CONTENT_LENGTH]";
            }
            else if (normalizedKey === "host") {
                normalized[normalizedKey] = "[HOST]";
            }
            else if (normalizedKey === "content-type" &&
                String(value).startsWith("multipart/form-data")) {
                normalized[normalizedKey] = "multipart/form-data; boundary=[BOUNDARY]";
            }
            else {
                normalized[normalizedKey] = String(value);
            }
        }
        return normalized;
    }
}
//# sourceMappingURL=test-utils.js.map