@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 199 lines 6.65 KB
Raw
1 import { describe, expect, it } from "@alchemy.run/test";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import { createPhysicalName } from "@/PhysicalName.ts";
5 import { ResourceContext } from "@/ResourceContext.ts";
6 import { Stack, type StackSpec } from "@/Stack.ts";
7 import { Stage } from "@/Stage.ts";
8 import { resourceContext } from "./Util/ResourceContext.ts";
9
10 type StackShape = Omit<StackSpec, "output">;
11
12 const stack: StackShape = {
13 name: "my-stack",
14 stage: "dev",
15 resources: {},
16 bindings: {},
17 actions: {},
18 };
19
20 const environment = (spec: StackShape = stack, instanceId = "0123456789abcdef0123456789abcdef") =>
21 Layer.mergeAll(
22 Layer.succeed(Stack, spec),
23 Layer.succeed(Stage, spec.stage),
24 Layer.succeed(ResourceContext, resourceContext(instanceId)),
25 );
26
27 const provide = <A, E>(
28 effect: Effect.Effect<A, E, Stack | Stage | ResourceContext>,
29 instanceId?: string,
30 ) => effect.pipe(Effect.provide(environment(stack, instanceId)));
31
32 describe("createPhysicalName", { tags: ["unit", "local"] }, () => {
33 it.effect("keeps short names untruncated", () =>
34 provide(
35 Effect.gen(function* () {
36 const name = yield* createPhysicalName({ id: "api", maxLength: 64 });
37 expect(name.length).toBeLessThanOrEqual(64);
38 expect(name.startsWith("my-stack-api-dev-")).toBe(true);
39 }),
40 ),
41 );
42
43 it.effect("is deterministic for the same inputs", () =>
44 provide(
45 Effect.gen(function* () {
46 const longId = "a".repeat(80);
47 const first = yield* createPhysicalName({ id: longId, maxLength: 64 });
48 const second = yield* createPhysicalName({ id: longId, maxLength: 64 });
49 expect(first).toBe(second);
50 expect(first.length).toBe(64);
51 }),
52 ),
53 );
54
55 it.effect(
56 "preserves suffix uniqueness when a long id truncates away the suffix (task-role vs execution-role)",
57 () =>
58 provide(
59 Effect.gen(function* () {
60 // Pathological logical id: long enough that `${stack}-${id}-…` alone
61 // exceeds IAM's 64-char role-name limit, so the distinguishing
62 // `-task-role` / `-execution-role` tail falls entirely inside the
63 // truncated region. Both names share the same instance ID (same
64 // resource), which is exactly the collision scenario.
65 const longId = "my-very-long-container-platform-service-logical-id";
66 const taskRole = yield* createPhysicalName({
67 id: `${longId}-task-role`,
68 maxLength: 64,
69 });
70 const executionRole = yield* createPhysicalName({
71 id: `${longId}-execution-role`,
72 maxLength: 64,
73 });
74 expect(taskRole.length).toBe(64);
75 expect(executionRole.length).toBe(64);
76 expect(taskRole).not.toBe(executionRole);
77 }),
78 ),
79 );
80
81 it.effect("distinguishes every colliding suffix pair used by role helpers", () =>
82 provide(
83 Effect.gen(function* () {
84 const longId = "z".repeat(100);
85 const suffixes = [
86 "task-role",
87 "execution-role",
88 "job-role",
89 "instance-role",
90 "access-role",
91 ];
92 const names = yield* Effect.forEach(suffixes, (suffix) =>
93 createPhysicalName({ id: `${longId}-${suffix}`, maxLength: 64 }),
94 );
95 expect(new Set(names).size).toBe(suffixes.length);
96 for (const name of names) {
97 expect(name.length).toBe(64);
98 }
99 }),
100 ),
101 );
102
103 it.effect("different instance ids still produce different names", () =>
104 Effect.gen(function* () {
105 const longId = "b".repeat(80);
106 const first = yield* provide(
107 createPhysicalName({ id: longId, maxLength: 64 }),
108 "0123456789abcdef0123456789abcdef",
109 );
110 const second = yield* provide(
111 createPhysicalName({ id: longId, maxLength: 64 }),
112 "fedcba9876543210fedcba9876543210",
113 );
114 expect(first).not.toBe(second);
115 }),
116 );
117
118 it.effect("keeps the full hash under tight limits (DAX-style maxLength 20)", () =>
119 provide(
120 Effect.gen(function* () {
121 // maxLength 20 with the default 16-char instance suffix leaves no
122 // room for prefix + hash + suffix; the hash must survive in full
123 // (the suffix shrinks) or same-resource names collide again.
124 const longId = "c".repeat(60);
125 const names = yield* Effect.forEach(["alpha", "beta"], (suffix) =>
126 createPhysicalName({
127 id: `${longId}-${suffix}`,
128 maxLength: 20,
129 lowercase: true,
130 }),
131 );
132 expect(names[0]!.length).toBe(20);
133 expect(names[1]!.length).toBe(20);
134 expect(names[0]).not.toBe(names[1]);
135 }),
136 ),
137 );
138
139 it.effect("keeps uniqueness with a shortened suffixLength (Canary-style 21/8)", () =>
140 provide(
141 Effect.gen(function* () {
142 const longId = "d".repeat(60);
143 const names = yield* Effect.forEach(["alpha", "beta"], (suffix) =>
144 createPhysicalName({
145 id: `${longId}-${suffix}`,
146 maxLength: 21,
147 suffixLength: 8,
148 lowercase: true,
149 }),
150 );
151 expect(names[0]!.length).toBe(21);
152 expect(names[0]).not.toBe(names[1]);
153 }),
154 ),
155 );
156
157 it.effect("prepends a safe prefix when the name collides with a forbidden prefix", () =>
158 Effect.gen(function* () {
159 // A stack named `AWS-…` (e.g. file-namespaced test stacks) collides
160 // with service-reserved prefixes like S3 Tables' / ResourceGroups' `aws`.
161 const awsStack: StackShape = { ...stack, name: "AWS-S3Tables-Test" };
162 const name = yield* createPhysicalName({
163 id: "bucket",
164 maxLength: 63,
165 lowercase: true,
166 forbiddenPrefixes: ["xn--", "sthree-", "amzn-s3-demo-", "aws"],
167 }).pipe(Effect.provide(environment(awsStack)));
168 expect(name.startsWith("x-aws-s3tables-test-")).toBe(true);
169 expect(name.length).toBeLessThanOrEqual(63);
170 }),
171 );
172
173 it.effect("non-colliding names are unaffected by forbiddenPrefixes", () =>
174 provide(
175 Effect.gen(function* () {
176 const withOption = yield* createPhysicalName({
177 id: "api",
178 forbiddenPrefixes: ["aws"],
179 });
180 const withoutOption = yield* createPhysicalName({ id: "api" });
181 expect(withOption).toBe(withoutOption);
182 }),
183 ),
184 );
185
186 it.effect("lowercase truncated names stay DNS-safe", () =>
187 provide(
188 Effect.gen(function* () {
189 const name = yield* createPhysicalName({
190 id: `MyMixedCase_${"x".repeat(80)}`,
191 maxLength: 63,
192 lowercase: true,
193 });
194 expect(name.length).toBe(63);
195 expect(name).toMatch(/^[a-z0-9-]+$/);
196 }),
197 ),
198 );
199 });