The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { Persistence } from "@t3tools/client-runtime/platform"; |
| 2 | import { EnvironmentId } from "@t3tools/contracts"; |
| 3 | import { describe, expect, it } from "@effect/vitest"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as Logger from "effect/Logger"; |
| 7 | import { vi } from "vite-plus/test"; |
| 8 | |
| 9 | const harness = vi.hoisted(() => ({ |
| 10 | clearOutbox: vi.fn<(environmentId: string) => Promise<void>>(), |
| 11 | clearDrafts: vi.fn<(environmentId: string) => Promise<void>>(), |
| 12 | clearComposerErrors: vi.fn(), |
| 13 | })); |
| 14 | |
| 15 | vi.mock("../state/thread-outbox-removal", () => ({ |
| 16 | clearThreadOutboxEnvironment: harness.clearOutbox, |
| 17 | })); |
| 18 | vi.mock("../state/use-composer-drafts", () => ({ |
| 19 | clearComposerDraftsEnvironment: harness.clearDrafts, |
| 20 | })); |
| 21 | vi.mock("../state/thread-composer-error", () => ({ |
| 22 | clearThreadComposerErrorsForEnvironment: harness.clearComposerErrors, |
| 23 | })); |
| 24 | |
| 25 | import * as EnvironmentOwnedDataCleanup from "./environment-owned-data-cleanup"; |
| 26 | |
| 27 | const ENVIRONMENT_ID = EnvironmentId.make("environment-1"); |
| 28 | |
| 29 | describe("EnvironmentOwnedDataCleanup", () => { |
| 30 | it.effect("waits for the other clears and logs a bounded warning when one rejects", () => { |
| 31 | const warnings: Array<unknown> = []; |
| 32 | let releaseDrafts = () => {}; |
| 33 | let draftsCleared = false; |
| 34 | // The drafts clear finishes only after the outbox failure is logged. |
| 35 | const logger = Logger.make(({ message }) => { |
| 36 | warnings.push(message); |
| 37 | releaseDrafts(); |
| 38 | }); |
| 39 | return Effect.gen(function* () { |
| 40 | harness.clearOutbox.mockRejectedValue(new Error("Some queued messages could not be read.")); |
| 41 | harness.clearDrafts.mockImplementation(() => |
| 42 | new Promise<void>((resolve) => { |
| 43 | releaseDrafts = resolve; |
| 44 | }).then(() => { |
| 45 | draftsCleared = true; |
| 46 | }), |
| 47 | ); |
| 48 | |
| 49 | const cleanup = yield* Persistence.EnvironmentOwnedDataCleanup; |
| 50 | yield* cleanup.clear(ENVIRONMENT_ID); |
| 51 | |
| 52 | expect(draftsCleared).toBe(true); |
| 53 | expect(warnings).toEqual([ |
| 54 | [ |
| 55 | "Could not clear mobile environment-owned data.", |
| 56 | { |
| 57 | environmentId: ENVIRONMENT_ID, |
| 58 | operation: "outbox", |
| 59 | errorType: "error", |
| 60 | errorName: "Error", |
| 61 | stack: expect.any(String), |
| 62 | }, |
| 63 | ], |
| 64 | ]); |
| 65 | }).pipe( |
| 66 | Effect.provide( |
| 67 | Layer.merge( |
| 68 | EnvironmentOwnedDataCleanup.layer, |
| 69 | Logger.layer([logger], { mergeWithExisting: false }), |
| 70 | ), |
| 71 | ), |
| 72 | ); |
| 73 | }); |
| 74 | }); |