- Add federation.config.ts with UUID v4 validation for DEFAULT_WORKSPACE_ID - Validate at module initialization (fail fast if misconfigured) - Replace hardcoded "default" fallback with proper validation - Add 18 tests covering valid UUIDs, invalid formats, and missing values - Clear error messages with expected UUID format Refs #338 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
165 lines
5.8 KiB
TypeScript
165 lines
5.8 KiB
TypeScript
/**
|
|
* Federation Configuration Tests
|
|
*
|
|
* Issue #338: Tests for DEFAULT_WORKSPACE_ID validation
|
|
*/
|
|
|
|
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
import {
|
|
isValidUuidV4,
|
|
getDefaultWorkspaceId,
|
|
validateFederationConfig,
|
|
} from "./federation.config";
|
|
|
|
describe("federation.config", () => {
|
|
const originalEnv = process.env.DEFAULT_WORKSPACE_ID;
|
|
|
|
afterEach(() => {
|
|
// Restore original environment
|
|
if (originalEnv === undefined) {
|
|
delete process.env.DEFAULT_WORKSPACE_ID;
|
|
} else {
|
|
process.env.DEFAULT_WORKSPACE_ID = originalEnv;
|
|
}
|
|
});
|
|
|
|
describe("isValidUuidV4", () => {
|
|
it("should return true for valid UUID v4", () => {
|
|
const validUuids = [
|
|
"123e4567-e89b-42d3-a456-426614174000",
|
|
"550e8400-e29b-41d4-a716-446655440000",
|
|
"6ba7b810-9dad-41d1-80b4-00c04fd430c8",
|
|
"f47ac10b-58cc-4372-a567-0e02b2c3d479",
|
|
];
|
|
|
|
for (const uuid of validUuids) {
|
|
expect(isValidUuidV4(uuid)).toBe(true);
|
|
}
|
|
});
|
|
|
|
it("should return true for uppercase UUID v4", () => {
|
|
expect(isValidUuidV4("123E4567-E89B-42D3-A456-426614174000")).toBe(true);
|
|
});
|
|
|
|
it("should return false for non-v4 UUID (wrong version digit)", () => {
|
|
// UUID v1 (version digit is 1)
|
|
expect(isValidUuidV4("123e4567-e89b-12d3-a456-426614174000")).toBe(false);
|
|
// UUID v3 (version digit is 3)
|
|
expect(isValidUuidV4("123e4567-e89b-32d3-a456-426614174000")).toBe(false);
|
|
// UUID v5 (version digit is 5)
|
|
expect(isValidUuidV4("123e4567-e89b-52d3-a456-426614174000")).toBe(false);
|
|
});
|
|
|
|
it("should return false for invalid variant digit", () => {
|
|
// Variant digit should be 8, 9, a, or b
|
|
expect(isValidUuidV4("123e4567-e89b-42d3-0456-426614174000")).toBe(false);
|
|
expect(isValidUuidV4("123e4567-e89b-42d3-7456-426614174000")).toBe(false);
|
|
expect(isValidUuidV4("123e4567-e89b-42d3-c456-426614174000")).toBe(false);
|
|
});
|
|
|
|
it("should return false for non-UUID strings", () => {
|
|
expect(isValidUuidV4("")).toBe(false);
|
|
expect(isValidUuidV4("default")).toBe(false);
|
|
expect(isValidUuidV4("not-a-uuid")).toBe(false);
|
|
expect(isValidUuidV4("123e4567-e89b-12d3-a456")).toBe(false);
|
|
expect(isValidUuidV4("123e4567e89b12d3a456426614174000")).toBe(false);
|
|
});
|
|
|
|
it("should return false for UUID with wrong length", () => {
|
|
expect(isValidUuidV4("123e4567-e89b-42d3-a456-4266141740001")).toBe(false);
|
|
expect(isValidUuidV4("123e4567-e89b-42d3-a456-42661417400")).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("getDefaultWorkspaceId", () => {
|
|
it("should return valid UUID when DEFAULT_WORKSPACE_ID is set correctly", () => {
|
|
const validUuid = "123e4567-e89b-42d3-a456-426614174000";
|
|
process.env.DEFAULT_WORKSPACE_ID = validUuid;
|
|
|
|
expect(getDefaultWorkspaceId()).toBe(validUuid);
|
|
});
|
|
|
|
it("should trim whitespace from UUID", () => {
|
|
const validUuid = "123e4567-e89b-42d3-a456-426614174000";
|
|
process.env.DEFAULT_WORKSPACE_ID = ` ${validUuid} `;
|
|
|
|
expect(getDefaultWorkspaceId()).toBe(validUuid);
|
|
});
|
|
|
|
it("should throw error when DEFAULT_WORKSPACE_ID is not set", () => {
|
|
delete process.env.DEFAULT_WORKSPACE_ID;
|
|
|
|
expect(() => getDefaultWorkspaceId()).toThrow(
|
|
"DEFAULT_WORKSPACE_ID environment variable is required for federation but is not set"
|
|
);
|
|
});
|
|
|
|
it("should throw error when DEFAULT_WORKSPACE_ID is empty string", () => {
|
|
process.env.DEFAULT_WORKSPACE_ID = "";
|
|
|
|
expect(() => getDefaultWorkspaceId()).toThrow(
|
|
"DEFAULT_WORKSPACE_ID environment variable is required for federation but is not set"
|
|
);
|
|
});
|
|
|
|
it("should throw error when DEFAULT_WORKSPACE_ID is only whitespace", () => {
|
|
process.env.DEFAULT_WORKSPACE_ID = " ";
|
|
|
|
expect(() => getDefaultWorkspaceId()).toThrow(
|
|
"DEFAULT_WORKSPACE_ID environment variable is required for federation but is not set"
|
|
);
|
|
});
|
|
|
|
it("should throw error when DEFAULT_WORKSPACE_ID is 'default' (not a valid UUID)", () => {
|
|
process.env.DEFAULT_WORKSPACE_ID = "default";
|
|
|
|
expect(() => getDefaultWorkspaceId()).toThrow("DEFAULT_WORKSPACE_ID must be a valid UUID v4");
|
|
expect(() => getDefaultWorkspaceId()).toThrow('Current value "default" is not a valid UUID');
|
|
});
|
|
|
|
it("should throw error when DEFAULT_WORKSPACE_ID is invalid UUID format", () => {
|
|
process.env.DEFAULT_WORKSPACE_ID = "not-a-valid-uuid";
|
|
|
|
expect(() => getDefaultWorkspaceId()).toThrow("DEFAULT_WORKSPACE_ID must be a valid UUID v4");
|
|
});
|
|
|
|
it("should throw error for UUID v1 (wrong version)", () => {
|
|
process.env.DEFAULT_WORKSPACE_ID = "123e4567-e89b-12d3-a456-426614174000";
|
|
|
|
expect(() => getDefaultWorkspaceId()).toThrow("DEFAULT_WORKSPACE_ID must be a valid UUID v4");
|
|
});
|
|
|
|
it("should include helpful error message with expected format", () => {
|
|
process.env.DEFAULT_WORKSPACE_ID = "invalid";
|
|
|
|
expect(() => getDefaultWorkspaceId()).toThrow(
|
|
"Expected format: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("validateFederationConfig", () => {
|
|
it("should not throw when DEFAULT_WORKSPACE_ID is valid", () => {
|
|
process.env.DEFAULT_WORKSPACE_ID = "123e4567-e89b-42d3-a456-426614174000";
|
|
|
|
expect(() => validateFederationConfig()).not.toThrow();
|
|
});
|
|
|
|
it("should throw when DEFAULT_WORKSPACE_ID is missing", () => {
|
|
delete process.env.DEFAULT_WORKSPACE_ID;
|
|
|
|
expect(() => validateFederationConfig()).toThrow(
|
|
"DEFAULT_WORKSPACE_ID environment variable is required for federation"
|
|
);
|
|
});
|
|
|
|
it("should throw when DEFAULT_WORKSPACE_ID is invalid", () => {
|
|
process.env.DEFAULT_WORKSPACE_ID = "invalid-uuid";
|
|
|
|
expect(() => validateFederationConfig()).toThrow(
|
|
"DEFAULT_WORKSPACE_ID must be a valid UUID v4"
|
|
);
|
|
});
|
|
});
|
|
});
|