117 lines
3.7 KiB
TypeScript
117 lines
3.7 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { defineDeployableAppModule } from "../../src/apps/deployable-app-module.ts";
|
|
import { defineBoundedContextModule } from "../../src/contexts/bounded-context-module.ts";
|
|
import { defineFeatureModule } from "../../src/features/feature-module.ts";
|
|
import { planTopologyCapacity } from "../../src/platform/cardinality/topology-capacity.ts";
|
|
|
|
const MAX_FEATURES_PER_CONTEXT = 200;
|
|
const MAX_CONTEXTS_PER_APP = 20;
|
|
|
|
function fakeFeature(id: string) {
|
|
return defineFeatureModule({
|
|
id,
|
|
contracts: { featureId: id },
|
|
routes: {},
|
|
adapters: [],
|
|
runtimes: [],
|
|
messages: {},
|
|
contractContributions: [],
|
|
});
|
|
}
|
|
|
|
describe("topology cardinality", () => {
|
|
it("accepts direct children at the configured ceilings", () => {
|
|
const features = Array.from({ length: MAX_FEATURES_PER_CONTEXT }, (_, index) =>
|
|
fakeFeature(`feature-${index}`),
|
|
);
|
|
const contexts = Array.from({ length: MAX_CONTEXTS_PER_APP }, (_, index) =>
|
|
defineBoundedContextModule({
|
|
id: `context-${index}`,
|
|
features,
|
|
budget: { maxFeatures: MAX_FEATURES_PER_CONTEXT },
|
|
}),
|
|
);
|
|
|
|
expect(() =>
|
|
defineDeployableAppModule({
|
|
id: "scale-ceiling-app",
|
|
contexts,
|
|
budget: { maxContexts: MAX_CONTEXTS_PER_APP },
|
|
}),
|
|
).not.toThrow();
|
|
});
|
|
|
|
it("rejects a context above its feature budget", () => {
|
|
expect(() =>
|
|
defineBoundedContextModule({
|
|
id: "commerce",
|
|
features: Array.from({ length: 201 }, (_, index) => fakeFeature(`f${index}`)),
|
|
budget: { maxFeatures: MAX_FEATURES_PER_CONTEXT },
|
|
}),
|
|
).toThrow(/commerce.*201.*200/);
|
|
});
|
|
|
|
it("rejects duplicate direct child ids", () => {
|
|
const context = defineBoundedContextModule({
|
|
id: "commerce",
|
|
features: [],
|
|
budget: { maxFeatures: MAX_FEATURES_PER_CONTEXT },
|
|
});
|
|
|
|
expect(() =>
|
|
defineDeployableAppModule({
|
|
id: "customer",
|
|
contexts: [context, context],
|
|
budget: { maxContexts: MAX_CONTEXTS_PER_APP },
|
|
}),
|
|
).toThrow(/Duplicate.*commerce/);
|
|
});
|
|
});
|
|
|
|
describe("extreme-scale topology capacity", () => {
|
|
const cases = [
|
|
{ featureCount: 100, expectedContexts: 1, expectedApps: 1 },
|
|
{ featureCount: 1_000, expectedContexts: 5, expectedApps: 1 },
|
|
{ featureCount: 10_000, expectedContexts: 50, expectedApps: 3 },
|
|
{ featureCount: 100_000, expectedContexts: 500, expectedApps: 25 },
|
|
{ featureCount: 1_000_000, expectedContexts: 5_000, expectedApps: 250 },
|
|
] as const;
|
|
|
|
it.each(cases)(
|
|
"partitions $featureCount features without exceeding direct-child budgets",
|
|
({ featureCount, expectedContexts, expectedApps }) => {
|
|
const plan = planTopologyCapacity({
|
|
featureCount,
|
|
maxFeaturesPerContext: MAX_FEATURES_PER_CONTEXT,
|
|
maxContextsPerApp: MAX_CONTEXTS_PER_APP,
|
|
});
|
|
|
|
expect(plan.contextCount).toBe(expectedContexts);
|
|
expect(plan.appCount).toBe(expectedApps);
|
|
expect(plan.maxFeaturesInAnyContext).toBeLessThanOrEqual(MAX_FEATURES_PER_CONTEXT);
|
|
expect(plan.maxContextsInAnyApp).toBeLessThanOrEqual(MAX_CONTEXTS_PER_APP);
|
|
expect(plan.totalFeatureCapacity).toBeGreaterThanOrEqual(featureCount);
|
|
expect(plan.totalFeatureCapacity - featureCount).toBeLessThan(
|
|
MAX_FEATURES_PER_CONTEXT * MAX_CONTEXTS_PER_APP,
|
|
);
|
|
},
|
|
);
|
|
|
|
it("keeps a zero-feature product as one deployable app with no contexts", () => {
|
|
expect(
|
|
planTopologyCapacity({
|
|
featureCount: 0,
|
|
maxFeaturesPerContext: MAX_FEATURES_PER_CONTEXT,
|
|
maxContextsPerApp: MAX_CONTEXTS_PER_APP,
|
|
}),
|
|
).toMatchObject({
|
|
featureCount: 0,
|
|
contextCount: 0,
|
|
appCount: 1,
|
|
maxFeaturesInAnyContext: 0,
|
|
maxContextsInAnyApp: 0,
|
|
});
|
|
});
|
|
});
|