import { describe, expect, it } from 'vitest'; import { mkdtemp, rm, writeFile } from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; import { temporalTls, workerNamespaces } from '../src/temporal.js'; describe('Temporal connections', () => { it('keeps each tenant in its own namespace when a prefix is configured', () => { const shared = { WF_DISPATCH_TENANTS: 'alpha,beta,alpha', TEMPORAL_NAMESPACE: 'wishforge' }; expect(workerNamespaces(shared, 'coordinator')).toEqual(['wishforge']); const perTenant = { ...shared, TEMPORAL_NAMESPACE_PREFIX: 'wf-' }; expect(workerNamespaces(perTenant, 'coordinator')).toEqual(['wf-alpha', 'wf-beta']); expect(workerNamespaces(perTenant, 'deployer')).toEqual(['wf-alpha', 'wf-beta']); expect(workerNamespaces({ ...perTenant, WF_SANDBOX_TENANT_ID: 'beta' }, 'executor')).toEqual(['wf-beta']); // A sandbox started by a tenant's provisioner uses the namespace it was given. expect(workerNamespaces({ TEMPORAL_NAMESPACE: 'wf-beta', WF_SANDBOX_TENANT_ID: 'beta' }, 'verifier')).toEqual([ 'wf-beta', ]); expect(() => workerNamespaces({ TEMPORAL_NAMESPACE_PREFIX: 'wf-' }, 'coordinator')).toThrow('WF_DISPATCH_TENANTS'); }); it('refuses a shared namespace for several tenants, and plaintext, in production', async () => { const production = { WF_DEPLOYMENT_MODE: 'production', WF_DISPATCH_TENANTS: 'alpha,beta' }; expect(() => workerNamespaces(production, 'coordinator')).toThrow('per-tenant Temporal namespaces'); expect(workerNamespaces({ ...production, WF_DISPATCH_TENANTS: 'alpha' }, 'coordinator')).toEqual(['default']); await expect(temporalTls(production)).rejects.toThrow('mutual TLS'); expect(await temporalTls({ WF_DEPLOYMENT_MODE: 'development' })).toBeUndefined(); }); it('reads the client certificate, key and CA for mutual TLS', async () => { const directory = await mkdtemp(path.join(os.tmpdir(), 'wishforge-temporal-tls-')); try { for (const name of ['tls.crt', 'tls.key', 'ca.crt']) await writeFile(path.join(directory, name), name); const tls = await temporalTls({ WF_DEPLOYMENT_MODE: 'production', TEMPORAL_TLS_CERT: path.join(directory, 'tls.crt'), TEMPORAL_TLS_KEY: path.join(directory, 'tls.key'), TEMPORAL_TLS_CA: path.join(directory, 'ca.crt'), TEMPORAL_TLS_SERVER_NAME: 'temporal.internal', }); expect(String(tls?.clientCertPair.crt)).toBe('tls.crt'); expect(String(tls?.clientCertPair.key)).toBe('tls.key'); expect(String(tls?.serverRootCACertificate)).toBe('ca.crt'); expect(tls?.serverNameOverride).toBe('temporal.internal'); await expect(temporalTls({ TEMPORAL_TLS_CERT: path.join(directory, 'tls.crt') })).rejects.toThrow('together'); } finally { await rm(directory, { recursive: true, force: true }); } }); });