The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as FileSystem from "effect/FileSystem"; |
| 4 | import * as Path from "effect/Path"; |
| 5 | import type * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 6 | import type * as Scope from "effect/Scope"; |
| 7 | import { describe, expect, it } from "vitest"; |
| 8 | import { resolveConfigPath, runOpenNextBuild } from "../Runner.ts"; |
| 9 | import source from "../source.ts"; |
| 10 | |
| 11 | const run = <A, E>( |
| 12 | effect: Effect.Effect< |
| 13 | A, |
| 14 | E, |
| 15 | FileSystem.FileSystem | Path.Path | Scope.Scope | ChildProcessSpawner.ChildProcessSpawner |
| 16 | >, |
| 17 | ) => Effect.runPromise(Effect.scoped(effect).pipe(Effect.provide(NodeServices.layer))); |
| 18 | |
| 19 | const fixture = Effect.gen(function* () { |
| 20 | const fs = yield* FileSystem.FileSystem; |
| 21 | const path = yield* Path.Path; |
| 22 | const root = yield* fs.makeTempDirectoryScoped({ |
| 23 | directory: yield* path.fromFileUrl(new URL("../../../", import.meta.url)), |
| 24 | prefix: ".nextjs-config-test-", |
| 25 | }); |
| 26 | return { fs, path, root }; |
| 27 | }); |
| 28 | |
| 29 | describe("optional OpenNext configuration", () => { |
| 30 | it("generates defaults only when the native config is absent", () => |
| 31 | run( |
| 32 | Effect.gen(function* () { |
| 33 | const { fs, path, root } = yield* fixture; |
| 34 | expect(yield* resolveConfigPath({ appDir: root })).toBeUndefined(); |
| 35 | expect(yield* fs.readDirectory(root)).toEqual([]); |
| 36 | const configPath = path.join(root, "open-next.config.ts"); |
| 37 | yield* fs.writeFileString(configPath, "export default { default: {} };\n"); |
| 38 | expect(yield* resolveConfigPath({ appDir: root })).toBe(configPath); |
| 39 | expect(yield* fs.readFileString(configPath)).toBe("export default { default: {} };\n"); |
| 40 | }), |
| 41 | )); |
| 42 | |
| 43 | it("prefers an explicit file and rejects a missing explicit path", () => |
| 44 | run( |
| 45 | Effect.gen(function* () { |
| 46 | const { fs, path, root } = yield* fixture; |
| 47 | yield* fs.writeFileString(path.join(root, "open-next.config.ts"), "native"); |
| 48 | yield* fs.writeFileString(path.join(root, "custom.mjs"), "custom"); |
| 49 | expect(yield* resolveConfigPath({ appDir: root, configPath: "custom.mjs" })).toBe( |
| 50 | path.join(root, "custom.mjs"), |
| 51 | ); |
| 52 | const error = yield* resolveConfigPath({ |
| 53 | appDir: root, |
| 54 | configPath: "missing.ts", |
| 55 | }).pipe(Effect.flip); |
| 56 | expect(error.message).toContain("OpenNext config file not found"); |
| 57 | }), |
| 58 | )); |
| 59 | |
| 60 | it("rejects a directory instead of treating it as absent", () => |
| 61 | run( |
| 62 | Effect.gen(function* () { |
| 63 | const { fs, path, root } = yield* fixture; |
| 64 | yield* fs.makeDirectory(path.join(root, "open-next.config.ts")); |
| 65 | const error = yield* resolveConfigPath({ appDir: root }).pipe(Effect.flip); |
| 66 | expect(error.message).toContain("OpenNext config is not a file"); |
| 67 | }), |
| 68 | )); |
| 69 | |
| 70 | it("hashes config creation, edits, and removal even outside a narrowed memo scope", () => |
| 71 | run( |
| 72 | Effect.gen(function* () { |
| 73 | const { fs, path, root } = yield* fixture; |
| 74 | const provider = yield* source.make({ |
| 75 | root, |
| 76 | memo: { include: ["app/**"] }, |
| 77 | }); |
| 78 | const context = { |
| 79 | id: "Site", |
| 80 | workerName: "site", |
| 81 | compatibility: { date: "2026-08-31", flags: [] }, |
| 82 | }; |
| 83 | const hash = () => provider.hash(context, undefined); |
| 84 | const absent = yield* hash(); |
| 85 | const configPath = path.join(root, "open-next.config.ts"); |
| 86 | yield* fs.writeFileString(configPath, "export default { default: {} };\n"); |
| 87 | const created = yield* hash(); |
| 88 | expect(created.input).not.toBe(absent.input); |
| 89 | expect((yield* hash()).input).toBe(created.input); |
| 90 | const relocated = yield* fixture; |
| 91 | yield* fs.writeFileString( |
| 92 | relocated.path.join(relocated.root, "open-next.config.ts"), |
| 93 | "export default { default: {} };\n", |
| 94 | ); |
| 95 | const relocatedProvider = yield* source.make({ |
| 96 | root: relocated.root, |
| 97 | memo: { include: ["app/**"] }, |
| 98 | }); |
| 99 | expect((yield* relocatedProvider.hash(context, undefined)).input).toBe(created.input); |
| 100 | yield* fs.writeFileString(configPath, "export default { default: { minify: true } };\n"); |
| 101 | expect((yield* hash()).input).not.toBe(created.input); |
| 102 | yield* fs.remove(configPath); |
| 103 | expect((yield* hash()).input).toBe(absent.input); |
| 104 | }), |
| 105 | )); |
| 106 | |
| 107 | it("cleans parent-owned temporary configuration when the child fails", () => |
| 108 | run( |
| 109 | Effect.gen(function* () { |
| 110 | const { fs, root } = yield* fixture; |
| 111 | const directories: string[] = []; |
| 112 | const scratchFiles: string[] = []; |
| 113 | const tracked = { |
| 114 | ...fs, |
| 115 | makeTempDirectoryScoped: (options?: Parameters<typeof fs.makeTempDirectoryScoped>[0]) => |
| 116 | fs.makeTempDirectoryScoped(options).pipe( |
| 117 | Effect.tap((directory) => |
| 118 | Effect.gen(function* () { |
| 119 | directories.push(directory); |
| 120 | yield* Effect.addFinalizer(() => |
| 121 | fs.readDirectory(directory).pipe( |
| 122 | Effect.tap((files) => |
| 123 | Effect.sync(() => { |
| 124 | scratchFiles.push(...files); |
| 125 | }), |
| 126 | ), |
| 127 | Effect.ignore, |
| 128 | ), |
| 129 | ); |
| 130 | }), |
| 131 | ), |
| 132 | ), |
| 133 | }; |
| 134 | const error = yield* runOpenNextBuild({ |
| 135 | appDir: root, |
| 136 | compatibilityDate: "2026-08-31", |
| 137 | }).pipe(Effect.provideService(FileSystem.FileSystem, tracked), Effect.flip); |
| 138 | expect(error._tag).toBe("RunnerError"); |
| 139 | expect(directories).toHaveLength(1); |
| 140 | expect(scratchFiles).toContain("open-next.config.mjs"); |
| 141 | expect(scratchFiles.some((file) => file.startsWith("open-next-tmp"))).toBe(true); |
| 142 | for (const directory of directories) expect(yield* fs.exists(directory)).toBe(false); |
| 143 | }), |
| 144 | )); |
| 145 | }); |