Files
planet/client/tests/ui-scale.test.ts
T

155 lines
4.7 KiB
TypeScript

import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { afterEach, describe, expect, it } from 'bun:test'
import { DesktopConfigStore } from '../src/main/config'
import {
computeAutoUiZoomFactor,
computeDisplayAutoUiZoomFactor,
formatUiScalePercent,
normalizeUiScaleMode,
resolveEffectiveUiZoomFactor,
} from '../src/main/ui-scale'
const tempDirs: string[] = []
afterEach(() => {
while (tempDirs.length > 0) {
const dir = tempDirs.pop()!
fs.rmSync(dir, { recursive: true, force: true })
}
})
describe('ui-scale normalization, auto high-res/low-dpi calculation & config persistence', () => {
it('normalizes auto, numeric presets, percentage strings, and rejects unsupported values', () => {
expect(normalizeUiScaleMode('auto')).toBe('auto')
expect(normalizeUiScaleMode(' AUTO ')).toBe('auto')
expect(normalizeUiScaleMode(1.5)).toBe(1.5)
expect(normalizeUiScaleMode('1.25')).toBe(1.25)
expect(normalizeUiScaleMode('150%')).toBe(1.5)
expect(normalizeUiScaleMode('175 %')).toBe(1.75)
expect(normalizeUiScaleMode('200%')).toBe(2)
expect(normalizeUiScaleMode('0.5')).toBe('auto')
expect(normalizeUiScaleMode('not-a-scale', 1.25)).toBe(1.25)
})
it('defaults 4K@100% low-DPI displays to 1.5x and avoids double-scaling high-DPI OS setups', () => {
// 1080p @ 100% -> 1x
expect(computeDisplayAutoUiZoomFactor(1920, 1080, 1)).toBe(1)
// 2560x1440 @ 100% -> 1.25x
expect(computeDisplayAutoUiZoomFactor(2560, 1440, 1)).toBe(1.25)
// 4K 3840x2160 @ 100% -> 1.5x (requested default)
expect(computeDisplayAutoUiZoomFactor(3840, 2160, 1)).toBe(1.5)
// 4K @ 125% (3072x1728 DIP) -> 1.25x (effective 156%)
expect(computeDisplayAutoUiZoomFactor(3072, 1728, 1.25)).toBe(1.25)
// 4K @ 150% (2560x1440 DIP, osScale >= 1.25) -> 1x (no double scaling)
expect(computeDisplayAutoUiZoomFactor(2560, 1440, 1.5)).toBe(1)
// 4K @ 200% (1920x1080 DIP) -> 1x
expect(computeDisplayAutoUiZoomFactor(1920, 1080, 2)).toBe(1)
// 5K+ @ 100% (5120x2880 DIP) -> 2x
expect(computeDisplayAutoUiZoomFactor(5120, 2880, 1)).toBe(2)
})
it('clamps floating windows so zoomed CSS viewports never drop into cramped mobile sizes and respects portrait', () => {
// Maximized 4K@100% -> full 1.5x
expect(
computeAutoUiZoomFactor({
displayWidth: 3840,
displayHeight: 2160,
displayScaleFactor: 1,
windowWidth: 3840,
windowHeight: 2080,
}),
).toBe(1.5)
// Fresh scaled initial window on 4K@100% (2160x1350 -> 1440x900 @ 1.5x)
expect(
computeAutoUiZoomFactor({
displayWidth: 3840,
displayHeight: 2160,
displayScaleFactor: 1,
windowWidth: 2160,
windowHeight: 1350,
}),
).toBe(1.5)
// Small 1440x900 floating window on 4K@100% steps down to 1x so logical width stays >= 1180
expect(
computeAutoUiZoomFactor({
displayWidth: 3840,
displayHeight: 2160,
displayScaleFactor: 1,
windowWidth: 1440,
windowHeight: 900,
}),
).toBe(1)
// Portrait mobile window always stays at 1x
expect(
computeAutoUiZoomFactor({
displayWidth: 3840,
displayHeight: 2160,
displayScaleFactor: 1,
windowWidth: 480,
windowHeight: 900,
isPortrait: true,
}),
).toBe(1)
// Manual preset overrides auto regardless of display
expect(
resolveEffectiveUiZoomFactor(1.75, {
displayWidth: 1920,
displayHeight: 1080,
displayScaleFactor: 1,
windowWidth: 1440,
windowHeight: 900,
}),
).toBe(1.75)
expect(formatUiScalePercent(1.5)).toBe('150%')
})
it('persists uiScale in DesktopConfigStore and honors env and CLI overrides', () => {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'planet-ui-scale-'))
tempDirs.push(tempDir)
const store = new DesktopConfigStore({
userDataDir: tempDir,
exeDir: tempDir,
argv: [],
env: {},
})
expect(store.get().uiScale).toBe('auto')
store.updateUiScale(1.5)
expect(store.get().uiScale).toBe(1.5)
const reloaded = new DesktopConfigStore({
userDataDir: tempDir,
exeDir: tempDir,
argv: [],
env: {},
})
expect(reloaded.get().uiScale).toBe(1.5)
const envOverride = new DesktopConfigStore({
userDataDir: tempDir,
exeDir: tempDir,
argv: [],
env: { PLANET_UI_SCALE: '175%' },
})
expect(envOverride.get().uiScale).toBe(1.75)
const cliOverride = new DesktopConfigStore({
userDataDir: tempDir,
exeDir: tempDir,
argv: ['--ui-scale=2'],
env: { PLANET_UI_SCALE: '1.25' },
})
expect(cliOverride.get().uiScale).toBe(2)
})
})