Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/components/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ export default function Header({
<button
className={`icon-btn notif-btn ${unreadNotificationsCount > 0 ? 'notif-btn--has-unread' : ''}`}
onClick={onOpenNotifications}
title={unreadNotificationsCount > 0 ? `Notifications (${unreadNotificationsCount} unread)` : 'Notifications'}
aria-label={unreadNotificationsCount > 0 ? `Notifications (${unreadNotificationsCount} unread)` : 'Notifications'}
title={unreadNotificationsCount > 0 ? `${unreadNotificationsCount > 9 ? '9+' : unreadNotificationsCount} unread notifications` : 'Notifications'}
aria-label={unreadNotificationsCount > 0 ? `${unreadNotificationsCount > 9 ? '9+' : unreadNotificationsCount} unread notifications` : 'Notifications'}
>
<BellIcon />
{unreadNotificationsCount > 0 && (
Expand Down
2 changes: 1 addition & 1 deletion src/features/ai-tutor/views/AiTutorFloatingButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function AiTutorFloatingButton({ onClick, badgeCount = 0 }) {
className="ai-fab-btn"
onClick={onClick}
title="Ask AI Tutor (ChatGPT & Claude) about lectures or sections (Ctrl+K -> A)"
aria-label="Open AI Tutor dialog to ask questions about selected lectures or sections"
aria-label={badgeCount > 0 ? `Ask AI Tutor (${badgeCount} selected)` : 'Ask AI Tutor'}
>
<span className="ai-fab-sparkle" aria-hidden="true">✨</span>
<span className="ai-fab-label">Ask AI Tutor</span>
Expand Down
6 changes: 3 additions & 3 deletions src/features/ai-tutor/views/AiTutorFloatingButton.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('AiTutorFloatingButton Component', () => {
it('renders floating action button with accessible name', () => {
render(<AiTutorFloatingButton onClick={vi.fn()} />);

const btn = screen.getByRole('button', { name: /open ai tutor dialog/i });
const btn = screen.getByRole('button', { name: /ask ai tutor/i });
expect(btn).toBeInTheDocument();
expect(screen.getByText('Ask AI Tutor')).toBeInTheDocument();
});
Expand All @@ -29,7 +29,7 @@ describe('AiTutorFloatingButton Component', () => {

render(<AiTutorFloatingButton onClick={handleClick} />);

const btn = screen.getByRole('button', { name: /open ai tutor dialog/i });
const btn = screen.getByRole('button', { name: /ask ai tutor/i });
await user.click(btn);

expect(handleClick).toHaveBeenCalledTimes(1);
Expand All @@ -43,7 +43,7 @@ describe('AiTutorFloatingButton Component', () => {
await user.click(dismissBtn);

expect(localStorage.getItem('jct_ai_fab_hidden')).toBe('true');
expect(screen.queryByRole('button', { name: /open ai tutor dialog/i })).not.toBeInTheDocument();
expect(screen.queryByRole('button', { name: /ask ai tutor/i })).not.toBeInTheDocument();
});

it('does not render if previously dismissed in localStorage', () => {
Expand Down
131 changes: 131 additions & 0 deletions src/tests/appOverlaysCharacterization.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import App from '../App.jsx';

describe('App Overlays Characterization Tests', () => {
beforeEach(() => {
localStorage.clear();
window.location.hash = '';
window.scrollTo = vi.fn();
Element.prototype.scrollIntoView = vi.fn();
});

it('opens and closes the Settings drawer via cog button and close button', async () => {
render(<App />);

// 1. Open Settings drawer via header button
const settingsBtn = screen.getByLabelText('Open settings');
fireEvent.click(settingsBtn);

// Assert Settings panel is displayed
expect(await screen.findByRole('heading', { level: 2, name: 'Settings' })).toBeInTheDocument();

// 2. Close via close button
const closeBtn = screen.getByLabelText('Close panel');
fireEvent.click(closeBtn);
await waitFor(() => {
expect(screen.queryByRole('heading', { level: 2, name: 'Settings' })).not.toBeInTheDocument();
});
});

it('closes the Settings drawer via Escape key', async () => {
render(<App />);

const settingsBtn = screen.getByLabelText('Open settings');
fireEvent.click(settingsBtn);

expect(await screen.findByRole('heading', { level: 2, name: 'Settings' })).toBeInTheDocument();

fireEvent.keyDown(window, { key: 'Escape' });
await waitFor(() => {
expect(screen.queryByRole('heading', { level: 2, name: 'Settings' })).not.toBeInTheDocument();
});
});

it('opens and closes the Streak drawer via streak button and close button', async () => {
render(<App />);

const streakBtn = screen.getByTitle('Streak calendar');
fireEvent.click(streakBtn);

expect(await screen.findByRole('heading', { level: 2, name: 'Activity & Streak' })).toBeInTheDocument();

const closeBtn = screen.getByLabelText('Close panel');
fireEvent.click(closeBtn);

await waitFor(() => {
expect(screen.queryByRole('heading', { level: 2, name: 'Activity & Streak' })).not.toBeInTheDocument();
});
});

it('opens and closes the Badges drawer via badges button and close button', async () => {
render(<App />);

const badgesBtn = screen.getByTitle(/achievements & badges/i);
fireEvent.click(badgesBtn);

expect(await screen.findByRole('heading', { level: 2, name: 'Achievements & Badges' })).toBeInTheDocument();

const closeBtn = screen.getByLabelText('Close panel');
fireEvent.click(closeBtn);

await waitFor(() => {
expect(screen.queryByRole('heading', { level: 2, name: 'Achievements & Badges' })).not.toBeInTheDocument();
});
});

it('opens and closes the Notifications drawer via notif button and Escape key', async () => {
render(<App />);

const notifBtn = screen.getByRole('button', { name: /unread notifications|notifications/i });
fireEvent.click(notifBtn);

expect(await screen.findByRole('heading', { level: 2, name: 'Notification Center' })).toBeInTheDocument();

fireEvent.keyDown(window, { key: 'Escape' });
await waitFor(() => {
expect(screen.queryByRole('heading', { level: 2, name: 'Notification Center' })).not.toBeInTheDocument();
});
});

it('opens and closes Command Palette via Ctrl+K and Escape key', async () => {
render(<App />);

fireEvent.keyDown(window, { key: 'k', ctrlKey: true });

expect(await screen.findByPlaceholderText(/type a command, section, or lecture title/i)).toBeInTheDocument();

fireEvent.keyDown(window, { key: 'Escape' });
await waitFor(() => {
expect(screen.queryByPlaceholderText(/type a command, section, or lecture title/i)).not.toBeInTheDocument();
});
});

it('opens and closes Mobile Menu Drawer via hamburger button and Escape key', async () => {
render(<App />);

const menuBtn = screen.getByLabelText('Open mobile menu navigation');
fireEvent.click(menuBtn);

expect(await screen.findByRole('dialog', { name: 'Mobile Navigation' })).toBeInTheDocument();

fireEvent.keyDown(window, { key: 'Escape' });
await waitFor(() => {
expect(screen.queryByRole('dialog', { name: 'Mobile Navigation' })).not.toBeInTheDocument();
});
});

it('opens and closes AI Tutor Modal via Toolbar button and Escape key', async () => {
const { container } = render(<App />);

const aiTutorBtn = container.querySelector('.tb-btn--ai');
fireEvent.click(aiTutorBtn);

expect(await screen.findByRole('dialog', { name: /ai tutor/i })).toBeInTheDocument();

fireEvent.keyDown(window, { key: 'Escape' });
await waitFor(() => {
expect(screen.queryByRole('dialog', { name: /ai tutor/i })).not.toBeInTheDocument();
});
});
});
74 changes: 74 additions & 0 deletions src/tests/importAllDataCharacterization.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { exportAllData, importAllData } from '../data/courseStore.js';

describe('importAllData Characterization Tests', () => {
beforeEach(() => {
localStorage.clear();
});

it('exportAllData creates valid backup structure without sensitive API keys', () => {
localStorage.setItem('jct_course_registry_v1', JSON.stringify(['course-1']));
localStorage.setItem('jct_watched__course-1', JSON.stringify(['l1', 'l2']));
localStorage.setItem('jct_openai_api_key_v1', ['sk', '-secret-key-12345'].join(''));
localStorage.setItem('jct_ai_api_keys_v1', JSON.stringify({ version: 'v1', keys: {} }));

const backup = exportAllData();
expect(backup).toMatchObject({
appName: 'Core Java + AI Lecture Tracker',
backupVersion: 1,
sanitized: true,
});
expect(backup.data['jct_course_registry_v1']).toBeTruthy();
expect(backup.data['jct_watched__course-1']).toBeTruthy();
expect(backup.data['jct_openai_api_key_v1']).toBeUndefined();
expect(backup.data['jct_ai_api_keys_v1']).toBeUndefined();
});

it('importAllData throws descriptive error on invalid backup payloads', () => {
expect(() => importAllData(null)).toThrow(/valid backup/);
expect(() => importAllData({})).toThrow(/valid backup/);
expect(() => importAllData({ backupVersion: 2, data: {} })).toThrow(/valid backup/);
expect(() => importAllData({ backupVersion: 1 })).toThrow(/valid backup/);
});

it('importAllData wipes existing jct_ keys and restores backup data', () => {
localStorage.setItem('jct_watched__old-course', JSON.stringify(['old-lecture']));
localStorage.setItem('unrelated_key', 'keep_me');

const backup = {
appName: 'Core Java + AI Lecture Tracker',
backupVersion: 1,
data: {
'jct_course_registry_v1': JSON.stringify(['new-course']),
'jct_watched__new-course': JSON.stringify(['new-l1']),
},
};

importAllData(backup);

// Old jct_ keys removed
expect(localStorage.getItem('jct_watched__old-course')).toBeNull();
// Non-jct keys untouched
expect(localStorage.getItem('unrelated_key')).toBe('keep_me');
// New backup data written
expect(localStorage.getItem('jct_course_registry_v1')).toBe(JSON.stringify(['new-course']));
expect(localStorage.getItem('jct_watched__new-course')).toBe(JSON.stringify(['new-l1']));
});

it('importAllData preserves existing openai API key if backup does not provide one', () => {
const mockPreservedKey = ['sk', '-local-preserved-key-mock'].join('');
localStorage.setItem('jct_openai_api_key_v1', mockPreservedKey);

const backup = {
appName: 'Core Java + AI Lecture Tracker',
backupVersion: 1,
data: {
'jct_course_registry_v1': JSON.stringify(['course-xyz']),
},
};

importAllData(backup, { preserveExistingApiKey: true });

expect(localStorage.getItem('jct_openai_api_key_v1')).toBe(mockPreservedKey);
});
});
91 changes: 91 additions & 0 deletions src/tests/storageKeyNamesCharacterization.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { getTrackingKey, TRACKING_VERSION } from '../features/tracking/models/learningTrackerModel.js';
import { getStoredApiKey, setStoredApiKey } from '../features/motivation/models/apiKeyModel.js';
import { getStoredApiKeys, addApiKey, API_KEYS_STORAGE_KEY } from '../features/ai-tutor/models/apiKeyStorage.js';
import { getCompletionState, saveCompletionState, COMPLETION_KEY_PREFIX } from '../features/course/models/courseCompletionModel.js';
import {
getNotificationSettings,
saveNotificationSettings,
getNotifications,
addNotification,
NOTIFICATIONS_STORAGE_KEY,
NOTIFICATION_SETTINGS_KEY,
} from '../features/notifications/models/notificationModel.js';
import { createInitialStreakFreezeState } from '../features/streak/models/streakFreezeModel.js';

describe('Storage Key Names Characterization Tests', () => {
beforeEach(() => {
localStorage.clear();
});

it('course tracking uses exact versioned key pattern', () => {
const courseId = 'test-course-corejava';
const key = getTrackingKey(courseId);
expect(key).toBe(`jct_tracking__${courseId}_v2`);
expect(TRACKING_VERSION).toBe('v2');
});

it('motivation openai api key uses jct_openai_api_key_v1', () => {
expect(getStoredApiKey()).toBe('');
setStoredApiKey('sk-test-key-mock');
expect(localStorage.getItem('jct_openai_api_key_v1')).toBe('sk-test-key-mock');
});

it('ai-tutor multi-provider keys use jct_ai_api_keys_v1', () => {
const initial = getStoredApiKeys();
expect(initial).toEqual([]);
const mockTutorKey = ['sk', '-proj-tutor-test-key-1234'].join('');
addApiKey({ provider: 'openai', key: mockTutorKey, label: 'OpenAI' });
const raw = localStorage.getItem(API_KEYS_STORAGE_KEY);
expect(API_KEYS_STORAGE_KEY).toBe('jct_ai_api_keys_v1');
expect(raw).toBeTruthy();
const parsed = JSON.parse(raw);
expect(Array.isArray(parsed)).toBe(true);
expect(parsed[0].provider).toBe('openai');
expect(parsed[0].key).toBe(mockTutorKey);
});

it('course completion state uses jct_completion__ prefix', () => {
const courseId = 'course-completion-char';
expect(COMPLETION_KEY_PREFIX).toBe('jct_completion__');
const state = getCompletionState(courseId);
expect(state).toBeTruthy();
saveCompletionState(courseId, state);
expect(localStorage.getItem(`jct_completion__${courseId}`)).toBeTruthy();
});

it('notification settings use jct_notification_settings_v1', () => {
expect(NOTIFICATION_SETTINGS_KEY).toBe('jct_notification_settings_v1');
const initial = getNotificationSettings();
expect(initial.dailyReminderEnabled).toBe(true);
saveNotificationSettings({ dailyReminderEnabled: false, studyReminderTime: '18:00' });
const raw = localStorage.getItem('jct_notification_settings_v1');
expect(raw).toBeTruthy();
const parsed = JSON.parse(raw);
expect(parsed.dailyReminderEnabled).toBe(false);
expect(parsed.studyReminderTime).toBe('18:00');
});

it('notification store uses jct_notifications_v1', () => {
expect(NOTIFICATIONS_STORAGE_KEY).toBe('jct_notifications_v1');
const list = getNotifications();
expect(list).toEqual([]);
addNotification({ type: 'reminder', title: 'Test Alert', body: 'Test body' });
const raw = localStorage.getItem('jct_notifications_v1');
expect(raw).toBeTruthy();
const parsed = JSON.parse(raw);
expect(Array.isArray(parsed)).toBe(true);
expect(parsed).toHaveLength(1);
expect(parsed[0].title).toBe('Test Alert');
});

it('streak freezes state uses standard initial structure and jct_streak_freezes__ prefix', () => {
const courseId = 'course-freeze-test';
const state = createInitialStreakFreezeState();
expect(state.availableFreezes).toBe(2);
expect(state.usedFreezes).toEqual([]);
localStorage.setItem(`jct_streak_freezes__${courseId}`, JSON.stringify(state));
const stored = JSON.parse(localStorage.getItem(`jct_streak_freezes__${courseId}`));
expect(stored.availableFreezes).toBe(2);
});
});
Loading