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

BrowserImport.test.ts

195 lines6.8 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import * as HostProcess from "@t3tools/shared/HostProcess";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Fiber from "effect/Fiber";
7import * as Layer from "effect/Layer";
8import * as Ref from "effect/Ref";
9​
10import * as BrowserSession from "../BrowserSession.ts";
11import * as BrowserImport from "./BrowserImport.ts";
12import { BROWSER_IMPORT_SOURCES, sourcePathContext } from "./Sources.ts";
13import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
14​
15const helium = BROWSER_IMPORT_SOURCES.find((source) => source.id === "helium")!;
16​
17const cookie = {
18 url: "https://rejected.example/path",
19 name: "session",
20 value: "value",
21 domain: undefined,
22 path: "/",
23 secure: true,
24 httpOnly: true,
25 expirationDate: undefined,
26 sameSite: "lax" as const,
27};
28​
29const layerRejectedBeforeSession = Layer.succeed(
30 BrowserSession.BrowserSession,
31 BrowserSession.BrowserSession.of({
32 getPartition: () => Effect.die("getPartition must not be reached"),
33 isPartition: () => false,
34 getSession: () => Effect.die("getSession must not be reached"),
35 clearCookies: () => Effect.die("clearCookies must not be reached"),
36 clearCache: () => Effect.die("clearCache must not be reached"),
37 }),
38);
39​
40const withImporter = Effect.fnUntraced(function* () {
41 const fileSystem = yield* FileSystem.FileSystem;
42 const home = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-import-" });
43 const layerEnvironment = Layer.succeed(HostProcess.Environment, { HOME: home });
44 const context = yield* sourcePathContext.pipe(
45 Effect.provideService(HostProcess.Environment, { HOME: home }),
46 Effect.provideService(HostProcess.Platform, "darwin"),
47 );
48 const root = helium.userDataDirectory(context);
49 if (root === undefined) throw new Error("Helium has no macOS user-data directory");
50 yield* fileSystem.makeDirectory(`${root}/Default`, { recursive: true });
51 // The cookie database is what marks a source as installed, so a fixture
52 // without one is reported as absent before any other check runs.
53 yield* fileSystem.writeFileString(`${root}/Default/Cookies`, "db");
54​
55 const importer = yield* BrowserImport.BrowserImport.pipe(
56 Effect.provide(
57 BrowserImport.layer.pipe(
58 Layer.provide(layerRejectedBeforeSession),
59 Layer.provide(layerEnvironment),
60 Layer.provide(Layer.succeed(HostProcess.Platform, "darwin")),
61 Layer.provide(Layer.succeed(HostProcess.ExecutablePath, "/Applications/T3 Code.app")),
62 Layer.provide(NodeServices.layer),
63 ),
64 ),
65 );
66 return { importer, home, root };
67});
68​
69describe("BrowserImport.importCookies", () => {
70 it.effect("rejects a source profile the browser never reported", () =>
71 Effect.gen(function* () {
72 const fileSystem = yield* FileSystem.FileSystem;
73 const { importer, home } = yield* withImporter();
74​
75 yield* fileSystem.makeDirectory(`${home}/secrets`, { recursive: true });
76 yield* fileSystem.writeFileString(`${home}/secrets/Cookies`, "not-a-db");
77​
78 const error = yield* importer
79 .importCookies({
80 input: {
81 sourceId: "helium",
82 sourceProfileDirectory: "../../../../secrets",
83 targetProfileId: "default",
84 },
85 scope: "persist:t3code-preview-test",
86 persistent: true,
87 })
88 .pipe(Effect.flip);
89​
90 assert.instanceOf(error, BrowserImport.BrowserImportFailedError);
91 assert.equal(error.reason, "unknownSourceProfile");
92 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
93 );
94​
95 it.effect.skipIf(!symlinksSupported)(
96 "refuses to import while the source browser holds its profile",
97 () =>
98 Effect.gen(function* () {
99 const fileSystem = yield* FileSystem.FileSystem;
100 const { importer, root } = yield* withImporter();
101 // The lock Chromium leaves while it is running, dangling target and
102 // all. This must stop the import before it ever asks the keychain.
103 yield* fileSystem.symlink("host-that-does-not-exist-1234", `${root}/SingletonLock`);
104​
105 const error = yield* importer
106 .importCookies({
107 input: {
108 sourceId: "helium",
109 sourceProfileDirectory: "Default",
110 targetProfileId: "default",
111 },
112 scope: "persist:t3code-preview-test",
113 persistent: true,
114 })
115 .pipe(Effect.flip);
116​
117 assert.equal(error.reason, "browserRunning");
118 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
119 );
120});
121​
122describe("BrowserImport.writeCookies", () => {
123 it.effect("counts a rejected cookie and its domain as skipped", () =>
124 Effect.gen(function* () {
125 let flushes = 0;
126 const result = yield* BrowserImport.writeCookies(
127 {
128 cookies: {
129 set: () => Promise.reject(new Error("fixture rejection")),
130 flushStore: () => {
131 flushes += 1;
132 return Promise.resolve();
133 },
134 },
135 },
136 { cookies: [cookie], undecryptable: 0, undecryptableHosts: [] },
137 );
138​
139 assert.deepEqual(result, {
140 imported: 0,
141 skipped: 1,
142 skippedDomains: ["rejected.example"],
143 });
144 assert.equal(flushes, 0);
145 }),
146 );
147​
148 it.effect("flushes the store after writing, and reports success if the flush fails", () =>
149 Effect.gen(function* () {
150 const events: Array<string> = [];
151 const result = yield* BrowserImport.writeCookies(
152 {
153 cookies: {
154 set: () => {
155 events.push("set");
156 return Promise.resolve();
157 },
158 flushStore: () => {
159 events.push("flush");
160 return Promise.reject(new Error("fixture flush failure"));
161 },
162 },
163 },
164 { cookies: [cookie, cookie], undecryptable: 0, undecryptableHosts: [] },
165 );
166​
167 assert.deepEqual(events, ["set", "set", "flush"]);
168 assert.deepEqual(result, { imported: 2, skipped: 0, skippedDomains: [] });
169 }),
170 );
171​
172 it.effect("propagates interruption while writing a cookie", () =>
173 Effect.gen(function* () {
174 const write = BrowserImport.writeCookies(
175 {
176 cookies: {
177 set: () => new Promise<void>(() => {}),
178 flushStore: () => Promise.resolve(),
179 },
180 },
181 { cookies: [cookie], undecryptable: 0, undecryptableHosts: [] },
182 );
183​
184 const interrupted = yield* Ref.make(false);
185 const fiber = yield* write.pipe(
186 Effect.onInterrupt(() => Ref.set(interrupted, true)),
187 Effect.forkChild,
188 );
189 yield* Effect.yieldNow;
190 yield* Fiber.interrupt(fiber);
191​
192 assert.isTrue(yield* Ref.get(interrupted));
193 }),
194 );
195});