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

TestProviderHost.ts

87 lines3.5 KB

1import { DEFAULT_SERVER_SETTINGS, type ServerSettings } from "@t3tools/contracts";
2import {
3 noCompatibilityPolicy,
4 type ProviderCompatibilityLookup,
5} from "@t3tools/provider-core/server/compatibility";
6import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
7import * as Context from "effect/Context";
8import * as Effect from "effect/Effect";
9import * as FileSystem from "effect/FileSystem";
10import * as Layer from "effect/Layer";
11import * as Option from "effect/Option";
12import * as Path from "effect/Path";
13import * as Ref from "effect/Ref";
14import * as Stream from "effect/Stream";
15​
16export interface TestProviderHostOptions {
17 readonly cwd?: string;
18 readonly settings?: ServerSettings;
19 readonly runBackgroundWork?: boolean;
20 readonly compatibility?: ProviderCompatibilityLookup;
22​
23export class TestProviderHostSettings extends Context.Service<
24 TestProviderHostSettings,
25 { readonly set: (settings: ServerSettings) => Effect.Effect<void> }
26>()("@t3tools/provider-testing/TestProviderHost/TestProviderHostSettings") {}
27​
28export const layer = (
29 options: TestProviderHostOptions = {},
30): Layer.Layer<
31 ProviderHost.ProviderHost | TestProviderHostSettings,
32 never,
33 FileSystem.FileSystem | Path.Path
34> =>
35 Layer.effectContext(
36 Effect.gen(function* () {
37 const fileSystem = yield* FileSystem.FileSystem;
38 const path = yield* Path.Path;
39 const baseDir = yield* fileSystem
40 .makeTempDirectoryScoped({ prefix: "t3-provider-host-" })
41 .pipe(Effect.orDie);
42 const stateDir = path.join(baseDir, "userdata");
43 const providerStatusCacheDir = path.join(baseDir, "caches");
44 const attachmentsDir = path.join(stateDir, "attachments");
45 for (const directory of [stateDir, providerStatusCacheDir, attachmentsDir]) {
46 yield* fileSystem.makeDirectory(directory, { recursive: true }).pipe(Effect.orDie);
47 }
48 const settings = yield* Ref.make(options.settings ?? DEFAULT_SERVER_SETTINGS);
49 const credentials = new Map<string, Uint8Array>();
50 const host = ProviderHost.ProviderHost.of({
51 compatibility: options.compatibility ?? noCompatibilityPolicy,
52 paths: {
53 cwd: options.cwd ?? process.cwd(),
54 baseDir,
55 stateDir,
56 providerStatusCacheDir,
57 attachmentsDir,
58 },
59 settings: {
60 get: Ref.get(settings),
61 withSnapshot: (use) => Ref.get(settings).pipe(Effect.flatMap(use)),
62 changes: Stream.empty,
63 subscribe: Effect.succeed(Stream.empty),
64 },
65 shouldRunBackgroundWork: Effect.fn("ProviderHost.shouldRunBackgroundWork")(function () {
66 return Effect.succeed(options.runBackgroundWork ?? true);
67 }),
68 resolveAttachmentPath: (attachment) => path.join(attachmentsDir, attachment.id),
69 credentials: Effect.fn("ProviderHost.credentials")(function (namespace, bindingId) {
70 return Effect.sync(() => {
71 const key = `${namespace}:${bindingId}`;
72 return {
73 binding: { owner: "t3" as const, key },
74 get: Effect.sync(() => Option.fromUndefinedOr(credentials.get(key))),
75 set: (value: Uint8Array) => Effect.sync(() => void credentials.set(key, value)),
76 remove: Effect.sync(() => void credentials.delete(key)),
77 };
78 });
79 }),
80 });
81 return Context.make(ProviderHost.ProviderHost, host).pipe(
82 Context.add(TestProviderHostSettings, {
83 set: (next) => Ref.set(settings, next),
84 }),
85 );
86 }),
87 );