83 lines · 2.9 KB · TypeScript
RawDownload
import { assert, describe, it } from "@effect/vitest";import * as NodeServices from "@effect/platform-node/NodeServices";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";import { beforeEach, vi } from "vite-plus/test"; const { fromPartition, sessions } = vi.hoisted(() => ({  fromPartition: vi.fn(),  sessions: new Map<    string,    {      readonly clearCache: ReturnType<typeof vi.fn>;      readonly clearStorageData: ReturnType<typeof vi.fn>;      readonly getUserAgent: ReturnType<typeof vi.fn>;      readonly setPermissionRequestHandler: ReturnType<typeof vi.fn>;      readonly setUserAgent: ReturnType<typeof vi.fn>;    }  >(),})); vi.mock("electron", () => ({  session: {    fromPartition,  },})); import * as BrowserSession from "./BrowserSession.ts"; const layer = BrowserSession.layer.pipe(Layer.provide(NodeServices.layer)); describe("BrowserSession", () => {  beforeEach(() => {    sessions.clear();    fromPartition.mockReset();    fromPartition.mockImplementation((partition: string) => {      const browserSession = {        clearCache: vi.fn(() => Promise.resolve()),        clearStorageData: vi.fn(() => Promise.resolve()),        getUserAgent: vi.fn(() => "Mozilla/5.0 Electron/41.5.0 t3code/0.0.27"),        setPermissionRequestHandler: vi.fn(),        setUserAgent: vi.fn(),      };      sessions.set(partition, browserSession);      return browserSession;    });  });   it.effect("derives deterministic partitions and memoizes sessions", () =>    Effect.gen(function* () {      const browserSessions = yield* BrowserSession.BrowserSession;       const partition = yield* browserSessions.getPartition("scope-a");      const first = yield* browserSessions.getSession("scope-a");      const second = yield* browserSessions.getSession("scope-a");       assert.strictEqual(partition, "persist:t3code-preview-f051bb2c68cb7b2fe969");      assert.strictEqual(first, second);      assert.strictEqual(fromPartition.mock.calls.length, 1);    }).pipe(Effect.provide(layer)),  );   it.effect("clears storage and cache for every created session", () =>    Effect.gen(function* () {      const browserSessions = yield* BrowserSession.BrowserSession;      yield* browserSessions.getSession("scope-a");      yield* browserSessions.getSession("scope-b");       yield* browserSessions.clearCookies();      yield* browserSessions.clearCache();       assert.strictEqual(sessions.size, 2);      for (const browserSession of sessions.values()) {        assert.strictEqual(browserSession.clearStorageData.mock.calls.length, 1);        assert.deepEqual(browserSession.clearStorageData.mock.calls[0], [          {            storages: ["cookies", "localstorage", "indexdb", "websql", "serviceworkers"],          },        ]);        assert.strictEqual(browserSession.clearCache.mock.calls.length, 1);      }    }).pipe(Effect.provide(layer)),  );});