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

BrowserImport.test.ts

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