@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 182 lines 6.13 KB
Raw
1 import { expect } from "@alchemy.run/test";
2 import * as Test from "@alchemy.run/test/Alchemy";
3 import * as Turso from "@alchemy.run/turso";
4 import { organization } from "@alchemy.run/turso/Credentials";
5 import * as turso from "@distilled.cloud/turso/turso";
6 import { adopt, OwnedBySomeoneElse } from "alchemy/AdoptPolicy";
7 import { Stage } from "alchemy/Stage";
8 import * as Effect from "effect/Effect";
9 import * as Result from "effect/Result";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: Turso.providers() });
13
14 const expectGroupGone = (name: string) =>
15 Effect.gen(function* () {
16 const org = yield* organization;
17 const found = yield* turso.getGroup({ organizationSlug: org, groupName: name }).pipe(
18 Effect.map(() => true),
19 Effect.catchTag("NotFound", () => Effect.succeed(false)),
20 Effect.repeat({
21 schedule: Schedule.spaced("1 second"),
22 until: (exists) => !exists,
23 times: 10,
24 }),
25 );
26 expect(found).toBe(false);
27 });
28
29 test.provider(
30 "create, update delete protection, and delete a group",
31 (stack) =>
32 Effect.gen(function* () {
33 yield* stack.destroy();
34 const org = yield* organization;
35
36 const group = yield* stack.deploy(
37 Effect.gen(function* () {
38 return yield* Turso.Group("Group", { location: "aws-us-east-1" });
39 }),
40 );
41 expect(group.location).toBe("aws-us-east-1");
42 expect(group.locations).toEqual(["aws-us-east-1"]);
43 expect(group.deleteProtection).toBe(false);
44 expect(group.organization).toBe(org);
45
46 const observed = yield* turso.getGroup({ organizationSlug: org, groupName: group.name });
47 expect(observed.group?.uuid).toBe(group.uuid);
48
49 const protectedGroup = yield* stack.deploy(
50 Effect.gen(function* () {
51 return yield* Turso.Group("Group", {
52 location: "aws-us-east-1",
53 deleteProtection: true,
54 });
55 }),
56 );
57 expect(protectedGroup.uuid).toBe(group.uuid);
58 expect(protectedGroup.deleteProtection).toBe(true);
59 const config = yield* turso.getGroupConfiguration({
60 organizationSlug: org,
61 groupName: group.name,
62 });
63 expect(config.delete_protection).toBe(true);
64
65 // Turn protection back off so the group can be destroyed.
66 const unprotected = yield* stack.deploy(
67 Effect.gen(function* () {
68 return yield* Turso.Group("Group", { location: "aws-us-east-1" });
69 }),
70 );
71 expect(unprotected.deleteProtection).toBe(false);
72
73 yield* stack.destroy();
74 yield* expectGroupGone(group.name);
75 }),
76 { tags: ["provider:turso", "provider:turso:group", "live"], timeout: 120_000 },
77 );
78
79 test.provider(
80 "changing the location replaces the group",
81 (stack) =>
82 Effect.gen(function* () {
83 yield* stack.destroy();
84
85 const east = yield* stack.deploy(
86 Effect.gen(function* () {
87 return yield* Turso.Group("Moving", { location: "aws-us-east-1" });
88 }),
89 );
90 const west = yield* stack.deploy(
91 Effect.gen(function* () {
92 return yield* Turso.Group("Moving", { location: "aws-us-west-2" });
93 }),
94 );
95 expect(west.location).toBe("aws-us-west-2");
96 expect(west.uuid).not.toBe(east.uuid);
97 expect(west.name).not.toBe(east.name);
98 yield* expectGroupGone(east.name);
99
100 yield* stack.destroy();
101 yield* expectGroupGone(west.name);
102 }),
103 { tags: ["provider:turso", "provider:turso:group", "live"], timeout: 120_000 },
104 );
105
106 test.provider(
107 "adding a replica location on an AWS group fails with a typed error",
108 (stack) =>
109 Effect.gen(function* () {
110 yield* stack.destroy();
111 const result = yield* stack
112 .deploy(
113 Effect.gen(function* () {
114 return yield* Turso.Group("Replicated", {
115 location: "aws-us-east-1",
116 replicas: ["aws-us-west-2"],
117 });
118 }),
119 )
120 .pipe(Effect.flip);
121 expect(String(result)).toContain("replication is not supported");
122 yield* stack.destroy();
123 }),
124 { tags: ["provider:turso", "provider:turso:group", "live"], timeout: 120_000 },
125 );
126
127 const pinned = (suffix: string) =>
128 Effect.gen(function* () {
129 const stage = yield* Stage;
130 return `alchemy-${stage}-${suffix}`.toLowerCase().replace(/[^a-z0-9-]/g, "-");
131 });
132
133 test.provider(
134 "changing the location of a group with a pinned name deletes the old one first",
135 (stack) =>
136 Effect.gen(function* () {
137 yield* stack.destroy();
138 const name = yield* pinned("pinned-group");
139
140 const east = yield* stack.deploy(Turso.Group("Pinned", { name, location: "aws-us-east-1" }));
141 const west = yield* stack.deploy(Turso.Group("Pinned", { name, location: "aws-us-west-2" }));
142 expect(west.name).toBe(name);
143 expect(west.location).toBe("aws-us-west-2");
144 expect(west.uuid).not.toBe(east.uuid);
145
146 yield* stack.destroy();
147 yield* expectGroupGone(name);
148 }),
149 { tags: ["provider:turso", "provider:turso:group", "live"], timeout: 120_000 },
150 );
151
152 test.provider(
153 "a group with a pinned name created elsewhere is refused unless adopted",
154 (stack) =>
155 Effect.gen(function* () {
156 yield* stack.destroy();
157 const org = yield* organization;
158 const name = yield* pinned("foreign-group");
159 yield* turso
160 .deleteGroup({ organizationSlug: org, groupName: name })
161 .pipe(Effect.catchTag("NotFound", () => Effect.void));
162 const foreign = yield* turso.createGroup({
163 organizationSlug: org,
164 name,
165 location: "aws-us-east-1",
166 });
167
168 const app = (allow: boolean) =>
169 Turso.Group("Foreign", { name, location: "aws-us-east-1" }).pipe(adopt(allow));
170
171 const refused = yield* stack.deploy(app(false)).pipe(Effect.result);
172 expect(Result.isFailure(refused)).toBe(true);
173 if (Result.isFailure(refused)) expect(refused.failure).toBeInstanceOf(OwnedBySomeoneElse);
174
175 const adopted = yield* stack.deploy(app(true));
176 expect(adopted.uuid).toBe(foreign.group?.uuid);
177
178 yield* stack.destroy();
179 yield* expectGroupGone(name);
180 }),
181 { tags: ["provider:turso", "provider:turso:group", "live"], timeout: 120_000 },
182 );