executive-ai/test/deploy.test.ts

76 lines
3.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { readFileSync } from "node:fs";
import { describe, expect, it, vi } from "vitest";
import { createApp } from "../src/app.js";
import { loadConfig } from "../src/config.js";
import { browser } from "./helpers.js";
vi.mock("../src/sentry.js", () => ({ reportError: vi.fn(), initSentry: vi.fn() }));
const workflow = readFileSync(new URL("../.gitea/workflows/deploy.yml", import.meta.url), "utf8");
const compose = readFileSync(new URL("../deploy/compose.yml", import.meta.url), "utf8");
/** Блок `env:` верхнего уровня deploy.yml: переменная → выражение. */
function workflowEnv(): Map<string, string> {
const block = workflow.match(/^env:\n((?:[ \t]+.*\n|[ \t]*#.*\n)+)/m)?.[1] ?? "";
const pairs = [...block.matchAll(/^ {2}([A-Z_]+):\s*(.+)$/gm)];
return new Map(pairs.map(([, name, value]) => [name as string, (value as string).trim()]));
}
/** Окружение шага деплоя при заданных secrets репозитория (Actions подставляет "" для отсутствующих). */
function jobEnv(secrets: Record<string, string>): Record<string, string> {
const env: Record<string, string> = {};
for (const [name, value] of workflowEnv()) {
const secret = value.match(/^\$\{\{\s*secrets\.([A-Z_]+)\s*\}\}$/)?.[1];
if (secret) env[name] = secrets[secret] ?? "";
}
return env;
}
/** Окружение контейнера: environment сервиса из compose с подстановкой ${VAR:-default}. */
function containerEnv(hostEnv: Record<string, string>): Record<string, string> {
const env: Record<string, string> = {};
for (const [, name, raw] of compose.matchAll(/^ {6}([A-Z_]+):\s*"(.*)"$/gm)) {
env[name as string] = (raw as string).replace(
/\$\{([A-Z_]+)(?::-([^}]*))?\}/g,
(_, v: string, d = "") => hostEnv[v] || d,
);
}
return env;
}
async function firstPage(secrets: Record<string, string>) {
const config = loadConfig(containerEnv(jobEnv(secrets)));
const app = createApp({ google: config.google, sessionSecret: config.sessionSecret });
const res = await browser(app).get("/");
return { config, res, html: await res.text() };
}
describe("availability / Ключи календаря доходят до выкладки", () => {
it("каждая переменная Google из compose передаётся из secrets в deploy.yml", () => {
const env = workflowEnv();
for (const name of ["GOOGLE_CLIENT_ID", "GOOGLE_CLIENT_SECRET", "SESSION_SECRET"]) {
expect(compose).toContain(`${name}: "\${${name}:-}"`);
expect(env.get(name)).toBe(`\${{ secrets.${name} }}`);
}
});
it("Scenario: Ключи заведены в секретах", async () => {
const { config, res, html } = await firstPage({
GOOGLE_CLIENT_ID: "mock-id.apps.googleusercontent.com",
GOOGLE_CLIENT_SECRET: "mock-secret",
});
expect(config.google.isMock).toBe(false);
expect(res.status).toBe(200);
expect(html).not.toContain('data-testid="mock-banner"');
expect(html).not.toContain('data-testid="sample"');
});
it("Scenario: Ключей в секретах нет", async () => {
const { config, res, html } = await firstPage({});
expect(config.google.isMock).toBe(true);
expect(res.status).toBe(200);
expect(html).toContain('data-testid="mock-banner"');
expect(html).toContain('data-testid="sample"');
});
});