mirror of
https://github.com/NanmiCoder/cc-haha
synced 2026-07-18 13:23:33 +08:00
The pure white appearance option needed to avoid warm-theme leakage while preserving the existing warm classic brand theme. This adds the white theme mode, keeps local browser startup from reusing stale H5 server URLs in dev, and moves visible legacy warm surfaces onto theme tokens. Constraint: H5 server auth policy, CORS policy, SDK routes, adapter routes, and IM access paths must not change for a visual theme fix Rejected: Rename the original light theme to pure white | the original theme is a warm classic palette, not a neutral white workspace Confidence: high Scope-risk: moderate Directive: Keep structural white-theme borders neutral; reserve the warm brand color for selected states, primary actions, and small accents Tested: cd desktop && bun run test -- src/__tests__/generalSettings.test.tsx src/lib/desktopRuntime.test.ts src/lib/persistenceMigrations.test.ts src/stores/uiStore.test.ts src/stores/settingsStore.test.ts Tested: cd desktop && bun run lint Tested: cd desktop && bun run build Tested: bun test src/server/__tests__/settings.test.ts src/server/__tests__/h5-access-policy.test.ts src/server/__tests__/h5-access-auth.test.ts src/server/middleware/cors.test.ts Tested: Browser smoke at http://127.0.0.1:5173 with data-theme=white, no H5 token prompt, /status inspector visible, inspector border #DDE3EA Not-tested: Full bun run verify gate
427 lines
13 KiB
TypeScript
427 lines
13 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { ApiError } from '../api/client'
|
|
|
|
describe('settingsStore locale defaults', () => {
|
|
beforeEach(() => {
|
|
vi.resetModules()
|
|
window.localStorage.clear()
|
|
})
|
|
|
|
it('defaults to Chinese when no locale is stored', async () => {
|
|
const { useSettingsStore } = await import('./settingsStore')
|
|
|
|
expect(useSettingsStore.getState().locale).toBe('zh')
|
|
})
|
|
|
|
it('keeps a stored locale override', async () => {
|
|
window.localStorage.setItem('cc-haha-locale', 'en')
|
|
|
|
const { useSettingsStore } = await import('./settingsStore')
|
|
|
|
expect(useSettingsStore.getState().locale).toBe('en')
|
|
})
|
|
})
|
|
|
|
describe('settingsStore desktop notification persistence', () => {
|
|
beforeEach(() => {
|
|
vi.resetModules()
|
|
vi.clearAllMocks()
|
|
window.localStorage.clear()
|
|
})
|
|
|
|
it('defaults desktop notifications to explicit opt-in', async () => {
|
|
vi.doMock('../api/settings', () => ({
|
|
settingsApi: {
|
|
getUser: vi.fn(),
|
|
updateUser: vi.fn(),
|
|
getPermissionMode: vi.fn(),
|
|
setPermissionMode: vi.fn(),
|
|
getCliLauncherStatus: vi.fn(),
|
|
},
|
|
}))
|
|
vi.doMock('../api/models', () => ({
|
|
modelsApi: {
|
|
list: vi.fn(),
|
|
getCurrent: vi.fn(),
|
|
setCurrent: vi.fn(),
|
|
getEffort: vi.fn(),
|
|
setEffort: vi.fn(),
|
|
},
|
|
}))
|
|
vi.doMock('../api/h5Access', () => ({
|
|
h5AccessApi: {
|
|
get: vi.fn().mockResolvedValue({
|
|
settings: {
|
|
enabled: false,
|
|
tokenPreview: null,
|
|
allowedOrigins: [],
|
|
publicBaseUrl: null,
|
|
},
|
|
}),
|
|
enable: vi.fn(),
|
|
disable: vi.fn(),
|
|
regenerate: vi.fn(),
|
|
update: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
const { useSettingsStore } = await import('./settingsStore')
|
|
|
|
expect(useSettingsStore.getState().desktopNotificationsEnabled).toBe(false)
|
|
})
|
|
|
|
it('keeps desktop notifications disabled when user settings do not opt in', async () => {
|
|
vi.doMock('../api/settings', () => ({
|
|
settingsApi: {
|
|
getUser: vi.fn().mockResolvedValue({}),
|
|
updateUser: vi.fn(),
|
|
getPermissionMode: vi.fn().mockResolvedValue({ mode: 'default' }),
|
|
setPermissionMode: vi.fn(),
|
|
getCliLauncherStatus: vi.fn(),
|
|
},
|
|
}))
|
|
vi.doMock('../api/models', () => ({
|
|
modelsApi: {
|
|
list: vi.fn().mockResolvedValue({ models: [] }),
|
|
getCurrent: vi.fn().mockResolvedValue({ model: null }),
|
|
setCurrent: vi.fn(),
|
|
getEffort: vi.fn().mockResolvedValue({ level: 'medium' }),
|
|
setEffort: vi.fn(),
|
|
},
|
|
}))
|
|
vi.doMock('../api/h5Access', () => ({
|
|
h5AccessApi: {
|
|
get: vi.fn().mockResolvedValue({
|
|
settings: {
|
|
enabled: false,
|
|
tokenPreview: null,
|
|
allowedOrigins: [],
|
|
publicBaseUrl: null,
|
|
},
|
|
}),
|
|
enable: vi.fn(),
|
|
disable: vi.fn(),
|
|
regenerate: vi.fn(),
|
|
update: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
const { useSettingsStore } = await import('./settingsStore')
|
|
|
|
await useSettingsStore.getState().fetchAll()
|
|
|
|
expect(useSettingsStore.getState().desktopNotificationsEnabled).toBe(false)
|
|
})
|
|
|
|
it('persists the latest desktop notification toggle when saves overlap', async () => {
|
|
const pendingSaves: Array<() => void> = []
|
|
const updateUser = vi.fn(
|
|
() =>
|
|
new Promise<{ ok: true }>((resolve) => {
|
|
pendingSaves.push(() => resolve({ ok: true }))
|
|
}),
|
|
)
|
|
|
|
vi.doMock('../api/settings', () => ({
|
|
settingsApi: {
|
|
getUser: vi.fn(),
|
|
updateUser,
|
|
getPermissionMode: vi.fn(),
|
|
setPermissionMode: vi.fn(),
|
|
getCliLauncherStatus: vi.fn(),
|
|
},
|
|
}))
|
|
vi.doMock('../api/models', () => ({
|
|
modelsApi: {
|
|
list: vi.fn(),
|
|
getCurrent: vi.fn(),
|
|
setCurrent: vi.fn(),
|
|
getEffort: vi.fn(),
|
|
setEffort: vi.fn(),
|
|
},
|
|
}))
|
|
vi.doMock('../api/h5Access', () => ({
|
|
h5AccessApi: {
|
|
get: vi.fn().mockResolvedValue({
|
|
settings: {
|
|
enabled: false,
|
|
tokenPreview: null,
|
|
allowedOrigins: [],
|
|
publicBaseUrl: null,
|
|
},
|
|
}),
|
|
enable: vi.fn(),
|
|
disable: vi.fn(),
|
|
regenerate: vi.fn(),
|
|
update: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
const { useSettingsStore } = await import('./settingsStore')
|
|
|
|
const firstSave = useSettingsStore.getState().setDesktopNotificationsEnabled(false)
|
|
await vi.waitFor(() => {
|
|
expect(updateUser).toHaveBeenCalledWith({ desktopNotificationsEnabled: false })
|
|
})
|
|
|
|
const secondSave = useSettingsStore.getState().setDesktopNotificationsEnabled(true)
|
|
expect(useSettingsStore.getState().desktopNotificationsEnabled).toBe(true)
|
|
|
|
pendingSaves.shift()?.()
|
|
await vi.waitFor(() => {
|
|
expect(updateUser).toHaveBeenCalledWith({ desktopNotificationsEnabled: true })
|
|
})
|
|
pendingSaves.shift()?.()
|
|
await Promise.all([firstSave, secondSave])
|
|
|
|
expect(updateUser).toHaveBeenLastCalledWith({ desktopNotificationsEnabled: true })
|
|
expect(useSettingsStore.getState().desktopNotificationsEnabled).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('settingsStore theme persistence', () => {
|
|
beforeEach(() => {
|
|
vi.resetModules()
|
|
vi.clearAllMocks()
|
|
window.localStorage.clear()
|
|
document.documentElement.removeAttribute('data-theme')
|
|
document.documentElement.style.colorScheme = ''
|
|
})
|
|
|
|
it('hydrates the pure white theme from user settings', async () => {
|
|
vi.doMock('../api/settings', () => ({
|
|
settingsApi: {
|
|
getUser: vi.fn().mockResolvedValue({ theme: 'white' }),
|
|
updateUser: vi.fn(),
|
|
getPermissionMode: vi.fn().mockResolvedValue({ mode: 'default' }),
|
|
setPermissionMode: vi.fn(),
|
|
getCliLauncherStatus: vi.fn(),
|
|
},
|
|
}))
|
|
vi.doMock('../api/models', () => ({
|
|
modelsApi: {
|
|
list: vi.fn().mockResolvedValue({ models: [] }),
|
|
getCurrent: vi.fn().mockResolvedValue({ model: null }),
|
|
setCurrent: vi.fn(),
|
|
getEffort: vi.fn().mockResolvedValue({ level: 'medium' }),
|
|
setEffort: vi.fn(),
|
|
},
|
|
}))
|
|
vi.doMock('../api/h5Access', () => ({
|
|
h5AccessApi: {
|
|
get: vi.fn().mockResolvedValue({
|
|
settings: {
|
|
enabled: false,
|
|
tokenPreview: null,
|
|
allowedOrigins: [],
|
|
publicBaseUrl: null,
|
|
},
|
|
}),
|
|
enable: vi.fn(),
|
|
disable: vi.fn(),
|
|
regenerate: vi.fn(),
|
|
update: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
const { useSettingsStore } = await import('./settingsStore')
|
|
const { useUIStore } = await import('./uiStore')
|
|
|
|
await useSettingsStore.getState().fetchAll()
|
|
|
|
expect(useSettingsStore.getState().theme).toBe('white')
|
|
expect(useUIStore.getState().theme).toBe('white')
|
|
expect(document.documentElement.getAttribute('data-theme')).toBe('white')
|
|
expect(document.documentElement.style.colorScheme).toBe('light')
|
|
})
|
|
})
|
|
|
|
describe('settingsStore H5 access behavior', () => {
|
|
beforeEach(() => {
|
|
vi.resetModules()
|
|
vi.clearAllMocks()
|
|
window.localStorage.clear()
|
|
})
|
|
|
|
it.each([404, 405])('falls back to disabled defaults only for legacy H5 endpoint status %s', async (status) => {
|
|
vi.doMock('../api/settings', () => ({
|
|
settingsApi: {
|
|
getUser: vi.fn().mockResolvedValue({}),
|
|
updateUser: vi.fn(),
|
|
getPermissionMode: vi.fn().mockResolvedValue({ mode: 'default' }),
|
|
setPermissionMode: vi.fn(),
|
|
getCliLauncherStatus: vi.fn(),
|
|
},
|
|
}))
|
|
vi.doMock('../api/models', () => ({
|
|
modelsApi: {
|
|
list: vi.fn().mockResolvedValue({ models: [] }),
|
|
getCurrent: vi.fn().mockResolvedValue({ model: null }),
|
|
setCurrent: vi.fn(),
|
|
getEffort: vi.fn().mockResolvedValue({ level: 'medium' }),
|
|
setEffort: vi.fn(),
|
|
},
|
|
}))
|
|
vi.doMock('../api/h5Access', () => ({
|
|
h5AccessApi: {
|
|
get: vi.fn().mockRejectedValue(new ApiError(status, { message: 'legacy' })),
|
|
enable: vi.fn(),
|
|
disable: vi.fn(),
|
|
regenerate: vi.fn(),
|
|
update: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
const { useSettingsStore } = await import('./settingsStore')
|
|
useSettingsStore.setState({
|
|
h5Access: {
|
|
enabled: true,
|
|
tokenPreview: 'h5_prev',
|
|
allowedOrigins: ['https://prev.example'],
|
|
publicBaseUrl: 'https://prev.example/app',
|
|
},
|
|
})
|
|
|
|
await useSettingsStore.getState().fetchAll()
|
|
|
|
expect(useSettingsStore.getState().h5Access).toEqual({
|
|
enabled: false,
|
|
tokenPreview: null,
|
|
allowedOrigins: [],
|
|
publicBaseUrl: null,
|
|
})
|
|
expect(useSettingsStore.getState().h5AccessError).toBeNull()
|
|
})
|
|
|
|
it('preserves the last known H5 state and surfaces an H5 error on non-legacy load failures', async () => {
|
|
vi.doMock('../api/settings', () => ({
|
|
settingsApi: {
|
|
getUser: vi.fn().mockResolvedValue({}),
|
|
updateUser: vi.fn(),
|
|
getPermissionMode: vi.fn().mockResolvedValue({ mode: 'default' }),
|
|
setPermissionMode: vi.fn(),
|
|
getCliLauncherStatus: vi.fn(),
|
|
},
|
|
}))
|
|
vi.doMock('../api/models', () => ({
|
|
modelsApi: {
|
|
list: vi.fn().mockResolvedValue({ models: [] }),
|
|
getCurrent: vi.fn().mockResolvedValue({ model: null }),
|
|
setCurrent: vi.fn(),
|
|
getEffort: vi.fn().mockResolvedValue({ level: 'medium' }),
|
|
setEffort: vi.fn(),
|
|
},
|
|
}))
|
|
vi.doMock('../api/h5Access', () => ({
|
|
h5AccessApi: {
|
|
get: vi.fn().mockRejectedValue(new ApiError(500, { message: 'H5 unavailable' })),
|
|
enable: vi.fn(),
|
|
disable: vi.fn(),
|
|
regenerate: vi.fn(),
|
|
update: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
const { useSettingsStore } = await import('./settingsStore')
|
|
useSettingsStore.setState({
|
|
h5Access: {
|
|
enabled: true,
|
|
tokenPreview: 'h5_prev',
|
|
allowedOrigins: ['https://prev.example'],
|
|
publicBaseUrl: 'https://prev.example/app',
|
|
},
|
|
})
|
|
|
|
await useSettingsStore.getState().fetchAll()
|
|
|
|
expect(useSettingsStore.getState().h5Access).toEqual({
|
|
enabled: true,
|
|
tokenPreview: 'h5_prev',
|
|
allowedOrigins: ['https://prev.example'],
|
|
publicBaseUrl: 'https://prev.example/app',
|
|
})
|
|
expect(useSettingsStore.getState().h5AccessError).toBe('H5 unavailable')
|
|
})
|
|
|
|
it('handles H5 enable, regenerate, and disable transitions without persisting a raw token in store state', async () => {
|
|
vi.doMock('../api/settings', () => ({
|
|
settingsApi: {
|
|
getUser: vi.fn(),
|
|
updateUser: vi.fn(),
|
|
getPermissionMode: vi.fn(),
|
|
setPermissionMode: vi.fn(),
|
|
getCliLauncherStatus: vi.fn(),
|
|
},
|
|
}))
|
|
vi.doMock('../api/models', () => ({
|
|
modelsApi: {
|
|
list: vi.fn(),
|
|
getCurrent: vi.fn(),
|
|
setCurrent: vi.fn(),
|
|
getEffort: vi.fn(),
|
|
setEffort: vi.fn(),
|
|
},
|
|
}))
|
|
vi.doMock('../api/h5Access', () => ({
|
|
h5AccessApi: {
|
|
get: vi.fn(),
|
|
enable: vi.fn().mockResolvedValue({
|
|
settings: {
|
|
enabled: true,
|
|
tokenPreview: 'h5_first',
|
|
allowedOrigins: [],
|
|
publicBaseUrl: null,
|
|
},
|
|
token: 'raw-enable-token',
|
|
}),
|
|
disable: vi.fn().mockResolvedValue({
|
|
settings: {
|
|
enabled: false,
|
|
tokenPreview: null,
|
|
allowedOrigins: [],
|
|
publicBaseUrl: null,
|
|
},
|
|
}),
|
|
regenerate: vi.fn().mockResolvedValue({
|
|
settings: {
|
|
enabled: true,
|
|
tokenPreview: 'h5_second',
|
|
allowedOrigins: ['https://phone.example'],
|
|
publicBaseUrl: 'https://phone.example/app',
|
|
},
|
|
token: 'raw-regenerated-token',
|
|
}),
|
|
update: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
const { useSettingsStore } = await import('./settingsStore')
|
|
|
|
await expect(useSettingsStore.getState().enableH5Access()).resolves.toBe('raw-enable-token')
|
|
expect(useSettingsStore.getState().h5Access).toEqual({
|
|
enabled: true,
|
|
tokenPreview: 'h5_first',
|
|
allowedOrigins: [],
|
|
publicBaseUrl: null,
|
|
})
|
|
|
|
await expect(useSettingsStore.getState().regenerateH5AccessToken()).resolves.toBe('raw-regenerated-token')
|
|
expect(useSettingsStore.getState().h5Access).toEqual({
|
|
enabled: true,
|
|
tokenPreview: 'h5_second',
|
|
allowedOrigins: ['https://phone.example'],
|
|
publicBaseUrl: 'https://phone.example/app',
|
|
})
|
|
|
|
await expect(useSettingsStore.getState().disableH5Access()).resolves.toBeUndefined()
|
|
expect(useSettingsStore.getState().h5Access).toEqual({
|
|
enabled: false,
|
|
tokenPreview: null,
|
|
allowedOrigins: [],
|
|
publicBaseUrl: null,
|
|
})
|
|
expect(useSettingsStore.getState().h5AccessError).toBeNull()
|
|
expect('h5AccessGeneratedToken' in useSettingsStore.getState()).toBe(false)
|
|
})
|
|
})
|