Files
pikasTech-unidesk/scripts/native/hwlab/runtime-gitops-verify.test.ts
T

112 lines
4.5 KiB
TypeScript

import { afterEach, expect, test } from "bun:test";
import { spawnSync } from "node:child_process";
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
const roots: string[] = [];
const script = resolve(import.meta.dir, "runtime-gitops-verify.mjs");
afterEach(() => {
while (roots.length > 0) rmSync(roots.pop()!, { recursive: true, force: true });
});
test("accepts the complete transactional projection capability env", () => {
const fixture = createFixture();
const result = runVerify(fixture.root);
expect(result.status).toBe(0);
expect(result.stderr).toContain('"code-agent-runtime-kafka-event-bridge-enabled"');
});
test("fails closed when a required workload capability env is missing", () => {
const fixture = createFixture({ omitWebEnv: "HWLAB_WORKBENCH_PROJECTION_REALTIME_ENABLED" });
const result = runVerify(fixture.root);
expect(result.status).toBe(48);
expect(result.stderr).toContain('"reason":"code-agent-runtime-capability-env-invalid"');
expect(result.stderr).toContain('"name":"HWLAB_WORKBENCH_PROJECTION_REALTIME_ENABLED"');
});
test("fails closed when a required workload was not matched", () => {
const fixture = createFixture({ omitWebWorkload: true });
const result = runVerify(fixture.root);
expect(result.status).toBe(48);
expect(result.stderr).toContain('"reason":"code-agent-runtime-workload-missing"');
expect(result.stderr).toContain('"hwlab-cloud-web"');
});
test("fails closed when the transactional projection authority is incomplete", () => {
const fixture = createFixture({ featureOverrides: { projectionOutboxRelay: false } });
const result = runVerify(fixture.root);
expect(result.status).toBe(48);
expect(result.stderr).toContain('"reason":"kafka-event-bridge-authority-invalid"');
expect(result.stderr).toContain('"projectionOutboxRelay"');
});
function createFixture(options: { omitWebEnv?: string; omitWebWorkload?: boolean; featureOverrides?: Record<string, boolean> } = {}) {
const root = mkdtempSync(join(tmpdir(), "runtime-gitops-verify-"));
roots.push(root);
const runtimeDir = join(root, "runtime");
mkdirSync(runtimeDir);
writeFileSync(join(root, "overlay.json"), JSON.stringify(overlay(options.featureOverrides)));
writeFileSync(join(runtimeDir, "cloud-api.yaml"), deploymentYaml("hwlab-cloud-api", cloudApiEnv()));
if (!options.omitWebWorkload) {
writeFileSync(join(runtimeDir, "cloud-web.yaml"), deploymentYaml("hwlab-cloud-web", cloudWebEnv().filter(([name]) => name !== options.omitWebEnv)));
}
return { root };
}
function runVerify(root: string) {
return spawnSync(process.execPath, [script], {
cwd: root,
env: { ...process.env, UNIDESK_RUNTIME_GITOPS_OVERLAY_FILE: join(root, "overlay.json") },
encoding: "utf8",
});
}
function overlay(featureOverrides: Record<string, boolean> = {}) {
return {
runtimePath: "runtime",
codeAgentRuntime: {
enabled: true,
kafkaEventBridge: {
enabled: true,
features: {
directPublish: false,
liveKafkaSse: false,
kafkaRefreshReplay: false,
transactionalProjector: true,
projectionOutboxRelay: true,
projectionRealtime: true,
...featureOverrides,
},
},
},
};
}
function cloudApiEnv(): Array<[string, string]> {
return [
["HWLAB_KAFKA_ENABLED", "true"],
["HWLAB_KAFKA_AGENTRUN_EVENT_CONSUME_ENABLED", "true"],
["HWLAB_KAFKA_DIRECT_PUBLISH_ENABLED", "false"],
["HWLAB_WORKBENCH_LIVE_KAFKA_SSE_ENABLED", "false"],
["HWLAB_WORKBENCH_KAFKA_REFRESH_REPLAY_ENABLED", "false"],
["HWLAB_KAFKA_TRANSACTIONAL_PROJECTOR_ENABLED", "true"],
["HWLAB_KAFKA_PROJECTION_OUTBOX_RELAY_ENABLED", "true"],
["HWLAB_WORKBENCH_PROJECTION_REALTIME_ENABLED", "true"],
];
}
function cloudWebEnv(): Array<[string, string]> {
return [
["HWLAB_WORKBENCH_LIVE_KAFKA_SSE_ENABLED", "false"],
["HWLAB_WORKBENCH_KAFKA_REFRESH_REPLAY_ENABLED", "false"],
["HWLAB_WORKBENCH_PROJECTION_REALTIME_ENABLED", "true"],
];
}
function deploymentYaml(name: string, env: Array<[string, string]>) {
const envYaml = env.map(([envName, value]) => ` - name: ${envName}\n value: "${value}"`).join("\n");
return `apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: ${name}\n labels:\n app.kubernetes.io/name: ${name}\nspec:\n template:\n spec:\n containers:\n - name: ${name}\n env:\n${envYaml}\n`;
}