Files
hikari-desktop/src/lib/notifications/notifications.test.ts
T
hikari fa906684c2
Security Scan and Upload / Security & DefectDojo Upload (push) Successful in 57s
CI / Lint & Test (push) Has been cancelled
CI / Build Linux (push) Has been cancelled
CI / Build Windows (cross-compile) (push) Has been cancelled
feat: multiple UI improvements, font settings, and memory file display names (#175)
## Summary

- **fix**: `show_thinking_blocks` setting now persists across sessions — it was defined on the TypeScript side but missing from the Rust `HikariConfig` struct, so serde silently dropped it on every save/load
- **feat**: Tool calls are now rendered as collapsible blocks matching the Extended Thinking block aesthetic, replacing the old inline dropdown approach
- **feat**: Add configurable max output tokens setting
- **feat**: Use random creative names for conversation tabs
- **test**: Significantly expanded frontend unit test coverage
- **docs**: Require tests for all changes in CLAUDE.md
- **feat**: Allow users to specify a custom terminal font (Closes #176)
- **feat**: Display friendly names for memory files derived from the first heading (Closes #177)
- **feat**: Add custom UI font support for the app chrome (buttons, labels, tabs)
- **fix**: Apply custom UI font to the full app interface — `.app-container` was hardcoded, blocking inheritance from `body`; also renamed "Custom Font" to "Custom Terminal Font" for clarity

 This PR was created with help from Hikari~ 🌸

Reviewed-on: #175
Co-authored-by: Hikari <hikari@nhcarrigan.com>
Co-committed-by: Hikari <hikari@nhcarrigan.com>
2026-03-03 20:21:58 -08:00

382 lines
12 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { NotificationType, NOTIFICATION_SOUNDS, type NotificationSound } from "./types";
// Mock HTMLAudioElement for soundPlayer tests
class MockAudioElement {
src: string = "";
preload: string = "";
volume: number = 1;
constructor(src?: string) {
if (src) this.src = src;
}
cloneNode(): MockAudioElement {
const clone = new MockAudioElement(this.src);
clone.volume = this.volume;
return clone;
}
async play(): Promise<void> {
return Promise.resolve();
}
}
// Store original Audio before mocking
const OriginalAudio = globalThis.Audio;
describe("notifications", () => {
describe("NotificationType enum", () => {
it("has SUCCESS type", () => {
expect(NotificationType.SUCCESS).toBe("success");
});
it("has ERROR type", () => {
expect(NotificationType.ERROR).toBe("error");
});
it("has PERMISSION type", () => {
expect(NotificationType.PERMISSION).toBe("permission");
});
it("has CONNECTION type", () => {
expect(NotificationType.CONNECTION).toBe("connection");
});
it("has TASK_START type", () => {
expect(NotificationType.TASK_START).toBe("task_start");
});
it("has ACHIEVEMENT type", () => {
expect(NotificationType.ACHIEVEMENT).toBe("achievement");
});
it("has exactly 7 notification types", () => {
const types = Object.values(NotificationType);
expect(types.length).toBe(7);
});
it("has COST_ALERT type", () => {
expect(NotificationType.COST_ALERT).toBe("cost_alert");
});
});
describe("NOTIFICATION_SOUNDS constant", () => {
it("has sounds for all notification types", () => {
Object.values(NotificationType).forEach((type) => {
expect(NOTIFICATION_SOUNDS[type]).toBeDefined();
});
});
it("each sound has required properties", () => {
Object.values(NOTIFICATION_SOUNDS).forEach((sound) => {
expect(sound.type).toBeDefined();
expect(sound.filename).toBeDefined();
expect(sound.phrase).toBeDefined();
expect(typeof sound.filename).toBe("string");
expect(typeof sound.phrase).toBe("string");
expect(sound.filename.endsWith(".mp3")).toBe(true);
});
});
it("SUCCESS sound has correct properties", () => {
const sound = NOTIFICATION_SOUNDS[NotificationType.SUCCESS];
expect(sound.type).toBe(NotificationType.SUCCESS);
expect(sound.filename).toBe("im-done.mp3");
expect(sound.phrase).toBe("I'm done!");
expect(sound.volume).toBe(0.7);
});
it("ERROR sound has correct properties", () => {
const sound = NOTIFICATION_SOUNDS[NotificationType.ERROR];
expect(sound.type).toBe(NotificationType.ERROR);
expect(sound.filename).toBe("oh-no.mp3");
expect(sound.phrase).toBe("Oh no...");
expect(sound.volume).toBe(0.8);
});
it("PERMISSION sound has correct properties", () => {
const sound = NOTIFICATION_SOUNDS[NotificationType.PERMISSION];
expect(sound.type).toBe(NotificationType.PERMISSION);
expect(sound.filename).toBe("access-please.mp3");
expect(sound.phrase).toBe("Access please!");
expect(sound.volume).toBe(0.9);
});
it("CONNECTION sound has correct properties", () => {
const sound = NOTIFICATION_SOUNDS[NotificationType.CONNECTION];
expect(sound.type).toBe(NotificationType.CONNECTION);
expect(sound.filename).toBe("connected.mp3");
expect(sound.phrase).toBe("Connected!");
expect(sound.volume).toBe(0.7);
});
it("TASK_START sound has correct properties", () => {
const sound = NOTIFICATION_SOUNDS[NotificationType.TASK_START];
expect(sound.type).toBe(NotificationType.TASK_START);
expect(sound.filename).toBe("working-on-it.mp3");
expect(sound.phrase).toBe("Working on it!");
expect(sound.volume).toBe(0.6);
});
it("ACHIEVEMENT sound has correct properties", () => {
const sound = NOTIFICATION_SOUNDS[NotificationType.ACHIEVEMENT];
expect(sound.type).toBe(NotificationType.ACHIEVEMENT);
expect(sound.filename).toBe("achievement.mp3");
expect(sound.phrase).toBe("Achievement Get~!");
expect(sound.volume).toBe(0.8);
});
it("all volumes are within valid range (0-1)", () => {
Object.values(NOTIFICATION_SOUNDS).forEach((sound) => {
if (sound.volume !== undefined) {
expect(sound.volume).toBeGreaterThanOrEqual(0);
expect(sound.volume).toBeLessThanOrEqual(1);
}
});
});
});
describe("NotificationSound interface", () => {
it("can create a valid notification sound object", () => {
const sound: NotificationSound = {
type: NotificationType.SUCCESS,
filename: "test-sound.mp3",
phrase: "Test phrase",
volume: 0.5,
};
expect(sound.type).toBe(NotificationType.SUCCESS);
expect(sound.filename).toBe("test-sound.mp3");
expect(sound.phrase).toBe("Test phrase");
expect(sound.volume).toBe(0.5);
});
it("volume is optional", () => {
const sound: NotificationSound = {
type: NotificationType.ERROR,
filename: "error.mp3",
phrase: "Error occurred",
};
expect(sound.volume).toBeUndefined();
});
});
describe("SoundPlayer class", () => {
beforeEach(() => {
// Mock Audio constructor
globalThis.Audio = MockAudioElement as unknown as typeof Audio;
});
afterEach(() => {
// Restore original Audio
globalThis.Audio = OriginalAudio;
vi.resetModules();
});
it("can import soundPlayer singleton", async () => {
const { soundPlayer } = await import("./soundPlayer");
expect(soundPlayer).toBeDefined();
});
it("setEnabled changes enabled state", async () => {
const { soundPlayer } = await import("./soundPlayer");
soundPlayer.setEnabled(true);
expect(soundPlayer.isEnabled()).toBe(true);
soundPlayer.setEnabled(false);
expect(soundPlayer.isEnabled()).toBe(false);
});
it("starts disabled by default", async () => {
// Need to reimport to get fresh instance behavior
// But since it's a singleton, we just test the method
const { soundPlayer } = await import("./soundPlayer");
// Reset to default state
soundPlayer.setEnabled(false);
expect(soundPlayer.isEnabled()).toBe(false);
});
it("setGlobalVolume clamps values to 0-1 range", async () => {
const { soundPlayer } = await import("./soundPlayer");
// Test that it doesn't throw on edge cases
soundPlayer.setGlobalVolume(0);
soundPlayer.setGlobalVolume(1);
soundPlayer.setGlobalVolume(0.5);
// Test clamping below 0
soundPlayer.setGlobalVolume(-0.5);
// Test clamping above 1
soundPlayer.setGlobalVolume(1.5);
});
it("play returns early when disabled", async () => {
const { soundPlayer } = await import("./soundPlayer");
soundPlayer.setEnabled(false);
// Should not throw when disabled
await expect(soundPlayer.play(NotificationType.SUCCESS)).resolves.toBeUndefined();
});
it("play attempts to play when enabled", async () => {
const { soundPlayer } = await import("./soundPlayer");
soundPlayer.setEnabled(true);
// Should not throw
await expect(soundPlayer.play(NotificationType.SUCCESS)).resolves.toBeUndefined();
});
it("play warns when audio type is not in the cache", async () => {
const { soundPlayer } = await import("./soundPlayer");
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
soundPlayer.setEnabled(true);
await soundPlayer.play("nonexistent" as NotificationType);
expect(warnSpy).toHaveBeenCalledWith("No audio found for notification type: nonexistent");
warnSpy.mockRestore();
});
it("play catches errors from audio playback", async () => {
vi.resetModules();
class FailingAudio {
volume = 1;
preload = "auto";
cloneNode() {
const clone = new FailingAudio();
clone.volume = this.volume;
return clone;
}
async play(): Promise<void> {
throw new Error("Playback blocked by browser");
}
}
const originalAudio = globalThis.Audio;
globalThis.Audio = FailingAudio as unknown as typeof Audio;
const { soundPlayer: freshPlayer } = await import("./soundPlayer");
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
freshPlayer.setEnabled(true);
await freshPlayer.play(NotificationType.SUCCESS);
expect(errorSpy).toHaveBeenCalledWith(
"Failed to play notification sound:",
expect.any(Error)
);
errorSpy.mockRestore();
globalThis.Audio = originalAudio;
});
});
describe("NotificationManager class", () => {
beforeEach(() => {
globalThis.Audio = MockAudioElement as unknown as typeof Audio;
vi.resetModules();
});
afterEach(() => {
globalThis.Audio = OriginalAudio;
});
it("can import notificationManager singleton", async () => {
vi.mock("@tauri-apps/api/core", () => ({
invoke: vi.fn().mockRejectedValue(new Error("Not available")),
}));
const { notificationManager } = await import("./notificationManager");
expect(notificationManager).toBeDefined();
});
it("has notifySuccess method", async () => {
vi.mock("@tauri-apps/api/core", () => ({
invoke: vi.fn().mockRejectedValue(new Error("Not available")),
}));
const { notificationManager } = await import("./notificationManager");
expect(typeof notificationManager.notifySuccess).toBe("function");
});
it("has notifyError method", async () => {
vi.mock("@tauri-apps/api/core", () => ({
invoke: vi.fn().mockRejectedValue(new Error("Not available")),
}));
const { notificationManager } = await import("./notificationManager");
expect(typeof notificationManager.notifyError).toBe("function");
});
it("has notifyPermission method", async () => {
vi.mock("@tauri-apps/api/core", () => ({
invoke: vi.fn().mockRejectedValue(new Error("Not available")),
}));
const { notificationManager } = await import("./notificationManager");
expect(typeof notificationManager.notifyPermission).toBe("function");
});
it("has notifyConnection method", async () => {
vi.mock("@tauri-apps/api/core", () => ({
invoke: vi.fn().mockRejectedValue(new Error("Not available")),
}));
const { notificationManager } = await import("./notificationManager");
expect(typeof notificationManager.notifyConnection).toBe("function");
});
it("has notifyTaskStart method", async () => {
vi.mock("@tauri-apps/api/core", () => ({
invoke: vi.fn().mockRejectedValue(new Error("Not available")),
}));
const { notificationManager } = await import("./notificationManager");
expect(typeof notificationManager.notifyTaskStart).toBe("function");
});
it("has notify method", async () => {
vi.mock("@tauri-apps/api/core", () => ({
invoke: vi.fn().mockRejectedValue(new Error("Not available")),
}));
const { notificationManager } = await import("./notificationManager");
expect(typeof notificationManager.notify).toBe("function");
});
});
describe("notification sounds file paths", () => {
it("all sound files have valid paths", () => {
Object.values(NOTIFICATION_SOUNDS).forEach((sound) => {
// Check that filename doesn't contain path traversal
expect(sound.filename).not.toContain("..");
expect(sound.filename).not.toContain("/");
expect(sound.filename).not.toContain("\\");
});
});
it("sound filenames are mostly unique", () => {
const filenames = Object.values(NOTIFICATION_SOUNDS).map((s) => s.filename);
const uniqueFilenames = new Set(filenames);
// Allow some sound reuse (e.g., COST_ALERT reuses ERROR sound)
expect(uniqueFilenames.size).toBeGreaterThanOrEqual(filenames.length - 1);
});
it("phrases are unique", () => {
const phrases = Object.values(NOTIFICATION_SOUNDS).map((s) => s.phrase);
const uniquePhrases = new Set(phrases);
expect(uniquePhrases.size).toBe(phrases.length);
});
});
});