| 1 | import { noopSession } from "@/Report"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import { Server, ServerProviderLive } from "@/Website/Server"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as FileSystem from "effect/FileSystem"; |
| 7 | import * as Path from "effect/Path"; |
| 8 | |
| 9 | const { test } = Test.make({ providers: ServerProviderLive() }); |
| 10 | |
| 11 | for (const location of [ |
| 12 | "root", |
| 13 | "ancestor", |
| 14 | "sibling", |
| 15 | "symlink", |
| 16 | "dist", |
| 17 | ] as const) { |
| 18 | test.provider( |
| 19 | `cleanup preserves source when output is ${location}`, |
| 20 | () => |
| 21 | Effect.gen(function* () { |
| 22 | const fs = yield* FileSystem.FileSystem; |
| 23 | const path = yield* Path.Path; |
| 24 | const directory = yield* fs.makeTempDirectoryScoped(); |
| 25 | const root = path.join(directory, "app"); |
| 26 | const dist = path.join(root, "dist"); |
| 27 | const sibling = path.join(directory, "other-app"); |
| 28 | yield* fs.makeDirectory(dist, { recursive: true }); |
| 29 | yield* fs.makeDirectory(sibling); |
| 30 | const source = path.join(root, "page.tsx"); |
| 31 | yield* fs.writeFileString(source, "export default () => 'keep me';"); |
| 32 | yield* fs.writeFileString(path.join(dist, "index.html"), "generated"); |
| 33 | yield* fs.writeFileString( |
| 34 | path.join(sibling, "source.ts"), |
| 35 | "keep sibling", |
| 36 | ); |
| 37 | const alias = path.join(root, "output-link"); |
| 38 | yield* fs.symlink(root, alias); |
| 39 | const distDir = { |
| 40 | root, |
| 41 | ancestor: directory, |
| 42 | sibling, |
| 43 | symlink: alias, |
| 44 | dist, |
| 45 | }[location]; |
| 46 | const provider = yield* Server.Provider; |
| 47 | const remove = provider.delete({ |
| 48 | id: "Build", |
| 49 | fqn: "Build", |
| 50 | instanceId: "cleanup-test", |
| 51 | olds: { framework: "unused", target: "unused", root }, |
| 52 | output: { |
| 53 | distDir, |
| 54 | clientDir: undefined, |
| 55 | serverEntry: undefined, |
| 56 | url: undefined, |
| 57 | hash: { input: undefined, output: undefined }, |
| 58 | }, |
| 59 | bindings: [], |
| 60 | session: { ...noopSession, note: () => Effect.void }, |
| 61 | }); |
| 62 | yield* remove; |
| 63 | yield* remove; |
| 64 | expect(yield* fs.readFileString(source)).toBe( |
| 65 | "export default () => 'keep me';", |
| 66 | ); |
| 67 | expect(yield* fs.readFileString(path.join(sibling, "source.ts"))).toBe( |
| 68 | "keep sibling", |
| 69 | ); |
| 70 | expect(yield* fs.exists(dist)).toBe(location !== "dist"); |
| 71 | }), |
| 72 | { tags: ["unit", "local"], timeout: 10_000 }, |
| 73 | ); |
| 74 | } |