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
| 1 | import { expect, it } from "@alchemy.run/test"; |
| 2 | import * as Context from "effect/Context"; |
| 3 | import * as Deferred from "effect/Deferred"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { onEvent } from "@/Events.ts"; |
| 6 | import * as Nuke from "@/Nuke.ts"; |
| 7 | import type { ProviderService } from "@/Provider.ts"; |
| 8 | |
| 9 | it.effect( |
| 10 | "reports each deletion before a slow coordinated pass finishes", |
| 11 | () => |
| 12 | Effect.gen(function* () { |
| 13 | const fastDeleted = yield* Deferred.make<void>(); |
| 14 | const events: string[] = []; |
| 15 | const provider: ProviderService = { |
| 16 | list: () => Effect.succeed([]), |
| 17 | reconcile: () => Effect.succeed({}), |
| 18 | delete: ({ id }) => (id === "slow" ? Deferred.await(fastDeleted) : Effect.void), |
| 19 | }; |
| 20 | const result = yield* Nuke.destroy({ |
| 21 | targets: ["fast", "slow"].map((displayName) => ({ |
| 22 | key: "t101", |
| 23 | providerId: "Test.Resource", |
| 24 | displayName, |
| 25 | attributes: {}, |
| 26 | provider, |
| 27 | })), |
| 28 | context: Context.empty(), |
| 29 | strategy: { type: "coordinated" }, |
| 30 | }).pipe( |
| 31 | onEvent((event) => |
| 32 | Effect.gen(function* () { |
| 33 | if (event.type === "nuke.pass.started") events.push(`pass ${event.pass}`); |
| 34 | if (event.type !== "nuke.resource.deleted") return; |
| 35 | events.push(event.resource); |
| 36 | if (event.resource === "fast") yield* Deferred.succeed(fastDeleted, undefined); |
| 37 | }), |
| 38 | ), |
| 39 | Effect.timeout("2 seconds"), |
| 40 | ); |
| 41 | expect(result.deleted).toHaveLength(2); |
| 42 | expect(events).toEqual(["pass 1", "fast", "slow"]); |
| 43 | }), |
| 44 | { tags: ["unit", "local"] }, |
| 45 | ); |
| 46 | |
| 47 | it.effect( |
| 48 | "reports scan totals and settles failed providers", |
| 49 | () => |
| 50 | Effect.gen(function* () { |
| 51 | const events: string[] = []; |
| 52 | const provider: ProviderService = { |
| 53 | list: () => Effect.fail(new Error("unavailable")), |
| 54 | reconcile: () => Effect.succeed({}), |
| 55 | delete: () => Effect.void, |
| 56 | }; |
| 57 | const tag = Context.Service<ProviderService>("Test.Resource"); |
| 58 | const result = yield* Nuke.list({ |
| 59 | context: Context.make(tag, provider), |
| 60 | mode: "live", |
| 61 | }).pipe( |
| 62 | onEvent((event) => |
| 63 | Effect.sync(() => { |
| 64 | if (event.type === "nuke.scan.started") events.push(`total ${event.total}`); |
| 65 | if (event.type === "nuke.scan.provider.started") events.push(`start ${event.provider}`); |
| 66 | if (event.type === "nuke.scan.provider.completed") |
| 67 | events.push(`done ${event.provider} ${event.resources}`); |
| 68 | }), |
| 69 | ), |
| 70 | ); |
| 71 | expect(result.failures).toHaveLength(1); |
| 72 | expect(events).toEqual(["total 1", "start Test.Resource", "done Test.Resource 0"]); |
| 73 | }), |
| 74 | { tags: ["unit", "local"] }, |
| 75 | ); |
| 76 | |
| 77 | it.effect( |
| 78 | "preserves structured provider errors in scan and deletion reports", |
| 79 | () => |
| 80 | Effect.gen(function* () { |
| 81 | const error = { |
| 82 | _tag: "AccessDeniedException", |
| 83 | message: "This service is unavailable for this account", |
| 84 | details: { code: 403 }, |
| 85 | }; |
| 86 | const provider: ProviderService = { |
| 87 | list: () => Effect.fail(error), |
| 88 | reconcile: () => Effect.succeed({}), |
| 89 | delete: () => Effect.fail(error), |
| 90 | }; |
| 91 | const context = Context.make(Context.Service<ProviderService>("Test.Resource"), provider); |
| 92 | const scan = yield* Nuke.list({ context, mode: "live" }); |
| 93 | const messages = scan.failures.map((failure) => failure.message); |
| 94 | for (const strategy of [ |
| 95 | { type: "coordinated" }, |
| 96 | { type: "independent", retries: 0 }, |
| 97 | ] as const) { |
| 98 | const result = yield* Nuke.destroy({ |
| 99 | targets: [ |
| 100 | { |
| 101 | key: "t1", |
| 102 | providerId: "Test.Resource", |
| 103 | displayName: "example", |
| 104 | attributes: {}, |
| 105 | provider, |
| 106 | }, |
| 107 | ], |
| 108 | context, |
| 109 | strategy, |
| 110 | }).pipe( |
| 111 | onEvent((event) => |
| 112 | Effect.sync(() => { |
| 113 | if (event.type === "nuke.resource.failed") messages.push(event.message); |
| 114 | }), |
| 115 | ), |
| 116 | ); |
| 117 | expect(result.failed).toHaveLength(1); |
| 118 | messages.push(result.failed[0]!.failure.message); |
| 119 | } |
| 120 | expect(messages).toHaveLength(5); |
| 121 | for (const message of messages) { |
| 122 | expect(message).toContain("AccessDeniedException"); |
| 123 | expect(message).toContain(error.message); |
| 124 | expect(message).toContain("403"); |
| 125 | expect(message).not.toContain("[object Object]"); |
| 126 | expect(message).not.toContain("no progress"); |
| 127 | } |
| 128 | }), |
| 129 | { tags: ["unit", "local"] }, |
| 130 | ); |
| 131 | |
| 132 | it.effect( |
| 133 | "reports scan failures before other providers finish listing", |
| 134 | () => |
| 135 | Effect.gen(function* () { |
| 136 | const reported = yield* Deferred.make<void>(); |
| 137 | const failed: ProviderService = { |
| 138 | list: () => Effect.fail({ _tag: "AccessDenied", message: "denied during scan" }), |
| 139 | reconcile: () => Effect.succeed({}), |
| 140 | delete: () => Effect.void, |
| 141 | }; |
| 142 | const slow: ProviderService = { |
| 143 | ...failed, |
| 144 | list: () => Deferred.await(reported).pipe(Effect.as([])), |
| 145 | }; |
| 146 | const context = Context.make(Context.Service<ProviderService>("Test.Failed"), failed).pipe( |
| 147 | Context.add(Context.Service<ProviderService>("Test.Slow"), slow), |
| 148 | ); |
| 149 | const errors: string[] = []; |
| 150 | const result = yield* Nuke.list({ |
| 151 | context, |
| 152 | mode: "live", |
| 153 | }).pipe( |
| 154 | onEvent((event) => |
| 155 | Effect.gen(function* () { |
| 156 | if (event.type !== "nuke.scan.provider.completed" || event.error === undefined) return; |
| 157 | expect(event.provider).toBe("Test.Failed"); |
| 158 | expect(event.resources).toBe(0); |
| 159 | errors.push(event.error); |
| 160 | yield* Deferred.succeed(reported, undefined); |
| 161 | }), |
| 162 | ), |
| 163 | Effect.timeout("2 seconds"), |
| 164 | ); |
| 165 | expect(errors).toHaveLength(1); |
| 166 | expect(errors[0]).toContain("denied during scan"); |
| 167 | expect(result.failures[0]?.message).toBe(errors[0]); |
| 168 | }), |
| 169 | { tags: ["unit", "local"] }, |
| 170 | ); |
| 171 | |
| 172 | it.effect( |
| 173 | "settles a provider that dies, like a rejected credential layer", |
| 174 | () => |
| 175 | Effect.gen(function* () { |
| 176 | const provider: ProviderService = { |
| 177 | list: () => Effect.die({ _tag: "RailwayForbidden", message: "Not Authorized" }), |
| 178 | reconcile: () => Effect.succeed({}), |
| 179 | delete: () => Effect.die({ _tag: "RailwayForbidden", message: "Not Authorized" }), |
| 180 | }; |
| 181 | const healthy: ProviderService = { |
| 182 | list: () => Effect.succeed([{ name: "kept" }]), |
| 183 | reconcile: () => Effect.succeed({}), |
| 184 | delete: () => Effect.void, |
| 185 | }; |
| 186 | const context = Context.make(Context.Service<ProviderService>("Test.Dies"), provider).pipe( |
| 187 | Context.add(Context.Service<ProviderService>("Test.Healthy"), healthy), |
| 188 | ); |
| 189 | const scan = yield* Nuke.list({ context, mode: "live" }); |
| 190 | expect(scan.resources.map((resource) => resource.providerId)).toEqual(["Test.Healthy"]); |
| 191 | expect(scan.failures.map((failure) => failure.provider)).toEqual(["Test.Dies"]); |
| 192 | expect(scan.failures[0]?.message).toContain("Not Authorized"); |
| 193 | for (const strategy of [ |
| 194 | { type: "coordinated" }, |
| 195 | { type: "independent", retries: 0 }, |
| 196 | ] as const) { |
| 197 | const result = yield* Nuke.destroy({ |
| 198 | targets: [ |
| 199 | { key: "t1", providerId: "Test.Dies", displayName: "x", attributes: {}, provider }, |
| 200 | ], |
| 201 | context, |
| 202 | strategy, |
| 203 | }); |
| 204 | expect(result.failed[0]?.failure.message).toContain("Not Authorized"); |
| 205 | } |
| 206 | }), |
| 207 | { tags: ["unit", "local"] }, |
| 208 | ); |