[codex] Structure terminal adapter startup defects (#3425)
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
b6e384fb27
commit
13a4789a5d
4 files changed
+100
-8
No files matched your search
@@ -1,9 +1,13 @@
|
||||
import { expect, it } from "@effect/vitest";
|
||||
import { assert, expect, it } from "@effect/vitest";
|
||||
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
|
||||
import * as Cause from "effect/Cause";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Exit from "effect/Exit";
|
||||
|
||||
import { BunPtyOperationUnavailableError } from "./BunPtyAdapter.ts";
|
||||
import * as BunPtyAdapter from "./BunPtyAdapter.ts";
|
||||
|
||||
it("describes unavailable Bun PTY operations structurally", () => {
|
||||
const error = new BunPtyOperationUnavailableError({
|
||||
const error = new BunPtyAdapter.BunPtyOperationUnavailableError({
|
||||
operation: "resize",
|
||||
pid: 42,
|
||||
});
|
||||
@@ -15,3 +19,26 @@ it("describes unavailable Bun PTY operations structurally", () => {
|
||||
});
|
||||
expect(error.message).toBe("Bun PTY resize is unavailable for process 42.");
|
||||
});
|
||||
|
||||
it.effect("reports unsupported platforms with a structured startup defect", () =>
|
||||
Effect.gen(function* () {
|
||||
const exit = yield* BunPtyAdapter.make().pipe(
|
||||
Effect.provideService(HostProcessPlatform, "win32"),
|
||||
Effect.exit,
|
||||
);
|
||||
|
||||
expect(Exit.isFailure(exit)).toBe(true);
|
||||
if (Exit.isFailure(exit)) {
|
||||
expect(Cause.hasDies(exit.cause)).toBe(true);
|
||||
const error = Cause.squash(exit.cause);
|
||||
assert.instanceOf(error, BunPtyAdapter.BunPtyUnsupportedPlatformError);
|
||||
expect(error).toMatchObject({
|
||||
_tag: "BunPtyUnsupportedPlatformError",
|
||||
platform: "win32",
|
||||
});
|
||||
expect(error.message).toBe(
|
||||
"Bun PTY terminal support is unavailable on win32. Please use Node.js (e.g. by running `npx t3`) instead.",
|
||||
);
|
||||
}
|
||||
}),
|
||||
);
|
||||
@@ -7,6 +7,17 @@ import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
|
||||
|
||||
import * as PtyAdapter from "./PtyAdapter.ts";
|
||||
|
||||
export class BunPtyUnsupportedPlatformError extends Schema.TaggedErrorClass<BunPtyUnsupportedPlatformError>()(
|
||||
"BunPtyUnsupportedPlatformError",
|
||||
{
|
||||
platform: Schema.Literal("win32"),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Bun PTY terminal support is unavailable on ${this.platform}. Please use Node.js (e.g. by running \`npx t3\`) instead.`;
|
||||
}
|
||||
}
|
||||
|
||||
export class BunPtyOperationUnavailableError extends Schema.TaggedErrorClass<BunPtyOperationUnavailableError>()(
|
||||
"BunPtyOperationUnavailableError",
|
||||
{
|
||||
@@ -109,9 +120,7 @@ class BunPtyProcess implements PtyAdapter.PtyProcess {
|
||||
export const make = Effect.fn("BunPtyAdapter.make")(function* () {
|
||||
const platform = yield* HostProcessPlatform;
|
||||
if (platform === "win32") {
|
||||
return yield* Effect.die(
|
||||
"Bun PTY terminal support is unavailable on Windows. Please use Node.js (e.g. by running `npx t3`) instead.",
|
||||
);
|
||||
return yield* Effect.die(new BunPtyUnsupportedPlatformError({ platform }));
|
||||
}
|
||||
return PtyAdapter.PtyAdapter.of({
|
||||
spawn: (input) =>
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import { assert, it } from "@effect/vitest";
|
||||
import { HostProcessArchitecture, HostProcessPlatform } from "@t3tools/shared/hostProcess";
|
||||
import * as Cause from "effect/Cause";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Exit from "effect/Exit";
|
||||
import * as Layer from "effect/Layer";
|
||||
import { vi } from "vite-plus/test";
|
||||
|
||||
@@ -56,3 +58,31 @@ it.effect("spawns through the public adapter with the provided host references",
|
||||
]);
|
||||
}).pipe(Effect.provide(testLayer)),
|
||||
);
|
||||
|
||||
it.effect("reports native module load failures as structured startup defects", () =>
|
||||
Effect.gen(function* () {
|
||||
const cause = new Error("native binding could not be loaded");
|
||||
const exit = yield* NodePtyAdapter.make(() => Promise.reject(cause)).pipe(Effect.exit);
|
||||
|
||||
assert.isTrue(Exit.isFailure(exit));
|
||||
if (Exit.isFailure(exit)) {
|
||||
assert.isTrue(Cause.hasDies(exit.cause));
|
||||
const error = Cause.squash(exit.cause);
|
||||
assert.instanceOf(error, NodePtyAdapter.NodePtyModuleLoadError);
|
||||
assert.deepInclude(error, {
|
||||
_tag: "NodePtyModuleLoadError",
|
||||
platform: "win32",
|
||||
architecture: "x64",
|
||||
});
|
||||
assert.equal(error.message, "Failed to load node-pty for win32-x64.");
|
||||
}
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
Layer.mergeAll(
|
||||
NodeServices.layer,
|
||||
Layer.succeed(HostProcessPlatform, "win32"),
|
||||
Layer.succeed(HostProcessArchitecture, "x64"),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -4,10 +4,26 @@ import * as Effect from "effect/Effect";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Path from "effect/Path";
|
||||
import * as Schema from "effect/Schema";
|
||||
import { HostProcessArchitecture, HostProcessPlatform } from "@t3tools/shared/hostProcess";
|
||||
|
||||
import * as PtyAdapter from "./PtyAdapter.ts";
|
||||
|
||||
export class NodePtyModuleLoadError extends Schema.TaggedErrorClass<NodePtyModuleLoadError>()(
|
||||
"NodePtyModuleLoadError",
|
||||
{
|
||||
platform: Schema.String,
|
||||
architecture: Schema.String,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Failed to load node-pty for ${this.platform}-${this.architecture}.`;
|
||||
}
|
||||
}
|
||||
|
||||
type NodePtyModuleLoader = () => Promise<typeof import("node-pty")>;
|
||||
|
||||
let didEnsureSpawnHelperExecutable = false;
|
||||
|
||||
const resolveNodePtySpawnHelperPath = Effect.gen(function* () {
|
||||
@@ -94,13 +110,23 @@ class NodePtyProcess implements PtyAdapter.PtyProcess {
|
||||
}
|
||||
}
|
||||
|
||||
export const make = Effect.fn("NodePtyAdapter.make")(function* () {
|
||||
export const make = Effect.fn("NodePtyAdapter.make")(function* (
|
||||
loadNodePtyModule: NodePtyModuleLoader = () => import("node-pty"),
|
||||
) {
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const platform = yield* HostProcessPlatform;
|
||||
const architecture = yield* HostProcessArchitecture;
|
||||
|
||||
const nodePty = yield* Effect.promise(() => import("node-pty"));
|
||||
const nodePty = yield* Effect.tryPromise({
|
||||
try: loadNodePtyModule,
|
||||
catch: (cause) =>
|
||||
new NodePtyModuleLoadError({
|
||||
platform,
|
||||
architecture,
|
||||
cause,
|
||||
}),
|
||||
}).pipe(Effect.orDie);
|
||||
|
||||
const ensureNodePtySpawnHelperExecutableCached = yield* Effect.cached(
|
||||
ensureNodePtySpawnHelperExecutable().pipe(
|
||||
|
||||
Reference in new issue
Block a user