Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

7.1 KB
Raw Blame
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";

let mockLocalStorage: Record<string, string> = {};
let mockSessionStorage: Record<string, string> = {};
let dispatchedEvents: Array<{ type: string; detail: any }> = [];

function mockWindow() {
  global.window = {
    location: { search: "", origin: "http://localhost" },
    localStorage: {
      getItem: (key: string) => mockLocalStorage[key] ?? null,
      setItem: (key: string, value: string) => {
        mockLocalStorage[key] = value;
      },
      removeItem: (key: string) => {
        delete mockLocalStorage[key];
      },
    },
    sessionStorage: {
      getItem: (key: string) => mockSessionStorage[key] ?? null,
      setItem: (key: string, value: string) => {
        mockSessionStorage[key] = value;
      },
      removeItem: (key: string) => {
        delete mockSessionStorage[key];
      },
    },
    dispatchEvent: (event: any) => {
      dispatchedEvents.push({ type: event.type, detail: event.detail });
      return true;
    },
    addEventListener: () => {},
    removeEventListener: () => {},
  } as any;
}

mockWindow();

import * as settingsContext from "../features/settings/store/SettingsStore";
import {
  CODE_BLOCK_SHOW_LINE_NUMBERS_STORAGE_KEY,
  CODE_BLOCK_SETTINGS_EVENT,
  CODE_BLOCK_THEME_STORAGE_KEY,
  CODE_BLOCK_WRAP_LONG_LINES_STORAGE_KEY,
  readStoredCodeBlockShowLineNumbers,
  readStoredCodeBlockTheme,
  readStoredCodeBlockWrapLongLines,
} from "../context/app-shell-storage";

const settingsContextPath = path.join(
  process.cwd(),
  "features",
  "settings",
  "store",
  "SettingsStore.tsx",
);

function readSettingsContextSource() {
  return fs.readFileSync(settingsContextPath, "utf8");
}

test("settings-context: syncLoadedCodeBlockSettingsToAppShell writes backend-loaded values into app-shell storage", () => {
  const sync = (settingsContext as any).syncLoadedCodeBlockSettingsToAppShell;

  assert.equal(typeof sync, "function");

  mockLocalStorage = {};
  dispatchedEvents = [];

  const normalized = sync({
    code_block_theme: "dracula",
    code_block_show_line_numbers: true,
    code_block_wrap_long_lines: true,
  });

  assert.equal(readStoredCodeBlockTheme(), "dracula");
  assert.equal(readStoredCodeBlockShowLineNumbers(), true);
  assert.equal(readStoredCodeBlockWrapLongLines(), true);
  assert.deepEqual(normalized, {
    code_block_theme: "dracula",
    code_block_show_line_numbers: true,
    code_block_wrap_long_lines: true,
  });
  assert.equal(
    dispatchedEvents.filter((event) => event.type === CODE_BLOCK_SETTINGS_EVENT)
      .length,
    3,
  );
});

test("settings-context: persistUiSettingsPatch sends only the changed code-block field", async () => {
  const persist = (settingsContext as any).persistUiSettingsPatch;

  assert.equal(typeof persist, "function");

  let capturedInput: unknown;
  let capturedInit: RequestInit | undefined;

  await persist(
    {
      code_block_theme: "dracula",
    },
    async (input: RequestInfo | URL, init?: RequestInit) => {
      capturedInput = input;
      capturedInit = init;
      return { ok: true } as Response;
    },
  );

  assert.match(String(capturedInput), /\/api\/settings\/ui\?dt_workspace=$/);
  assert.equal(capturedInit?.method, "PUT");
  assert.deepEqual(JSON.parse(String(capturedInit?.body)), {
    code_block_theme: "dracula",
  });
});

test("settings-context: persistUiSettingsPatch can save theme without sending code-block fields", async () => {
  const persist = (settingsContext as any).persistUiSettingsPatch;

  assert.equal(typeof persist, "function");

  let capturedInit: RequestInit | undefined;

  await persist(
    {
      theme: "dark",
    },
    async (_input: RequestInfo | URL, init?: RequestInit) => {
      capturedInit = init;
      return { ok: true } as Response;
    },
  );

  assert.deepEqual(JSON.parse(String(capturedInit?.body)), {
    theme: "dark",
  });
});

test("settings-context: syncLoadedCodeBlockSettingsToAppShell handles string boolean representations from backend", () => {
  const sync = (settingsContext as any).syncLoadedCodeBlockSettingsToAppShell;

  mockLocalStorage = {};

  const result = sync({
    code_block_theme: "dracula",
    code_block_show_line_numbers: "True",
    code_block_wrap_long_lines: "true",
  });

  assert.equal(
    result.code_block_show_line_numbers,
    true,
    "String 'True' should be normalized to boolean true",
  );
  assert.equal(
    result.code_block_wrap_long_lines,
    true,
    "String 'true' should be normalized to boolean true",
  );
  assert.equal(
    readStoredCodeBlockShowLineNumbers(),
    true,
    "Line numbers should be stored as true",
  );
  assert.equal(
    readStoredCodeBlockWrapLongLines(),
    true,
    "Wrap long lines should be stored as true",
  );
});

test("settings-context: backend values override localStorage values during sync", () => {
  const sync = (settingsContext as any).syncLoadedCodeBlockSettingsToAppShell;

  // Simulate localStorage having different (old/stale) values
  mockLocalStorage = {
    [CODE_BLOCK_SHOW_LINE_NUMBERS_STORAGE_KEY]: "false",
    [CODE_BLOCK_WRAP_LONG_LINES_STORAGE_KEY]: "false",
    [CODE_BLOCK_THEME_STORAGE_KEY]: "oneDark",
  };

  // Backend returns true values
  const result = sync({
    code_block_theme: "dracula",
    code_block_show_line_numbers: true,
    code_block_wrap_long_lines: true,
  });

  // Verify backend values win
  assert.equal(
    result.code_block_show_line_numbers,
    true,
    "Backend true should override localStorage false",
  );
  assert.equal(
    result.code_block_wrap_long_lines,
    true,
    "Backend true should override localStorage false",
  );
  assert.equal(
    result.code_block_theme,
    "dracula",
    "Backend theme should override localStorage theme",
  );

  // Verify localStorage is updated to backend values
  assert.equal(
    readStoredCodeBlockShowLineNumbers(),
    true,
    "localStorage should be updated to backend true",
  );
  assert.equal(
    readStoredCodeBlockWrapLongLines(),
    true,
    "localStorage should be updated to backend true",
  );
  assert.equal(
    readStoredCodeBlockTheme(),
    "dracula",
    "localStorage theme should be updated to backend value",
  );

  // This test would fail if a post-mount effect re-read from localStorage,
  // as it would overwrite the backend-loaded true values with the old false values.
});

test("settings-context: routes code-block state through the AppShell single source", () => {
  const source = readSettingsContextSource();

  assert.match(
    source,
    /useAppShell/,
    "SettingsContext should read and write code-block state via AppShellContext.",
  );
  // Backend-loaded values reach AppShell through the storage/event sync helper.
  assert.match(
    source,
    /syncLoadedCodeBlockSettingsToAppShell\(\s*payload\.ui,?\s*\)/,
    "loadSettings should push backend-loaded code-block values into the AppShell source.",
  );
  // User edits stay in the global draft until Apply publishes to AppShell.
  assert.match(source, /stageUi\(\{ code_block_show_line_numbers \}\)/);
  assert.match(source, /stageUi\(\{ code_block_wrap_long_lines \}\)/);
  assert.match(source, /syncLoadedCodeBlockSettingsToAppShell\(ui\)/);
});