Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
687 changes: 0 additions & 687 deletions apps/server/integration/WebAppReplayHarness.ts

This file was deleted.

2 changes: 2 additions & 0 deletions apps/server/integration/harness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { createHarness } from "./harness/index.ts";
export type { Fixture, Interaction, Harness } from "./harness/index.ts";
36 changes: 36 additions & 0 deletions apps/server/integration/harness/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# harness (app adapter)

`harness` is the server-side adapter for replay E2E tests.

It wires `@t3tools/rr-e2e` primitives into T3 server services (Codex manager, Git/GitHub adapters, persistence, and web app bootstrapping).

## Key components

- `harness.ts`
- Creates the runtime + Vite app and returns `{ appUrl, openPage, dispose }`.
- `codexProcess.ts`
- Bridges Codex app-server lifecycle to generic JSON-RPC replay transport.
- `services.ts`
- Bridges Git and GitHub service calls to generic CLI replay invoker.
- `types.ts`
- App-facing aliases (`Fixture`, `Interaction`) over shared rr-e2e types.
- `../thread.rr-e2e.test.ts`
- Scenario-driven browser tests using this adapter.
- `../thread.rr-e2e.test.fixture.ts`
- Named fixtures (`bootstrap`, `happyPath`, `twoTurns`, `providerOffline`).

## How to add a new scenario fixture

1. Add a new named fixture in `thread.rr-e2e.test.fixture.ts`.
2. Reuse shared helpers (`baseGitInteractions`, `turnInteraction`, `makeBaseFixture`) whenever possible.
3. Add a test case in `thread.rr-e2e.test.ts` using `runScenario("<fixtureName>", ...)`.
4. Run replay E2E and fill any missing interactions reported by mismatch errors.

## How to record fixture changes safely

- Start with an existing passing scenario and mutate incrementally.
- Keep each interaction purpose-specific and named.
- Prefer state references (`$ref: "state.*"`) over duplicated literals.
- If a new backend IO method appears (for example `github.<newMethod>`), add only fixture entries; adapter code should usually not require changes.

For core replay semantics and matching rules, see `packages/rr-e2e/README.md`.
30 changes: 30 additions & 0 deletions apps/server/integration/harness/codexProcess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { createReplayJsonRpcProcessController } from "@t3tools/rr-e2e";

import type {
CodexAppServerProcessController,
CodexCliVersionCheckResult,
} from "../../src/codexAppServerManager.ts";

import type { Fixture } from "./types.ts";

export function makeReplayCodexProcessController(
fixture: Fixture,
state: Record<string, unknown>,
): CodexAppServerProcessController {
const controller = createReplayJsonRpcProcessController(fixture, state, {
requestService: "codex.request",
versionCheckService: "codex.versionCheck",
requestContext: (input, request) => ({
binaryPath: input.binaryPath,
cwd: input.cwd,
method: request.method,
...(request.params !== undefined ? { params: request.params } : {}),
}),
});

return {
spawnAppServer: controller.spawnAppServer,
runVersionCheck: (input) => controller.runVersionCheck(input) as CodexCliVersionCheckResult,
kill: controller.kill,
};
}
203 changes: 203 additions & 0 deletions apps/server/integration/harness/harness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";

import * as NodeServices from "@effect/platform-node/NodeServices";
import { Effect, Exit, Layer, Scope } from "effect";
import type { Browser, BrowserContext, BrowserContextOptions, Page } from "playwright";
import type { ViteDevServer } from "vite";
import { createServer as createViteServer } from "vite";

import { CodexAppServerManager } from "../../src/codexAppServerManager.ts";
import { ServerConfig, type ServerConfigShape } from "../../src/config.ts";
import { GitHubCli } from "../../src/git/Services/GitHubCli.ts";
import { GitService } from "../../src/git/Services/GitService.ts";
import { Open } from "../../src/open.ts";
import { makeSqlitePersistenceLive } from "../../src/persistence/Layers/Sqlite.ts";
import { ProviderSessionRuntimeRepositoryLive } from "../../src/persistence/Layers/ProviderSessionRuntime.ts";
import { makeCodexAdapterLive } from "../../src/provider/Layers/CodexAdapter.ts";
import { ProviderAdapterRegistryLive } from "../../src/provider/Layers/ProviderAdapterRegistry.ts";
import { makeProviderServiceLive } from "../../src/provider/Layers/ProviderService.ts";
import { ProviderSessionDirectoryLive } from "../../src/provider/Layers/ProviderSessionDirectory.ts";
import { ProviderHealth } from "../../src/provider/Services/ProviderHealth.ts";
import { makeServerRuntimeServicesLayer } from "../../src/serverLayers.ts";
import { TerminalManager } from "../../src/terminal/Services/Manager.ts";
import { AnalyticsService } from "../../src/telemetry/Services/AnalyticsService.ts";
import { createServer } from "../../src/wsServer.ts";

import webViteConfig from "../../../web/vite.config.ts";

import { makeReplayCodexProcessController } from "./codexProcess.ts";
import { cloneJson, readReplayFixture } from "@t3tools/rr-e2e";
import {
defaultProviderStatuses,
makeReplayGitHubCli,
makeReplayGitService,
noOpOpenService,
noOpTerminalManager,
} from "./services.ts";
import type { Fixture } from "./types.ts";

export interface Harness {
readonly appUrl: string;
readonly openPage: (
browser: Browser,
options?: BrowserContextOptions,
) => Promise<{ context: BrowserContext; page: Page }>;
readonly dispose: () => Promise<void>;
}

interface CreateHarnessOptions {
readonly fixture?: Fixture;
readonly fixtureName?: string;
}

function webRootPath(): string {
return path.resolve(fileURLToPath(new URL("../../../web", import.meta.url)));
}

export async function createHarness(
testFileUrl: string,
options?: CreateHarnessOptions,
): Promise<Harness> {
const fixture =
options?.fixture ?? ((await readReplayFixture(testFileUrl, options?.fixtureName)) as Fixture);
const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), "t3-web-replay-"));
const workspaceDir = path.join(rootDir, "workspace");
const stateDir = path.join(rootDir, "state");
const dbPath = path.join(stateDir, "state.sqlite");
fs.mkdirSync(workspaceDir, { recursive: true });
fs.mkdirSync(stateDir, { recursive: true });

const state = cloneJson(fixture.state ?? {});
state.cwd = workspaceDir;
state.projectName = state.projectName ?? (path.basename(workspaceDir) || "workspace");

const providerSessionDirectoryLayer = ProviderSessionDirectoryLive.pipe(
Layer.provide(ProviderSessionRuntimeRepositoryLive),
);
const codexProcessController = makeReplayCodexProcessController(fixture, state);
const codexAdapterLayer = makeCodexAdapterLive({
makeManager: (services) =>
new CodexAppServerManager(services, {
processController: codexProcessController,
}),
});
const providerRegistryLayer = ProviderAdapterRegistryLive.pipe(Layer.provide(codexAdapterLayer));
const providerLayer = makeProviderServiceLive().pipe(
Layer.provide(providerRegistryLayer),
Layer.provide(providerSessionDirectoryLayer),
);

const replayGitServiceLayer = Layer.succeed(GitService, makeReplayGitService(fixture, state));
const replayGitHubCliLayer = Layer.succeed(GitHubCli, makeReplayGitHubCli(fixture, state));
const replayTerminalLayer = Layer.succeed(TerminalManager, noOpTerminalManager);
const persistenceLayer = makeSqlitePersistenceLive(dbPath);
const infrastructureLayer = providerLayer.pipe(Layer.provideMerge(persistenceLayer));
const serverConfigLayer = Layer.succeed(ServerConfig, {
mode: "web",
port: 0,
host: "127.0.0.1",
cwd: workspaceDir,
keybindingsConfigPath: path.join(stateDir, "keybindings.json"),
stateDir,
staticDir: undefined,
devUrl: undefined,
noBrowser: true,
authToken: undefined,
autoBootstrapProjectFromCwd: true,
logWebSocketEvents: false,
} satisfies ServerConfigShape);
const providerHealthLayer = Layer.succeed(ProviderHealth, {
getStatuses: Effect.succeed(fixture.providerStatuses ?? defaultProviderStatuses()),
});
const openLayer = Layer.succeed(Open, noOpOpenService);
const runtimeLayer = Layer.merge(
makeServerRuntimeServicesLayer({
gitServiceLayer: replayGitServiceLayer,
gitHubCliLayer: replayGitHubCliLayer,
terminalLayer: replayTerminalLayer,
}).pipe(Layer.provide(infrastructureLayer)),
infrastructureLayer,
);
const dependenciesLayer = Layer.empty.pipe(
Layer.provideMerge(runtimeLayer),
Layer.provideMerge(providerHealthLayer),
Layer.provideMerge(openLayer),
Layer.provideMerge(serverConfigLayer),
Layer.provideMerge(AnalyticsService.layerTest),
Layer.provideMerge(NodeServices.layer),
);

let scope: Scope.Closeable | null = null;
let webServer: ViteDevServer | null = null;
const previousCwd = process.cwd();

try {
scope = await Effect.runPromise(Scope.make("sequential"));
const runtimeServices = await Effect.runPromise(
Layer.build(dependenciesLayer).pipe(Scope.provide(scope)),
);
const httpServer = await Effect.runPromise(
createServer().pipe(Effect.provide(runtimeServices), Scope.provide(scope)),
);
const address = httpServer.address();
if (!address || typeof address === "string") {
throw new Error("Replay HTTP server did not expose a TCP address.");
}

process.chdir(webRootPath());
webServer = await createViteServer({
configFile: false,
...webViteConfig,
root: webRootPath(),
clearScreen: false,
define: {
...webViteConfig.define,
"import.meta.env.VITE_WS_URL": JSON.stringify(`ws://127.0.0.1:${address.port}`),
},
server: {
...webViteConfig.server,
host: "127.0.0.1",
port: 0,
strictPort: false,
hmr: {
protocol: "ws",
host: "127.0.0.1",
},
},
});
await webServer.listen();

const appUrl = webServer.resolvedUrls?.local[0];
if (!appUrl) {
throw new Error("Vite dev server did not expose a local URL.");
}

return {
appUrl,
openPage: async (browser, browserContextOptions) => {
const context = await browser.newContext(browserContextOptions);
const page = await context.newPage();
return { context, page };
},
dispose: async () => {
await webServer?.close().catch(() => undefined);
if (scope) {
await Effect.runPromise(Scope.close(scope, Exit.void)).catch(() => undefined);
}
process.chdir(previousCwd);
fs.rmSync(rootDir, { recursive: true, force: true });
},
};
} catch (error) {
await webServer?.close().catch(() => undefined);
if (scope) {
await Effect.runPromise(Scope.close(scope, Exit.void)).catch(() => undefined);
}
process.chdir(previousCwd);
fs.rmSync(rootDir, { recursive: true, force: true });
throw error;
}
}
3 changes: 3 additions & 0 deletions apps/server/integration/harness/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { createHarness } from "./harness.ts";
export type { Harness } from "./harness.ts";
export type { Fixture, Interaction } from "./types.ts";
Loading