Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions tests/e2e/settings-toggles.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { test, expect } from "@playwright/test";

test.describe("Settings Toggles", () => {
test.describe("Debug Mode Toggle", () => {
test("should toggle debug mode on and off", async ({ page }) => {
await page.goto("/dashboard/settings");
await page.waitForLoadState("networkidle");
await page.click("text=Advanced");

const debugToggle = page.locator('[aria-label*="debug" i], [data-testid*="debug" i]').first();

if (await debugToggle.isVisible({ timeout: 3000 }).catch(() => false)) {
const initialState = await debugToggle.isChecked();
await debugToggle.click();
await page.waitForTimeout(500);
const newState = await debugToggle.isChecked();
expect(newState).not.toBe(initialState);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Avoid using conditional logic (if) to wrap assertions in E2E tests. If the element is not found, the test should fail to indicate a regression or UI change. Additionally, replace page.waitForTimeout with Playwright's web-first assertions like toBeChecked(), which automatically wait for the expected state and are more resilient to timing issues.

      await expect(debugToggle).toBeVisible();
      const initialState = await debugToggle.isChecked();
      await debugToggle.click();
      await expect(debugToggle).toBeChecked({ checked: !initialState });

});
});

test.describe("Sidebar Visibility Toggle", () => {
test("should toggle sidebar items visibility", async ({ page }) => {
await page.goto("/dashboard/settings");
await page.waitForLoadState("networkidle");
await page.click("text=General");

const sidebarToggle = page
.locator('[aria-label*="sidebar" i], [data-testid*="sidebar" i]')
.first();

if (await sidebarToggle.isVisible({ timeout: 3000 }).catch(() => false)) {
const initialState = await sidebarToggle.isChecked();
await sidebarToggle.click();
await page.waitForTimeout(500);
const newState = await sidebarToggle.isChecked();
expect(newState).not.toBe(initialState);
}
});
});

test.describe("Settings Persistence", () => {
test("should persist debug mode after page reload", async ({ page }) => {
await page.goto("/dashboard/settings");
await page.waitForLoadState("networkidle");
await page.click("text=Advanced");

const debugToggle = page.locator('[aria-label*="debug" i], [data-testid*="debug" i]').first();

if (await debugToggle.isVisible({ timeout: 3000 }).catch(() => false)) {
const wasChecked = await debugToggle.isChecked();
await debugToggle.click();
await page.waitForTimeout(500);
await page.reload();
await page.waitForLoadState("networkidle");
await page.click("text=Advanced");
const isChecked = await debugToggle.isChecked();
expect(isChecked).not.toBe(wasChecked);
}
});
});
});
67 changes: 67 additions & 0 deletions tests/unit/settings-api.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { describe, test } from "node:test";
import assert from "node:assert/strict";
import { getSettings, updateSettings } from "../../src/lib/localDb.ts";

describe("Settings API - debugMode and hiddenSidebarItems", () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The unit tests modify the global settings state in the database without resetting it between tests. This can lead to test interference and non-deterministic results. Consider adding a beforeEach hook to reset the settings to a default state before each test.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: Tests modify global settings state without resetting between tests - this can cause test interference and non-deterministic results. Consider adding a beforeEach hook to reset settings to a default state before each test.

describe("debugMode", () => {
test("PATCH with debugMode=true succeeds", async () => {
const result = await updateSettings({ debugMode: true });
assert.strictEqual(result.ok, true, "updateSettings should return ok: true");

const settings = getSettings();
assert.strictEqual(settings.debugMode, true, "debugMode should be true");
Comment on lines +8 to +12

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This test has two correctness issues that will cause it to fail:

  1. updateSettings returns the updated settings object directly (as seen in src/app/api/settings/route.ts), so result.ok will be undefined and the assertion will fail.
  2. getSettings() is an asynchronous function but is called here without await. This returns a Promise, making settings.debugMode undefined.

This pattern is repeated throughout the file and should be corrected in all test cases.

Suggested change
const result = await updateSettings({ debugMode: true });
assert.strictEqual(result.ok, true, "updateSettings should return ok: true");
const settings = getSettings();
assert.strictEqual(settings.debugMode, true, "debugMode should be true");
const result = await updateSettings({ debugMode: true });
assert.strictEqual(result.debugMode, true, "updateSettings should return the updated settings");
const settings = await getSettings();
assert.strictEqual(settings.debugMode, true, "debugMode should be true");

});

test("PATCH with debugMode=false succeeds", async () => {
const result = await updateSettings({ debugMode: false });
assert.strictEqual(result.ok, true, "updateSettings should return ok: true");

const settings = getSettings();
assert.strictEqual(settings.debugMode, false, "debugMode should be false");
});
});

describe("hiddenSidebarItems", () => {
test("PATCH with hiddenSidebarItems=['translator'] succeeds", async () => {
const result = await updateSettings({ hiddenSidebarItems: ["translator"] });
assert.strictEqual(result.ok, true, "updateSettings should return ok: true");

const settings = getSettings();
assert.deepStrictEqual(
settings.hiddenSidebarItems,
["translator"],
"hiddenSidebarItems should contain translator"
);
});

test("PATCH with empty hiddenSidebarItems succeeds", async () => {
const result = await updateSettings({ hiddenSidebarItems: [] });
assert.strictEqual(result.ok, true, "updateSettings should return ok: true");

const settings = getSettings();
assert.deepStrictEqual(
settings.hiddenSidebarItems,
[],
"hiddenSidebarItems should be empty array"
);
});
});

describe("combined updates", () => {
test("PATCH with both debugMode and hiddenSidebarItems succeeds", async () => {
const result = await updateSettings({
debugMode: true,
hiddenSidebarItems: ["translator"],
});
assert.strictEqual(result.ok, true, "updateSettings should return ok: true");

const settings = getSettings();
assert.strictEqual(settings.debugMode, true, "debugMode should be true");
assert.deepStrictEqual(
settings.hiddenSidebarItems,
["translator"],
"hiddenSidebarItems should be updated"
);
});
});
});