Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

host.ts

54 lines2.3 KB

1/**
2 * A `ProviderHost` for driver and adapter tests. Its directories live in a
3 * scoped temp directory, settings are fixed, and background work always runs
4 * unless the test says otherwise.
5 *
6 * @module provider-testing/host
7 */
8import { DEFAULT_SERVER_SETTINGS, type ServerSettings } from "@t3tools/contracts";
9import { ProviderHost } from "@t3tools/provider-core/server/ProviderHost";
10import * as Effect from "effect/Effect";
11import * as FileSystem from "effect/FileSystem";
12import * as Layer from "effect/Layer";
13import * as Path from "effect/Path";
14import * as Stream from "effect/Stream";
15​
16export interface TestProviderHostOptions {
17 /** Session fallback cwd. Defaults to the test process cwd. */
18 readonly cwd?: string;
19 readonly settings?: ServerSettings;
20 /** Whether background work such as status probes may run. Defaults to `true`. */
21 readonly runBackgroundWork?: boolean;
23​
24export const layerTestProviderHost = (
25 options: TestProviderHostOptions = {},
26): Layer.Layer<ProviderHost, never, FileSystem.FileSystem | Path.Path> =>
27 Layer.effect(
28 ProviderHost,
29 Effect.gen(function* () {
30 const fileSystem = yield* FileSystem.FileSystem;
31 const path = yield* Path.Path;
32 const baseDir = yield* fileSystem
33 .makeTempDirectoryScoped({ prefix: "t3-provider-host-" })
34 .pipe(Effect.orDie);
35 const stateDir = path.join(baseDir, "userdata");
36 const providerStatusCacheDir = path.join(baseDir, "caches");
37 const attachmentsDir = path.join(stateDir, "attachments");
38 for (const directory of [stateDir, providerStatusCacheDir, attachmentsDir]) {
39 yield* fileSystem.makeDirectory(directory, { recursive: true }).pipe(Effect.orDie);
40 }
41 const settings = options.settings ?? DEFAULT_SERVER_SETTINGS;
42 return ProviderHost.of({
43 paths: { cwd: options.cwd ?? process.cwd(), baseDir, stateDir, providerStatusCacheDir },
44 settings: {
45 get: Effect.succeed(settings),
46 changes: Stream.empty,
47 subscribe: Effect.succeed(Stream.empty),
48 },
49 shouldRunBackgroundWork: () => Effect.succeed(options.runBackgroundWork ?? true),
50 // Tests store attachments flat under the attachments directory by id.
51 resolveAttachmentPath: (attachment) => path.join(attachmentsDir, attachment.id),
52 });
53 }),
54 );