Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

mobile-native-client.test.ts

65 lines2.6 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6​
7import { NativeClientError, prebuildAndroid } from "./mobile-native-client.ts";
8​
9const caches = [".gradle/history", "app/.cxx/object"];
10​
11// A generated Android project with Gradle and native outputs, and a stand-in for
12// `expo prebuild --clean`, which deletes the directory and writes fresh sources.
13const fixture = Effect.gen(function* () {
14 const fs = yield* FileSystem.FileSystem;
15 const path = yield* Path.Path;
16 const mobile = yield* fs.makeTempDirectoryScoped({ prefix: "mobile-native-client-" });
17 const android = path.join(mobile, "android");
18 for (const cache of caches) {
19 yield* fs.makeDirectory(path.dirname(path.join(android, cache)), { recursive: true });
20 yield* fs.writeFileString(path.join(android, cache), cache);
21 }
22 const cleanPrebuild = Effect.gen(function* () {
23 yield* fs.remove(android, { recursive: true });
24 yield* fs.makeDirectory(path.join(android, "app"), { recursive: true });
25 yield* fs.writeFileString(path.join(android, "app/build.gradle"), "regenerated");
26 });
27 const assertCachesRestored = Effect.gen(function* () {
28 for (const cache of caches) {
29 assert.strictEqual(yield* fs.readFileString(path.join(android, cache)), cache);
30 }
31 assert.deepStrictEqual(yield* fs.readDirectory(mobile), ["android"]);
32 });
33 return { fs, path, mobile, android, cleanPrebuild, assertCachesRestored };
34});
35​
36it.layer(NodeServices.layer)("prebuildAndroid", (it) => {
37 it.effect("keeps Gradle and native caches through a clean prebuild", () =>
38 Effect.gen(function* () {
39 const { fs, path, mobile, android, cleanPrebuild, assertCachesRestored } = yield* fixture;
40​
41 yield* prebuildAndroid(mobile, cleanPrebuild);
42​
43 assert.strictEqual(
44 yield* fs.readFileString(path.join(android, "app/build.gradle")),
45 "regenerated",
46 );
47 yield* assertCachesRestored;
48 }),
49 );
50​
51 it.effect("restores the caches and returns the error when prebuild fails", () =>
52 Effect.gen(function* () {
53 const { mobile, cleanPrebuild, assertCachesRestored } = yield* fixture;
54 const failure = new NativeClientError({ message: "prebuild failed" });
55​
56 const error = yield* prebuildAndroid(
57 mobile,
58 cleanPrebuild.pipe(Effect.andThen(Effect.fail(failure))),
59 ).pipe(Effect.flip);
60​
61 assert.strictEqual(error, failure);
62 yield* assertCachesRestored;
63 }),
64 );
65});