|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { expect, it } from "@effect/vitest"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as FileSystem from "effect/FileSystem"; |
| 5 | +import * as Path from "effect/Path"; |
| 6 | +import * as PlatformError from "effect/PlatformError"; |
| 7 | + |
| 8 | +import { retireAntigravityRuntimeTempDirs } from "./AntigravitySessionFiles.ts"; |
| 9 | + |
| 10 | +it.layer(NodeServices.layer)("retireAntigravityRuntimeTempDirs", (it) => { |
| 11 | + it.effect("moves leftovers aside at once and deletes them, plus older retirees, later", () => |
| 12 | + Effect.gen(function* () { |
| 13 | + const fs = yield* FileSystem.FileSystem; |
| 14 | + const path = yield* Path.Path; |
| 15 | + const parent = yield* fs.makeTempDirectoryScoped(); |
| 16 | + const runtimeTemp = path.join(parent, "abc123"); |
| 17 | + const orphan = path.join(runtimeTemp, "run-orphan", "_MEI123"); |
| 18 | + yield* fs.makeDirectory(orphan, { recursive: true }); |
| 19 | + yield* fs.writeFileString(path.join(orphan, "payload.bin"), "stale"); |
| 20 | + yield* fs.makeDirectory(path.join(parent, "abc123.old-unfinished", "runtime"), { |
| 21 | + recursive: true, |
| 22 | + }); |
| 23 | + yield* fs.makeDirectory(path.join(parent, "other-instance")); |
| 24 | + |
| 25 | + const removeRetired = yield* retireAntigravityRuntimeTempDirs(runtimeTemp); |
| 26 | + expect(yield* fs.exists(runtimeTemp)).toBe(false); |
| 27 | + const retired = (yield* fs.readDirectory(parent)).filter((entry) => |
| 28 | + entry.startsWith("abc123.old-"), |
| 29 | + ); |
| 30 | + expect(retired).toHaveLength(2); |
| 31 | + |
| 32 | + yield* removeRetired; |
| 33 | + expect((yield* fs.readDirectory(parent)).toSorted()).toEqual(["other-instance"]); |
| 34 | + }).pipe(Effect.scoped), |
| 35 | + ); |
| 36 | + |
| 37 | + it.effect( |
| 38 | + "deletes leftovers inline when they cannot be moved aside, such as on a full disk", |
| 39 | + () => |
| 40 | + Effect.gen(function* () { |
| 41 | + const removed: Array<string> = []; |
| 42 | + const fs = FileSystem.makeNoop({ |
| 43 | + exists: () => Effect.succeed(true), |
| 44 | + makeTempDirectory: () => |
| 45 | + Effect.fail( |
| 46 | + PlatformError.systemError({ |
| 47 | + _tag: "Unknown", |
| 48 | + module: "FileSystem", |
| 49 | + method: "makeTempDirectory", |
| 50 | + description: "ENOSPC", |
| 51 | + }), |
| 52 | + ), |
| 53 | + remove: (path) => Effect.sync(() => void removed.push(path)), |
| 54 | + readDirectory: () => Effect.succeed([]), |
| 55 | + }); |
| 56 | + |
| 57 | + const removeRetired = yield* retireAntigravityRuntimeTempDirs( |
| 58 | + "/state/antigravity-tmp/abc123", |
| 59 | + ).pipe(Effect.provideService(FileSystem.FileSystem, fs)); |
| 60 | + yield* removeRetired; |
| 61 | + expect(removed).toEqual(["/state/antigravity-tmp/abc123"]); |
| 62 | + }), |
| 63 | + ); |
| 64 | +}); |
0 commit comments