import { test } from 'node:test'; import assert from 'node:assert/strict'; import { mkdtemp, readFile, rm, stat, writeFile } from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; import { parseAllDocuments } from 'yaml'; import { buildFiles, CARRIED, clusterCaddyfile, clusterHttpCaddyfile, publicService, clusterEnvironments, clusterKubeconfig, publicIPv4, envFile, fanyuKubeconfig, fanyuProfile, imageBuildPod, renderPlatform, } from './cluster-poc.mjs'; import { bindingsOf, clusterCheckCommands, endpoint, parseEnvFile, privateCopy } from './cluster-poc-entry.mjs'; const IMAGE = `ccr-29eug8s3-vpc.cnc.bj.baidubce.com/dataflow-poc/wishforge-cluster-poc@sha256:${'c'.repeat(64)}`; const local = Object.fromEntries(CARRIED.map(key => [key, `value-of-${key.toLowerCase()}`])); const runner = { WF_RUNNER_TOKEN: 'runner-token', WF_TENANT_ID: 'demo', WF_ENVIRONMENT_ID: '6fabde42-b8e0-4398-80bb-be6ef78e5436', WF_RUNNER_PUBLIC_KEYS: 'key', WF_RUNNER_EXECUTE: 'true', }; const generated = { postgresPassword: 'p@ss/word', appPassword: 'app-password-123456', objectStorageUser: 'wishforge-user', objectStoragePassword: 'object-storage-password', }; test('the build sends only what Git tracks, never local agent settings or private state', () => { const listing = [ 'package.json', '.claude/settings.local.json', '.wishforge/local-access.json', '.env.local', 'apps/web/.env.production', 'scripts/dev.mjs', ].join('\0'); assert.deepEqual(buildFiles(`${listing}\0`), ['package.json', 'scripts/dev.mjs']); }); test('each container gets its own settings: no Mac path, and the executor no database or storage secret', () => { const env = clusterEnvironments({ local, runner, image: IMAGE, generated, origins: ['https://120.48.1.2'] }); for (const values of Object.values(env)) assert.ok(!Object.values(values).some(value => value.includes('/Users/')), 'no Mac paths'); assert.equal(env.platform.DATABASE_ADMIN_URL, 'postgresql://wishforge:p%40ss%2Fword@127.0.0.1:5432/wishforge'); assert.equal( env.platform.WF_LOCAL_ALLOWED_ORIGINS, 'http://127.0.0.1:3000,http://localhost:3000,http://127.0.0.1:13000,https://120.48.1.2', ); assert.equal(env.platform.WF_VAULT_KEY, local.WF_VAULT_KEY); for (const key of [ 'DATABASE_URL', 'DATABASE_ADMIN_URL', 'S3_SECRET_ACCESS_KEY', 'WF_DEV_AUTH_TOKEN', 'WF_LOCAL_API_TOKEN', 'WF_VAULT_KEY', ]) assert.equal(env.executor[key], undefined, `executor has no ${key}`); assert.equal(env.executor.WF_HOST_CHECK_POD_IMAGE, IMAGE); assert.deepEqual(Object.keys(env.runner).sort(), [ 'WF_CONTROL_PLANE_URL', 'WF_DEPLOYMENT_MODE', 'WF_ENVIRONMENT_ID', 'WF_POC_PROFILE_FILE', 'WF_RUNNER_ALLOW_LOCAL_HTTP', 'WF_RUNNER_EXECUTE', 'WF_RUNNER_PUBLIC_KEYS', 'WF_RUNNER_STATE_DIR', 'WF_RUNNER_TOKEN', 'WF_TENANT_ID', ]); assert.equal(env.runner.WF_RUNNER_PUBLIC_KEYS, '/etc/wishforge/config/runner-public.json'); assert.throws(() => clusterEnvironments({ local: {}, runner, image: IMAGE, generated }), /Missing settings/); assert.throws(() => clusterEnvironments({ local, runner, image: IMAGE, generated, origins: ['http://x'] }), /origin/); }); test('the console receives the control plane credential under the name its launcher allows', () => { const env = clusterEnvironments({ local: { ...local, WF_LOCAL_API_TOKEN: 'stale-mac-console-token' }, runner, image: IMAGE, generated, }); assert.equal(env.platform.WF_LOCAL_API_TOKEN, env.platform.WF_DEV_AUTH_TOKEN); assert.equal(env.platform.WF_WEB_SERVE_MODE, 'production'); assert.equal(env.executor.WF_LOCAL_API_TOKEN, undefined); assert.equal(env.runner.WF_LOCAL_API_TOKEN, undefined); }); test('settings files hold one value per line and read back the same', () => { const text = envFile({ A: 'plain', B: 'with space #', C: 'postgresql://u:p%40@127.0.0.1:5432/db' }); assert.deepEqual(parseEnvFile(text), { A: 'plain', B: 'with space #', C: 'postgresql://u:p%40@127.0.0.1:5432/db' }); assert.throws(() => envFile({ A: 'two\nlines' }), /single-line/); assert.throws(() => envFile({ A: "it's" }), /single-line/); }); test('the public entry takes a public IPv4 only, never a URL, private or documentation address', () => { assert.equal(publicIPv4('120.48.164.20'), '120.48.164.20'); for (const value of [ 'https://8.8.8.8', 'localhost', '127.0.0.1', '10.0.0.1', '192.168.1.2', '172.16.2.1', '100.64.1.2', '169.254.169.254', '203.0.113.1', '198.51.100.1', '224.0.0.1', '8.8.8.8\n', ]) assert.throws(() => publicIPv4(value), value); }); test('the public entry serves a Let’s Encrypt IP certificate and redirects to the standard HTTPS port', () => { const caddy = clusterCaddyfile('120.48.1.2'); assert.match(caddy, /https_port 8443/); assert.match(caddy, /redir @safe https:\/\/\{host\}\{uri\} 308/); assert.match(caddy, /^https:\/\/120\.48\.1\.2:8443 \{$/m); assert.match(caddy, /profile shortlived/); assert.match(caddy, /disable_tlsalpn_challenge/); assert.match(caddy, /acme-v02\.api\.letsencrypt\.org/); assert.match(clusterCaddyfile('120.48.1.2', { staging: true }), /acme-staging-v02/); assert.throws(() => clusterCaddyfile('192.168.1.2')); }); test('a plain-HTTP entry serves only port 80, proxies the console and asks for no certificate', async () => { const caddy = clusterHttpCaddyfile('120.48.1.2'); assert.match(caddy, /^http:\/\/120\.48\.1\.2:8080 \{$/m); assert.match(caddy, /reverse_proxy 127\.0\.0\.1:3000/); assert.match(caddy, /header_up X-Forwarded-Proto http$/m); assert.match(caddy, /auto_https off/); assert.doesNotMatch(caddy, /8443|tls|acme|https:\/\//); assert.throws(() => clusterHttpCaddyfile('192.168.1.2')); const template = await readFile(new URL('../infra/k8s/cluster-poc/public-service.yaml', import.meta.url), 'utf8'); const ports = text => parseAllDocuments(text) .map(document => document.toJS())[0] .spec.ports.map(port => port.port); assert.deepEqual(ports(publicService(template)), [80, 443]); assert.deepEqual(ports(publicService(template, { http: true })), [80]); const env = clusterEnvironments({ local, runner, image: IMAGE, generated, origins: ['http://120.48.1.2'] }); assert.equal(env.platform.WF_PUBLIC_URL, 'http://120.48.1.2'); assert.ok(env.platform.WF_LOCAL_ALLOWED_ORIGINS.split(',').includes('http://120.48.1.2')); }); test('the WishForge Pod is restricted everywhere and only Caddy listens beyond loopback', async () => { const template = await readFile(new URL('../infra/k8s/cluster-poc/platform.yaml', import.meta.url), 'utf8'); const settings = 'a'.repeat(32); assert.throws(() => renderPlatform(template, { image: 'wishforge:latest', settings }), /pinned/); assert.throws(() => renderPlatform(template, { image: IMAGE }), /settings digest/); const rendered = renderPlatform(template, { image: IMAGE, runnerDisabled: true, maintenance: false, settings }); const statefulSet = parseAllDocuments(rendered) .map(document => document.toJS()) .find(object => object.kind === 'StatefulSet'); const pod = statefulSet.spec.template.spec; assert.equal(statefulSet.spec.template.metadata.annotations['wishforge.io/settings'], settings); assert.equal(pod.automountServiceAccountToken, false); assert.deepEqual(pod.securityContext.seccompProfile, { type: 'RuntimeDefault' }); assert.equal(pod.securityContext.runAsNonRoot, true); assert.ok(!pod.volumes.some(volume => volume.hostPath)); for (const container of [...pod.initContainers, ...pod.containers]) { assert.equal(container.securityContext.allowPrivilegeEscalation, false, container.name); assert.equal(container.securityContext.readOnlyRootFilesystem, true, container.name); assert.deepEqual(container.securityContext.capabilities, { drop: ['ALL'] }, container.name); } const mounts = name => pod.containers.find(container => container.name === name).volumeMounts.map(mount => mount.name); assert.deepEqual( pod.containers.filter(container => container.ports).map(container => container.name), ['caddy'], ); for (const name of ['executor', 'runner']) assert.ok(mounts(name).includes('kube-token'), name); for (const name of ['postgres', 'temporal', 'minio', 'platform', 'caddy', 'backup', 'runner-fanyu']) assert.ok(!mounts(name).includes('kube-token'), name); // Fanyu's cluster credential reaches its builds (executor) and its Runner only. assert.deepEqual( pod.containers.filter(container => mounts(container.name).includes('fanyu-kube')).map(container => container.name), ['executor', 'runner-fanyu'], ); const fanyuRunner = pod.containers.find(container => container.name === 'runner-fanyu'); assert.equal( fanyuRunner.env.find(item => item.name === 'WF_ROLE_ENV_FILE').value, '/etc/wishforge/env/runner-fanyu.env', ); assert.equal( fanyuRunner.volumeMounts.find(mount => mount.mountPath === '/var/lib/wishforge/runner-state').subPath, 'runner-state-fanyu', ); for (const name of ['env-runner-fanyu', 'fanyu-kube']) assert.equal(pod.volumes.find(volume => volume.name === name).secret.optional, true, name); // The backup reads what it saves and writes only its own directory; no settings, keys or token. const backup = pod.containers.find(container => container.name === 'backup'); assert.deepEqual( backup.volumeMounts.filter(mount => mount.name === 'data' && !mount.readOnly).map(mount => mount.subPath), ['backups'], ); assert.deepEqual( backup.volumeMounts.filter(mount => mount.readOnly).map(mount => mount.subPath), ['git', 'minio', 'platform-state', 'poc-state', 'runner-state', 'runner-state-fanyu', 'temporal'], ); assert.deepEqual([...new Set(mounts('backup'))], ['data', 'backup-tmp']); assert.equal(backup.env.find(item => item.name === 'WF_IMAGE').value, IMAGE); assert.match(pod.initContainers[0].args[0], / backups; do/); assert.ok(!mounts('platform').includes('git-key') && mounts('executor').includes('git-key')); assert.ok(mounts('platform').includes('env-platform') && !mounts('executor').includes('env-platform')); const runnerEnv = pod.containers.find(container => container.name === 'runner').env; assert.deepEqual( runnerEnv.find(item => item.name === 'WF_RUNNER_DISABLED'), { name: 'WF_RUNNER_DISABLED', value: 'true' }, ); }); test('image builds run the POC’s Kaniko with only the registry credential and no token', () => { const pod = imageBuildPod( { namespace: 'droboticflow', registry: 'registry.example/ns', registrySecret: 'droboticflow-registry', kanikoImage: `gcr.io/kaniko-project/executor@sha256:${'d'.repeat(64)}`, registryMirrors: ['mirror.gcr.io'], }, { name: 'wishforge-image-x', tag: 't' }, ); assert.equal(pod.spec.automountServiceAccountToken, false); assert.deepEqual( pod.spec.volumes.map(volume => Object.keys(volume).filter(key => key !== 'name')[0]), ['emptyDir', 'secret'], ); assert.ok(pod.spec.containers[0].args.includes('--destination=registry.example/ns/wishforge-cluster-poc:t')); assert.ok(pod.spec.containers[0].args.some(arg => arg.startsWith('--build-arg=MINIO_COMMIT='))); }); test('the container entry reads service addresses and copies mounted files privately in one step', async t => { assert.deepEqual(endpoint('postgresql://u:p@127.0.0.1:5432/db'), { host: '127.0.0.1', port: 5432 }); assert.deepEqual(endpoint('127.0.0.1:7233'), { host: '127.0.0.1', port: 7233 }); assert.deepEqual(endpoint('http://127.0.0.1:9000'), { host: '127.0.0.1', port: 9000 }); const directory = await mkdtemp(path.join(os.tmpdir(), 'wishforge-entry-')); t.after(() => rm(directory, { recursive: true, force: true })); const source = path.join(directory, 'mounted.json'); await writeFile(source, '{"a":1}', { mode: 0o644 }); const target = path.join(directory, 'private/copy.json'); await Promise.all([privateCopy(source, target), privateCopy(source, target)]); assert.equal(await readFile(target, 'utf8'), '{"a":1}'); assert.equal((await stat(target)).mode & 0o777, 0o600); }); test('a project migrated from the Mac runs its POC checks with this image’s Node and script', () => { const node = '/usr/local/bin/node', script = '/opt/wishforge/scripts/poc-project-check.mjs'; const mac = [ ['/opt/homebrew/Cellar/node/26.7.0/bin/node', '/Users/me/wishforge-ide/scripts/poc-project-check.mjs', 'install'], ]; assert.deepEqual(clusterCheckCommands(mac, 'install', node, script), [[node, script, 'install']]); const here = [[node, script, 'verify']]; assert.equal(clusterCheckCommands(here, 'verify', node, script), here); for (const commands of [ [['npm', 'test']], [mac[0], ['npm', 'test']], [[mac[0][0], mac[0][1], 'verify']], [[...mac[0], '--fast']], [], undefined, ]) assert.equal(clusterCheckCommands(commands, 'install', node, script), commands); }); test('a domain the operator points at the load balancer becomes the console’s address, beside the IP', async () => { const { publicDomain } = await import('./cluster-poc.mjs'); assert.equal(publicDomain(' WishForge.Example.cc '), 'wishforge.example.cc'); for (const name of ['x', 'http://wishforge.example.cc', '120.48.1.2', '-bad.example.cc', 'a..b.cc', undefined]) assert.throws(() => publicDomain(name), /domain/); assert.match( clusterHttpCaddyfile('120.48.1.2', { domain: 'wishforge.example.cc' }), /^http:\/\/wishforge\.example\.cc:8080, http:\/\/120\.48\.1\.2:8080 \{$/m, ); const https = clusterCaddyfile('120.48.1.2', { domain: 'wishforge.example.cc' }); assert.match(https, /^http:\/\/wishforge\.example\.cc:8080, http:\/\/120\.48\.1\.2:8080 \{$/m); assert.match(https, /^https:\/\/wishforge\.example\.cc:8443, https:\/\/120\.48\.1\.2:8443 \{$/m); assert.match(https, /default_sni 120\.48\.1\.2/); const env = clusterEnvironments({ local, runner, image: IMAGE, generated, origins: ['http://wishforge.example.cc', 'http://120.48.1.2'], }); assert.equal(env.platform.WF_PUBLIC_URL, 'http://wishforge.example.cc'); assert.deepEqual(env.platform.WF_LOCAL_ALLOWED_ORIGINS.split(',').slice(-2), [ 'http://wishforge.example.cc', 'http://120.48.1.2', ]); }); test('the public address is the load balancer’s public IPv4, never its VPC address', async () => { const { publicAddressOf } = await import('./cluster-poc.mjs'); assert.equal( publicAddressOf({ status: { loadBalancer: { ingress: [{ ip: '192.168.0.74' }, { ip: '120.48.164.20' }] } } }), '120.48.164.20', ); assert.equal(publicAddressOf({ status: { loadBalancer: {} } }), undefined); assert.equal(publicAddressOf({ status: { loadBalancer: { ingress: [{ ip: '192.168.0.74' }] } } }), undefined); }); test('a container in maintenance stays up until Kubernetes stops it', async () => { const { spawn } = await import('node:child_process'); const child = spawn(process.execPath, [new URL('./cluster-poc-entry.mjs', import.meta.url).pathname, 'platform'], { env: { PATH: process.env.PATH, WF_MAINTENANCE: 'true' }, stdio: 'ignore', }); const exited = new Promise(resolve => child.once('exit', (code, signal) => resolve({ code, signal }))); await new Promise(resolve => setTimeout(resolve, 1500)); assert.equal(child.exitCode, null, 'still running'); child.kill('SIGTERM'); assert.deepEqual(await exited, { code: 0, signal: null }); }); test('the Claude CLI reaches the gateway by its Anthropic address with the requested models', async () => { const { claudeSettings } = await import('./cluster-poc.mjs'); const pending = claudeSettings(); assert.equal(pending.env.ANTHROPIC_BASE_URL, 'https://ai-api.d-robotics.cc'); assert.equal(pending.env.ANTHROPIC_API_KEY, undefined); assert.equal(pending.env.ANTHROPIC_MODEL, 'deepseek-pro@latest'); assert.equal(pending.env.ANTHROPIC_DEFAULT_HAIKU_MODEL, 'deepseek-flash@latest'); assert.equal(pending.env.CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC, '1'); assert.equal(claudeSettings('sk-fixture').env.ANTHROPIC_API_KEY, 'sk-fixture'); }); test('Fanyu’s Runner gets its own settings, keys, state and profile beside the POC’s', () => { const env = clusterEnvironments({ local, runner, runnerFanyu: { ...runner, WF_RUNNER_TOKEN: 'fanyu-token', WF_ENVIRONMENT_ID: '9fabde42-b8e0-4398-80bb-be6ef78e5436', }, image: IMAGE, generated, }); assert.equal(env.runner.WF_POC_PROFILE_FILE, '/var/lib/wishforge/poc-state/poc-profile.json'); assert.equal(env.runnerFanyu.WF_POC_PROFILE_FILE, '/var/lib/wishforge/poc-state/fanyu-profile.json'); assert.equal(env.runnerFanyu.WF_RUNNER_PUBLIC_KEYS, '/etc/wishforge/config/runner-fanyu-public.json'); assert.equal(env.runnerFanyu.WF_RUNNER_TOKEN, 'fanyu-token'); assert.equal(env.runnerFanyu.WF_RUNNER_STATE_DIR, env.runner.WF_RUNNER_STATE_DIR); assert.equal(clusterEnvironments({ local, runner, image: IMAGE, generated }).runnerFanyu, undefined); assert.throws( () => clusterEnvironments({ local, runner, runnerFanyu: {}, image: IMAGE, generated }), /Fanyu's Runner WF_RUNNER_TOKEN/, ); }); test('WishForge reaches Fanyu’s cluster with its own token only, through a profile pinned to Fanyu', () => { const kubeconfig = JSON.parse(fanyuKubeconfig({ certificateAuthorityData: 'Q0E=', token: 'eyJ.abc-def_1' })); assert.equal(kubeconfig.clusters[0].cluster.server, 'https://180.76.138.88:6443'); assert.deepEqual(kubeconfig.users, [{ name: 'wishforge', user: { token: 'eyJ.abc-def_1' } }]); assert.equal(kubeconfig.contexts[0].context.namespace, 'droboticflow-fanyu'); assert.throws(() => fanyuKubeconfig({ certificateAuthorityData: 'Q0E=', token: 'a b' }), /token/); const profile = fanyuProfile({ poc: { repository: 'git@gitea.d-robotics.cc:longtao.wu/droboticflow.git', kanikoImage: 'k@sha256:1', registryMirrors: ['mirror.gcr.io'], }, namespaceUid: 'n', deployments: { api: 'a' }, }); assert.equal(profile.branch, 'dev'); assert.equal(profile.kubeconfig, '/etc/wishforge/fanyu/kubeconfig'); assert.equal(profile.registry, 'ccr-29eug8s3-pub.cnc.bj.baidubce.com/fanyu'); assert.equal(profile.applicationUrl, 'http://120.48.180.80'); }); test('the containers read one binding or a list of them', () => { assert.deepEqual(bindingsOf('{"projectId":"a"}'), [{ projectId: 'a' }]); assert.deepEqual( bindingsOf('[{"projectId":"a"},{"projectId":"b"}]').map(binding => binding.projectId), ['a', 'b'], ); });