The best way to control Claude Code, Codex and more from any computer or phone
| 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 * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | |
| 9 | const { test } = Test.make({ providers: Turso.providers() }); |
| 10 | |
| 11 | // Invites and members need a team organization on a paid plan. The Starter |
| 12 | // plan rejects them with `BadRequest`: |
| 13 | // "organization <slug> can't invite users because plan starter doesn't support it" |
| 14 | // Set TURSO_TEST_TEAM=1 (and TURSO_TEST_MEMBER=<username>) on an entitled org. |
| 15 | const team = !!process.env.TURSO_TEST_TEAM; |
| 16 | const memberUsername = process.env.TURSO_TEST_MEMBER; |
| 17 | // Toggling overages changes billing; never run it by default. |
| 18 | const overages = !!process.env.TURSO_TEST_ORG_SETTINGS; |
| 19 | |
| 20 | const INVITE_EMAIL = "alchemy-turso-invite@example.com"; |
| 21 | |
| 22 | test.provider.skipIf(team)( |
| 23 | "invites on the Starter plan fail with a typed BadRequest", |
| 24 | (stack) => |
| 25 | Effect.gen(function* () { |
| 26 | yield* stack.destroy(); |
| 27 | const org = yield* organization; |
| 28 | const error = yield* turso |
| 29 | .inviteOrganizationMemberV2({ organizationSlug: org, email: INVITE_EMAIL, role: "member" }) |
| 30 | .pipe(Effect.flip); |
| 31 | expect(error._tag).toBe("BadRequest"); |
| 32 | yield* stack.destroy(); |
| 33 | }), |
| 34 | { tags: ["provider:turso", "provider:turso:organizationinvite", "live"], timeout: 60_000 }, |
| 35 | ); |
| 36 | |
| 37 | test.provider.skipIf(!team)( |
| 38 | "invite, change role, and withdraw an invite", |
| 39 | (stack) => |
| 40 | Effect.gen(function* () { |
| 41 | yield* stack.destroy(); |
| 42 | const org = yield* organization; |
| 43 | const pending = () => |
| 44 | turso |
| 45 | .listOrganizationInvitesV2({ organizationSlug: org }) |
| 46 | .pipe(Effect.map(({ invites }) => (invites ?? []).find((i) => i.email === INVITE_EMAIL))); |
| 47 | |
| 48 | yield* stack.deploy(Turso.OrganizationInvite("Invite", { email: INVITE_EMAIL })); |
| 49 | expect((yield* pending())?.role).toBe("member"); |
| 50 | |
| 51 | yield* stack.deploy( |
| 52 | Turso.OrganizationInvite("Invite", { email: INVITE_EMAIL, role: "viewer" }), |
| 53 | ); |
| 54 | expect((yield* pending())?.role).toBe("viewer"); |
| 55 | |
| 56 | yield* stack.destroy(); |
| 57 | expect(yield* pending()).toBeUndefined(); |
| 58 | }), |
| 59 | { tags: ["provider:turso", "provider:turso:organizationinvite", "live"], timeout: 60_000 }, |
| 60 | ); |
| 61 | |
| 62 | test.provider.skipIf(!team || !memberUsername)( |
| 63 | "add a member, change their role, and remove them", |
| 64 | (stack) => |
| 65 | Effect.gen(function* () { |
| 66 | yield* stack.destroy(); |
| 67 | const org = yield* organization; |
| 68 | const username = memberUsername!; |
| 69 | const role = () => |
| 70 | turso.getOrganizationMember({ organizationSlug: org, username }).pipe( |
| 71 | Effect.map(({ member }) => member?.role), |
| 72 | Effect.catchTag("NotFound", () => Effect.succeed(undefined)), |
| 73 | ); |
| 74 | |
| 75 | yield* stack.deploy(Turso.OrganizationMember("Member", { username, role: "viewer" })); |
| 76 | expect(yield* role()).toBe("viewer"); |
| 77 | yield* stack.deploy(Turso.OrganizationMember("Member", { username, role: "member" })); |
| 78 | expect(yield* role()).toBe("member"); |
| 79 | |
| 80 | yield* stack.destroy(); |
| 81 | expect(yield* role()).toBeUndefined(); |
| 82 | }), |
| 83 | { tags: ["provider:turso", "provider:turso:organizationmember", "live"], timeout: 60_000 }, |
| 84 | ); |
| 85 | |
| 86 | test.provider( |
| 87 | "restoreEnabled is applied and restored on destroy", |
| 88 | (stack) => |
| 89 | Effect.gen(function* () { |
| 90 | yield* stack.destroy(); |
| 91 | const org = yield* organization; |
| 92 | const observe = turso |
| 93 | .getOrganization({ organizationSlug: org }) |
| 94 | .pipe(Effect.map(({ organization }) => organization?.restore_enabled ?? false)); |
| 95 | const original = yield* observe; |
| 96 | |
| 97 | const settings = yield* stack.deploy( |
| 98 | Turso.OrganizationSettings("Org", { restoreEnabled: !original }), |
| 99 | ); |
| 100 | expect(settings.original.restoreEnabled).toBe(original); |
| 101 | expect(settings.restoreEnabled).toBe(!original); |
| 102 | // Organization reads can briefly lag a write; poll until it shows. |
| 103 | const settle = (expected: boolean) => |
| 104 | observe.pipe( |
| 105 | Effect.repeat({ |
| 106 | schedule: Schedule.spaced("1 second"), |
| 107 | until: (value) => value === expected, |
| 108 | times: 15, |
| 109 | }), |
| 110 | ); |
| 111 | expect(yield* settle(!original)).toBe(!original); |
| 112 | |
| 113 | yield* stack.destroy(); |
| 114 | expect(yield* settle(original)).toBe(original); |
| 115 | }), |
| 116 | // An account-wide setting: run with no other Turso test in flight. |
| 117 | { |
| 118 | tags: ["provider:turso", "provider:turso:organizationsettings", "live"], |
| 119 | timeout: 90_000, |
| 120 | exclusive: true, |
| 121 | }, |
| 122 | ); |
| 123 | |
| 124 | test.provider.skipIf(!overages)( |
| 125 | "organization settings are applied and restored on destroy", |
| 126 | (stack) => |
| 127 | Effect.gen(function* () { |
| 128 | yield* stack.destroy(); |
| 129 | const org = yield* organization; |
| 130 | const observe = turso |
| 131 | .getOrganization({ organizationSlug: org }) |
| 132 | .pipe(Effect.map(({ organization }) => organization?.overages ?? false)); |
| 133 | const original = yield* observe; |
| 134 | |
| 135 | const settings = yield* stack.deploy( |
| 136 | Turso.OrganizationSettings("Org", { overages: !original }), |
| 137 | ); |
| 138 | expect(settings.original.overages).toBe(original); |
| 139 | expect(yield* observe).toBe(!original); |
| 140 | |
| 141 | yield* stack.destroy(); |
| 142 | expect(yield* observe).toBe(original); |
| 143 | }), |
| 144 | { tags: ["provider:turso", "provider:turso:organizationsettings", "live"], timeout: 60_000 }, |
| 145 | ); |