@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 106 lines 3.23 KB
Raw
1 import { expect, it } from "@alchemy.run/test";
2 import * as Effect from "effect/Effect";
3 import * as FileSystem from "effect/FileSystem";
4 import * as Layer from "effect/Layer";
5 import * as PlatformError from "effect/PlatformError";
6 import * as Schema from "effect/Schema";
7 import { AuthError } from "@/Auth/AuthProvider.ts";
8 import { CredentialsStore, CredentialsStoreLive } from "@/Auth/Credentials.ts";
9
10 const failingDeleteLayer = CredentialsStoreLive.pipe(
11 Layer.provide(
12 FileSystem.layerNoop({
13 remove: (path) =>
14 Effect.fail(
15 PlatformError.systemError({
16 _tag: "PermissionDenied",
17 module: "FileSystem",
18 method: "remove",
19 pathOrDescriptor: path,
20 }),
21 ),
22 }),
23 ),
24 );
25
26 const TestCredentials = Schema.Struct({ type: Schema.Literal("apiKey"), apiKey: Schema.String });
27
28 it.effect(
29 "fails read with a reconfigure hint when the stored shape is wrong",
30 () =>
31 Effect.gen(function* () {
32 const store = yield* CredentialsStore;
33 const error = yield* store.read("test", "provider", TestCredentials).pipe(Effect.flip);
34
35 expect(error).toBeInstanceOf(AuthError);
36 expect(error.message).toContain("do not match the expected shape");
37 expect(error.message).toContain("--reconfigure");
38 }).pipe(
39 Effect.provide(
40 CredentialsStoreLive.pipe(
41 Layer.provide(
42 FileSystem.layerNoop({
43 // valid JSON, but not the declared credential shape
44 readFileString: () => Effect.succeed(JSON.stringify({ token: "not-the-shape" })),
45 }),
46 ),
47 ),
48 ),
49 ),
50 { tags: ["unit", "local"] },
51 );
52
53 it.effect(
54 "surfaces provider credential deletion failures",
55 () =>
56 Effect.gen(function* () {
57 const store = yield* CredentialsStore;
58 const error = yield* store.delete("test", "provider").pipe(Effect.flip);
59
60 expect(error).toBeInstanceOf(AuthError);
61 expect(error.message).toContain("Could not delete credentials at");
62 }).pipe(Effect.provide(failingDeleteLayer)),
63 { tags: ["unit", "local"] },
64 );
65
66 it.effect(
67 "surfaces profile credential deletion failures",
68 () =>
69 Effect.gen(function* () {
70 const store = yield* CredentialsStore;
71 const error = yield* store.deleteProfile("test").pipe(Effect.flip);
72
73 expect(error).toBeInstanceOf(AuthError);
74 expect(error.message).toBe("Could not delete credentials for profile 'test'.");
75 }).pipe(Effect.provide(failingDeleteLayer)),
76 { tags: ["unit", "local"] },
77 );
78
79 it.effect(
80 "ignores missing credential files",
81 () =>
82 Effect.gen(function* () {
83 const store = yield* CredentialsStore;
84 yield* store.delete("test", "provider");
85 yield* store.deleteProfile("test");
86 }).pipe(
87 Effect.provide(
88 CredentialsStoreLive.pipe(
89 Layer.provide(
90 FileSystem.layerNoop({
91 remove: (path) =>
92 Effect.fail(
93 PlatformError.systemError({
94 _tag: "NotFound",
95 module: "FileSystem",
96 method: "remove",
97 pathOrDescriptor: path,
98 }),
99 ),
100 }),
101 ),
102 ),
103 ),
104 ),
105 { tags: ["unit", "local"] },
106 );