|
1 | | -import { assert, it } from "@effect/vitest"; |
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { assert, expect, it } from "@effect/vitest"; |
| 3 | +import { |
| 4 | + HostProcessArguments, |
| 5 | + HostProcessExecutablePath, |
| 6 | + HostProcessIsExecutable, |
| 7 | + HostProcessPlatform, |
| 8 | +} from "@t3tools/shared/hostProcess"; |
| 9 | +import * as Effect from "effect/Effect"; |
| 10 | +import * as FileSystem from "effect/FileSystem"; |
| 11 | +import * as Path from "effect/Path"; |
2 | 12 |
|
3 | | -import { formatCliCommand } from "./invocation.ts"; |
| 13 | +import { formatCliCommand, resolveServerInstallation } from "./invocation.ts"; |
4 | 14 |
|
5 | 15 | it("formats package runner commands from their cache entry paths", () => { |
6 | 16 | for (const [entryPath, expected] of [ |
@@ -87,3 +97,123 @@ it("formats serve suggestions to match the launching command", () => { |
87 | 97 | "t3 serve", |
88 | 98 | ); |
89 | 99 | }); |
| 100 | + |
| 101 | +it.layer(NodeServices.layer)("manual server installation ownership", (it) => { |
| 102 | + it.effect("recognizes runner caches for both script and executable packages", () => |
| 103 | + Effect.gen(function* () { |
| 104 | + const fs = yield* FileSystem.FileSystem; |
| 105 | + const path = yield* Path.Path; |
| 106 | + const root = yield* fs.makeTempDirectoryScoped(); |
| 107 | + for (const [relative, kind] of [ |
| 108 | + ["npm/_npx/hash/node_modules/t3/dist/bin.mjs", "npx"], |
| 109 | + ["npm/_npx/hash/node_modules/@t3code/t3-linux-x64/t3", "npx"], |
| 110 | + ["pnpm/dlx/hash/node_modules/t3/dist/bin.mjs", "pnpm-dlx"], |
| 111 | + [".bun/install/cache/t3/dist/bin.mjs", "bunx"], |
| 112 | + ] as const) { |
| 113 | + const entry = path.join(root, relative); |
| 114 | + yield* fs.makeDirectory(path.dirname(entry), { recursive: true }); |
| 115 | + yield* fs.writeFileString(entry, ""); |
| 116 | + const installation = yield* resolveServerInstallation.pipe( |
| 117 | + Effect.provideService(HostProcessArguments, ["node", entry]), |
| 118 | + Effect.provideService(HostProcessExecutablePath, entry), |
| 119 | + Effect.provideService(HostProcessIsExecutable, entry.endsWith("/t3")), |
| 120 | + ); |
| 121 | + expect(installation).toEqual({ kind }); |
| 122 | + } |
| 123 | + }), |
| 124 | + ); |
| 125 | + |
| 126 | + it.effect("requires the npm prefix's bin to point to the running package", () => |
| 127 | + Effect.gen(function* () { |
| 128 | + const fs = yield* FileSystem.FileSystem; |
| 129 | + const path = yield* Path.Path; |
| 130 | + const root = yield* fs.makeTempDirectoryScoped(); |
| 131 | + const prefix = path.join(root, "bunx-tools"); |
| 132 | + const packageRoot = path.join(prefix, "lib/node_modules/t3"); |
| 133 | + const entry = path.join(packageRoot, "dist/bin.mjs"); |
| 134 | + const globalBin = path.join(prefix, "bin/t3"); |
| 135 | + yield* fs.makeDirectory(path.dirname(entry), { recursive: true }); |
| 136 | + yield* fs.makeDirectory(path.dirname(globalBin), { recursive: true }); |
| 137 | + yield* fs.writeFileString(entry, ""); |
| 138 | + yield* fs.writeFileString( |
| 139 | + path.join(packageRoot, "package.json"), |
| 140 | + '{"name":"t3","version":"0.0.45","bin":{"t3":"./dist/bin.mjs"}}', |
| 141 | + ); |
| 142 | + const resolve = resolveServerInstallation.pipe( |
| 143 | + Effect.provideService(HostProcessArguments, ["node", entry]), |
| 144 | + Effect.provideService(HostProcessIsExecutable, false), |
| 145 | + Effect.provideService(HostProcessPlatform, "linux"), |
| 146 | + ); |
| 147 | + expect(yield* resolve).toBeNull(); |
| 148 | + yield* fs.symlink(entry, globalBin); |
| 149 | + expect(yield* resolve).toEqual({ kind: "npm-global", prefix }); |
| 150 | + yield* fs.remove(globalBin); |
| 151 | + yield* fs.writeFileString(globalBin, "an unrelated t3 command"); |
| 152 | + expect(yield* resolve).toBeNull(); |
| 153 | + expect(yield* resolve.pipe(Effect.provideService(HostProcessPlatform, "win32"))).toBeNull(); |
| 154 | + }), |
| 155 | + ); |
| 156 | + |
| 157 | + it.effect("proves the native executable belongs to the npm launcher", () => |
| 158 | + Effect.gen(function* () { |
| 159 | + const fs = yield* FileSystem.FileSystem; |
| 160 | + const path = yield* Path.Path; |
| 161 | + const root = yield* fs.makeTempDirectoryScoped(); |
| 162 | + const prefix = path.join(root, "bunx-tools"); |
| 163 | + const packageRoot = path.join(prefix, "lib/node_modules/t3"); |
| 164 | + const launcher = path.join(packageRoot, "bin/t3.js"); |
| 165 | + const entry = path.join(packageRoot, "node_modules/@t3code/t3-linux-x64/t3"); |
| 166 | + yield* fs.makeDirectory(path.dirname(launcher), { recursive: true }); |
| 167 | + yield* fs.makeDirectory(path.dirname(entry), { recursive: true }); |
| 168 | + yield* fs.makeDirectory(path.join(prefix, "bin")); |
| 169 | + yield* fs.writeFileString(launcher, ""); |
| 170 | + yield* fs.writeFileString(entry, ""); |
| 171 | + yield* fs.writeFileString( |
| 172 | + path.join(packageRoot, "package.json"), |
| 173 | + '{"name":"t3","version":"0.0.45","bin":{"t3":"./bin/t3.js"},"optionalDependencies":{"@t3code/t3-linux-x64":"0.0.45"}}', |
| 174 | + ); |
| 175 | + yield* fs.symlink(launcher, path.join(prefix, "bin/t3")); |
| 176 | + const resolve = resolveServerInstallation.pipe( |
| 177 | + Effect.provideService(HostProcessExecutablePath, entry), |
| 178 | + Effect.provideService(HostProcessIsExecutable, true), |
| 179 | + Effect.provideService(HostProcessPlatform, "linux"), |
| 180 | + ); |
| 181 | + for (const [version, expected] of [ |
| 182 | + ["0.0.44", null], |
| 183 | + ["0.0.45", { kind: "npm-global", prefix }], |
| 184 | + ]) { |
| 185 | + yield* fs.writeFileString( |
| 186 | + path.join(path.dirname(entry), "package.json"), |
| 187 | + `{"name":"@t3code/t3-linux-x64","version":"${version}"}`, |
| 188 | + ); |
| 189 | + expect(yield* resolve).toEqual(expected); |
| 190 | + } |
| 191 | + }), |
| 192 | + ); |
| 193 | + |
| 194 | + it.effect("leaves local, standalone, missing and unreadable installs unknown", () => |
| 195 | + Effect.gen(function* () { |
| 196 | + const fs = yield* FileSystem.FileSystem; |
| 197 | + const path = yield* Path.Path; |
| 198 | + const root = yield* fs.makeTempDirectoryScoped(); |
| 199 | + for (const relative of [ |
| 200 | + "project/node_modules/t3/dist/bin.mjs", |
| 201 | + "project/apps/server/dist/bin.mjs", |
| 202 | + ".t3/runtime/0.0.45/t3", |
| 203 | + "missing/dist/bin.mjs", |
| 204 | + ]) { |
| 205 | + const entry = path.join(root, relative); |
| 206 | + if (!relative.startsWith("missing")) { |
| 207 | + yield* fs.makeDirectory(path.dirname(entry), { recursive: true }); |
| 208 | + yield* fs.writeFileString(entry, ""); |
| 209 | + } |
| 210 | + expect( |
| 211 | + yield* resolveServerInstallation.pipe( |
| 212 | + Effect.provideService(HostProcessArguments, ["node", entry]), |
| 213 | + Effect.provideService(HostProcessIsExecutable, false), |
| 214 | + ), |
| 215 | + ).toBeNull(); |
| 216 | + } |
| 217 | + }), |
| 218 | + ); |
| 219 | +}); |
0 commit comments