|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { assert, 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 | + |
| 7 | +import { applyWebBrandAssets } from "./apply-web-brand-assets.ts"; |
| 8 | + |
| 9 | +const WEB_ICON_FILENAMES = [ |
| 10 | + "apple-touch-icon.png", |
| 11 | + "favicon-16x16.png", |
| 12 | + "favicon-32x32.png", |
| 13 | + "favicon.ico", |
| 14 | +]; |
| 15 | + |
| 16 | +it.layer(NodeServices.layer)("apply-web-brand-assets", (it) => { |
| 17 | + it.effect("copies the icons into an absolute target directory as given", () => |
| 18 | + Effect.gen(function* () { |
| 19 | + const fs = yield* FileSystem.FileSystem; |
| 20 | + const targetDirectory = yield* fs.makeTempDirectoryScoped({ |
| 21 | + prefix: "apply-web-brand-assets-", |
| 22 | + }); |
| 23 | + |
| 24 | + yield* applyWebBrandAssets("production", targetDirectory); |
| 25 | + |
| 26 | + assert.deepStrictEqual( |
| 27 | + (yield* fs.readDirectory(targetDirectory)).toSorted(), |
| 28 | + WEB_ICON_FILENAMES, |
| 29 | + ); |
| 30 | + }), |
| 31 | + ); |
| 32 | + |
| 33 | + it.effect("resolves an empty target directory to the repo root", () => |
| 34 | + Effect.gen(function* () { |
| 35 | + const fs = yield* FileSystem.FileSystem; |
| 36 | + const path = yield* Path.Path; |
| 37 | + const repoRoot = yield* path.fromFileUrl(new URL("..", import.meta.url)); |
| 38 | + const destinations = WEB_ICON_FILENAMES.map((filename) => path.join(repoRoot, filename)); |
| 39 | + // readDirectory does not follow symlinks, so a dangling link counts as an existing entry. |
| 40 | + // Names are lowercased so a case-insensitive filesystem cannot hide a differently cased file. |
| 41 | + const existingEntries = new Set( |
| 42 | + (yield* fs.readDirectory(repoRoot)).map((entry) => entry.toLowerCase()), |
| 43 | + ); |
| 44 | + for (const destination of destinations) { |
| 45 | + assert.isFalse( |
| 46 | + existingEntries.has(path.basename(destination)), |
| 47 | + `${destination} already exists`, |
| 48 | + ); |
| 49 | + } |
| 50 | + yield* Effect.addFinalizer(() => |
| 51 | + Effect.forEach(destinations, (destination) => fs.remove(destination, { force: true })).pipe( |
| 52 | + Effect.orDie, |
| 53 | + ), |
| 54 | + ); |
| 55 | + |
| 56 | + yield* applyWebBrandAssets("production", ""); |
| 57 | + |
| 58 | + for (const destination of destinations) { |
| 59 | + assert.isTrue(yield* fs.exists(destination)); |
| 60 | + } |
| 61 | + }), |
| 62 | + ); |
| 63 | +}); |
0 commit comments