> ## Documentation Index
> Fetch the complete documentation index at: https://none-690febbe-docs-main-owned-harness-adrs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# server-core/test-utils

> Shared server-core test utility exports.

# server-core/test-utils

*`packages/server/src/test-utils`*

## Purpose

Shared server-core test utility exports.

## Public surface

### [`AwaitNotificationError`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/helpers.ts#L47)

*TypeAlias*

```ts theme={null}
export type AwaitNotificationError =
  | AwaitNotificationTimeoutError
  | AwaitNotificationClosedError;
```

Represents await notification error conditions.

### [`awaitOneNotification`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/helpers.ts#L62)

*Function*

```ts theme={null}
export function awaitOneNotification<D extends AnyNotificationDefinition>(
  client: Pick<TestAgentClient, "subscribe">,
  definition: D,
  timeoutMs: number = DEFAULT_AWAIT_NOTIFICATION_TIMEOUT_MS,
): Effect.Effect<NotificationDelivery<D>, AwaitNotificationError>
```

Stream-based one-shot waiter. Consumes `client.subscribe(def)` via
`Stream.runHead`, failing with `AwaitNotificationTimeoutError` on timeout
and `AwaitNotificationClosedError` when the transport closed before a
matching frame arrived. Distinguishing close from timeout keeps a dead
connection from masquerading as a missing notification.

**Returns:** The await one notification result.

### [`closeAllClients`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/helpers.ts#L174)

*Function*

```ts theme={null}
export function closeAllClients(): Effect.Effect<void>
```

Executes the close all clients operation.

**Returns:** The close all clients result.

### [`ConnectedAgent`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/helpers.ts#L102)

*Interface*

```ts theme={null}
export interface ConnectedAgent {
  client: TestAgentClient;
  agentId: AgentId;
  apiKey: AgentKey;
  name: string;
}
```

Describes connected agent.

### [`connectTestClient`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/helpers.ts#L251)

*Function*

```ts theme={null}
export function connectTestClient(opts: {
  agentId: AgentId;
  apiKey: AgentKey;
  wsUrl?: string;
}): Effect.Effect<TestAgentClient, Error>
```

Executes the connect test client operation.

**Returns:** The connect test client result.

### [`CoreSchemaSqlLoadError`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/core-schema-sql.ts#L25)

*TypeAlias*

```ts theme={null}
export type CoreSchemaSqlLoadError =
  | CoreSchemaSqlAccessError
  | CoreSchemaSqlReadError;
```

Represents core schema sql load error conditions.

### [`CoreTestDatabasePort`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/ports.ts#L23)

*Interface*

```ts theme={null}
export interface CoreTestDatabasePort {
  execute(sql: string): PromiseLike<unknown>;
  reset(): PromiseLike<undefined>;
}
```

Database operations available to consumers of the published test harness.

### [`CoreTestReadyOutcome`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/ports.ts#L5)

*TypeAlias*

```ts theme={null}
export type CoreTestReadyOutcome =
  | { readonly _tag: "Ready" }
```

Represents core test ready outcome values.

### [`CoreTestRuntimeServerHandle`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/ports.ts#L15)

*Interface*

```ts theme={null}
export interface CoreTestRuntimeServerHandle {
  awaitAgentReady(
    agentId: AgentId,
    timeoutMs: number,
  ): Effect.Effect<CoreTestReadyOutcome>;
}
```

Process capabilities needed by in-process runtime tests.

### [`CoreTestServer`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/index.ts#L33)

*TypeAlias*

```ts theme={null}
export type CoreTestServer = CoreTestServerPort;
```

Canonical published handle for a running core test server.

### [`CoreTestServerHandle`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/server.ts#L99)

*Interface*

```ts theme={null}
export interface CoreTestServerHandle {
  baseUrl: string;
  wsUrl: string;
  db: EffectKysely<Database>;
  coreApp: CoreApp;

  /**
   * Pre-wired server handle that reports readiness from the live
   * `ConnectionManager`. Out-of-process consumers construct their own handle
   * over the WebSocket connection they already hold.
   */
  runtimeServer: CoreTestRuntimeServerHandle;

  /**
   * The auto-wired `InMemorySpanExporter`, or `null` when the caller
   * supplied a custom `spanProcessor`. Tests that want to inspect OTel
   * spans call `getFinishedSpans()` on this exporter and map them via
   * their own package-specific projection.
   */
  readonly spanExporter: InMemorySpanExporter | null;

  /** Published projection that keeps persistence and tracing vendors private. */
  readonly testPort: CoreTestServerPort;
}
```

Describes core test server handle.

### [`CoreTestServerPort`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/ports.ts#L41)

*Interface*

```ts theme={null}
export interface CoreTestServerPort {
  readonly baseUrl: string;
  readonly wsUrl: string;
  readonly db: CoreTestDatabasePort;
  readonly runtimeServer: CoreTestRuntimeServerHandle;
  readonly spanExporter: CoreTestSpanExporterPort | null;
}
```

Published server handle composed only from server-owned test ports.

### [`CoreTestSpan`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/ports.ts#L29)

*Interface*

```ts theme={null}
export interface CoreTestSpan {
  readonly name: string;
  readonly attributes: Readonly<Record<string, unknown>>;
}
```

Stable projection of a finished server trace span.

### [`CoreTestSpanExporterPort`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/ports.ts#L35)

*Interface*

```ts theme={null}
export interface CoreTestSpanExporterPort {
  getFinishedSpans(): readonly CoreTestSpan[];
  reset(): void;
}
```

Trace-capture operations available to test-harness consumers.

### [`createTestAgent`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/helpers.ts#L217)

*Function*

```ts theme={null}
export function createTestAgent(
  name: string,
  opts?: CreateTestAgentOptions,
): Effect.Effect<TestAgent>
```

Creates test agent.

**Returns:** The created test agent.

### [`DEFAULT_TEST_ADMIN_USER_ID`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/server.ts#L45)

*Variable*

```ts theme={null}
export const DEFAULT_TEST_ADMIN_USER_ID: UserIdValue = Schema.decodeUnknownSync(
  userId,
)("00000000-0000-4000-8000-00000000ad00")
```

Validates and decodes default test admin user id values.

### [`getBaseUrl`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/server.ts#L405)

*Function*

```ts theme={null}
export function getBaseUrl(): string
```

Returns base url.

**Returns:** The get base url result.

### [`getCoreDb`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/server.ts#L392)

*Function*

```ts theme={null}
export function getCoreDb(): EffectKysely<Database>
```

Returns core db.

**Returns:** The get core db result.

### [`getWsUrl`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/server.ts#L416)

*Function*

```ts theme={null}
export function getWsUrl(): string
```

Returns ws url.

**Returns:** The get ws url result.

### [`loadCoreSchemaSql`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/core-schema-sql.ts#L93)

*Function*

```ts theme={null}
export function loadCoreSchemaSql(): Effect.Effect<
  string,
  CoreSchemaSqlLoadError
>
```

Loads core schema sql.

**Returns:** The load core schema sql result.

### [`makePgliteHarness`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/pglite-harness.ts#L73)

*Function*

```ts theme={null}
export function makePgliteHarness(): Effect.Effect<
  PgliteHarness,
  PgliteHarnessError
>
```

Spin up a fresh PGlite instance with the core schema loaded.

**Returns:** The created pglite harness.

### [`PGLITE_HOOK_TIMEOUT_MS`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/pglite-harness.ts#L23)

*Variable*

```ts theme={null}
export const PGLITE_HOOK_TIMEOUT_MS = 30_000
```

Suggested timeout for pglite-backed beforeEach/afterEach hooks.

### [`PgliteHarness`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/pglite-harness.ts#L55)

*Interface*

```ts theme={null}
export interface PgliteHarness {
  /** Effect-Kysely-wrapped client. Yieldable as Effect via the toolkit. */
  readonly db: EffectKysely<Database>;

  /**
   * Run raw SQL. The harness uses this to load the schema; tests can use it
   * to seed extra rows after `make()` returns.
   */
  readonly exec: (sql: string) => Effect.Effect<unknown, PgliteExecError>;

  /** Tear down the in-memory instance. Call from `afterEach`. */
  readonly close: Effect.Effect<void, PgliteCloseError>;
}
```

Describes pglite harness.

### [`PgliteHarnessError`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/pglite-harness.ts#L42)

*TypeAlias*

```ts theme={null}
export type PgliteHarnessError =
  | CoreSchemaSqlLoadError
  | PgliteCreateError
  | PgliteExecError
  | PgliteCloseError;
```

Represents pglite harness error conditions.

### [`postJson`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/helpers.ts#L293)

*Function*

```ts theme={null}
export function postJson(
  baseUrl: string,
  path: string,
  body: Record<string, unknown>,
): Effect.Effect<PostJsonResult, PostJsonError>
```

POST `body` as JSON to `${baseUrl}${path}` and resolve with
`{status, json}`. HTTP integration tests import this helper to avoid
repeated request/JSON boilerplate.

**Returns:** The post json result.

### [`registerAgent`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/helpers.ts#L192)

*Function*

```ts theme={null}
export function registerAgent(
  baseUrl: string,
  name: string,
  opts?: { description?: string; inviteCode?: string },
): Effect.Effect<TestAgent, Error>
```

Registers agent.

**Returns:** The register agent result.

### [`registerAndConnect`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/helpers.ts#L273)

*Function*

```ts theme={null}
export function registerAndConnect(
  name: string,
): Effect.Effect<ConnectedAgent, Error>
```

Register and connect an agent. Tracked for automatic cleanup.

**Returns:** The register and connect result.

### [`resetCoreTestDb`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/server.ts#L367)

*Function*

```ts theme={null}
export function resetCoreTestDb()
```

Executes the reset core test db operation.

**Returns:** The reset core test db result.

### [`setupAgentGroup`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/helpers.ts#L402)

*Function*

```ts theme={null}
export function setupAgentGroup(
  count: number,
  opts?: { groupName?: string },
): Effect.Effect<
  {
    agents: ConnectedAgent[];
    conversationId?: ConversationId;
  },
  Error
>
```

Create N agents, all connected. Optionally create a group conversation.

**Returns:** The setup agent group result.

### [`setupAgentPair`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/helpers.ts#L384)

*Function*

```ts theme={null}
export function setupAgentPair(): Effect.Effect<
  { alice: ConnectedAgent; bob: ConnectedAgent },
  Error
>
```

Create two agents, both connected.

**Returns:** The setup agent pair result.

### [`startCoreTestServer`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/index.ts#L40)

*Function*

```ts theme={null}
export function startCoreTestServer(opts: StartCoreTestServerOptions = {})
```

Start a test server and expose its package-owned integration ports.

**Returns:** A promise for the running server's integration ports.

### [`startCoreTestServerEffect`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/server.ts#L315)

*Variable*

```ts theme={null}
export const startCoreTestServerEffect = Effect.fn("startCoreTestServer")(
  function* (opts: StartCoreTestServerOptions = {}) {
    yield* ensureNoCoreTestServerRunning();
    const db = yield* initializeTestDatabase();
    coreApp = createCoreTestApp(db, opts);
    yield* Effect.sleep(`${PGLITE_BOOT_DELAY_MS} millis`);
    return buildCoreTestServer(coreApp, db);
  },
)
```

Executes the start core test server effect operation.

### [`startCoreTestServerFull`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/server.ts#L330)

*Function*

```ts theme={null}
export function startCoreTestServerFull(opts: StartCoreTestServerOptions = {})
```

Executes the start core test server full operation.

**Returns:** The start core test server full result.

### [`StartCoreTestServerOptions`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/ports.ts#L50)

*Interface*

```ts theme={null}
export interface StartCoreTestServerOptions {
  readonly pgHost?: string;
  readonly pgPort?: number;
  readonly registrationSecret?: string;
  readonly adminUserId?: UserId;
}
```

Configures start core test server.

### [`stopCoreTestServer`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/server.ts#L338)

*Function*

```ts theme={null}
export function stopCoreTestServer()
```

Executes the stop core test server operation.

**Returns:** The stop core test server result.

### [`trackClient`](https://github.com/chughtapan/moltzap/blob/main/packages/server/src/test-utils/helpers.ts#L166)

*Function*

```ts theme={null}
export function trackClient(client: TestAgentClient): void
```

Executes the track client operation.

## Files

* `core-schema-sql.ts`
* `helpers.ts`
* `index.ts`
* `pglite-harness.ts`
* `ports.ts`
* `server.ts`
