mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
A stored identity email that failed the stricter rule made every settings read fall back to defaults, and the next save persisted that reset. - the stored preferences schema accepts any email string again - PATCH /api/settings rejects an invalid gitIdentity or gitIdentities email with a field path, and saves the trimmed address - the rule accepts local hosts such as root@localhost
83 lines
3.0 KiB
TypeScript
83 lines
3.0 KiB
TypeScript
import { describe, expect, it, beforeEach } from 'vitest'
|
|
import { Database } from 'bun:sqlite'
|
|
import { DEFAULT_ACTIVITY_WINDOW_MINUTES } from '@opencode-manager/shared/schemas'
|
|
import { SettingsService } from '../../src/services/settings'
|
|
import { allMigrations } from '../../src/db/migrations'
|
|
import { migrate } from '../../src/db/migration-runner'
|
|
|
|
describe('SettingsService', () => {
|
|
let db: Database
|
|
let settingsService: SettingsService
|
|
|
|
beforeEach(() => {
|
|
db = new Database(':memory:')
|
|
migrate(db, allMigrations)
|
|
settingsService = new SettingsService(db)
|
|
})
|
|
|
|
it('recovers from a stored activityWindowMinutes outside the allowed options without losing other preferences', () => {
|
|
db.prepare(
|
|
`INSERT INTO user_preferences (user_id, preferences, updated_at)
|
|
VALUES (?, ?, ?)`
|
|
).run('default', JSON.stringify({ activityWindowMinutes: 5, autoScroll: false }), Date.now())
|
|
|
|
const settings = settingsService.getSettings()
|
|
|
|
expect(settings.preferences.activityWindowMinutes).toBe(DEFAULT_ACTIVITY_WINDOW_MINUTES)
|
|
expect(settings.preferences.autoScroll).toBe(false)
|
|
})
|
|
|
|
it('reads a stored git identity email that the request pattern rejects', () => {
|
|
db.prepare(
|
|
`INSERT INTO user_preferences (user_id, preferences, updated_at)
|
|
VALUES (?, ?, ?)`
|
|
).run(
|
|
'default',
|
|
JSON.stringify({ mode: 'plan', gitIdentity: { name: 'Root', email: 'root@box' } }),
|
|
Date.now()
|
|
)
|
|
|
|
const settings = settingsService.getSettings()
|
|
|
|
expect(settings.preferences.mode).toBe('plan')
|
|
expect(settings.preferences.gitIdentity).toEqual({ name: 'Root', email: 'root@box' })
|
|
})
|
|
|
|
it('keeps a stored git identity and other preferences when an unrelated field is updated', () => {
|
|
db.prepare(
|
|
`INSERT INTO user_preferences (user_id, preferences, updated_at)
|
|
VALUES (?, ?, ?)`
|
|
).run(
|
|
'default',
|
|
JSON.stringify({ mode: 'plan', gitIdentity: { name: 'Root', email: 'root@box' } }),
|
|
Date.now()
|
|
)
|
|
|
|
const settings = settingsService.updateSettings({ theme: 'light' })
|
|
|
|
expect(settings.preferences.theme).toBe('light')
|
|
expect(settings.preferences.mode).toBe('plan')
|
|
expect(settings.preferences.gitIdentity).toEqual({ name: 'Root', email: 'root@box' })
|
|
})
|
|
|
|
it('keeps a stored legacy identity email that no current pattern accepts', () => {
|
|
db.prepare(
|
|
`INSERT INTO user_preferences (user_id, preferences, updated_at)
|
|
VALUES (?, ?, ?)`
|
|
).run(
|
|
'default',
|
|
JSON.stringify({ mode: 'plan', gitIdentity: { name: 'Root', email: 'not an email' } }),
|
|
Date.now()
|
|
)
|
|
|
|
const settings = settingsService.getSettings()
|
|
const updated = settingsService.updateSettings({ autoScroll: false })
|
|
|
|
expect(settings.preferences.mode).toBe('plan')
|
|
expect(settings.preferences.gitIdentity).toEqual({ name: 'Root', email: 'not an email' })
|
|
expect(updated.preferences.autoScroll).toBe(false)
|
|
expect(updated.preferences.mode).toBe('plan')
|
|
expect(updated.preferences.gitIdentity).toEqual({ name: 'Root', email: 'not an email' })
|
|
})
|
|
})
|