SPB Git

Repository Public

KeyAndThemeTests.swift 14.1 KB 277 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : Claude Remote / Codex Remote9// File             : Packages/ClaudeRemoteKit/Tests/TerminalTests/KeyAndThemeTests.swift10// Module           : TerminalTests11// Repository       : https://git.spboucher.ai/claude-remote-apple12// Project Website  : https://claude.spboucher.ai13// Created          : 2026-11-2914// Last Modified    : 2026-11-2915//16// =============================================================================17// AUTHOR & PROFESSIONAL AFFILIATION18// =============================================================================19//20// Author           : Simon-Pierre Boucher21// Position         : Professor of Finance22// Institution      : Université du Québec en Outaouais (UQO)23// Department       : Département des sciences administratives24// Organization     : SP Boucher25//26// Website          : https://spboucher.ai27// Git              : https://git.spboucher.ai28//29// =============================================================================30// OFFICIAL CONTACT DIRECTORY31// =============================================================================32//33// General          : info@spboucher.ai34// Professional     : contact@spboucher.ai35// Direct           : simon@spboucher.ai36// Research         : research@spboucher.ai37// Development      : dev@spboucher.ai38// Support          : support@spboucher.ai39// Security/Admin   : admin@spboucher.ai40// Notifications    : notifications@spboucher.ai41//42// Preferred Contact: support@spboucher.ai43//44// =============================================================================45// COPYRIGHT & INTELLECTUAL PROPERTY46// =============================================================================47//48// Copyright (c) 202649// Simon-Pierre Boucher50//51// All rights reserved where applicable.52//53// This file is governed by the applicable project license,54// copyright notices, institutional agreements and relevant law.55//56// Unless expressly authorized by the applicable license or57// rights holder, copying, redistribution, sublicensing,58// publication and commercial exploitation are not permitted.59//60// Existing third-party rights and notices remain unaffected.61//62// =============================================================================63// CONFIDENTIALITY & SECURITY64// =============================================================================65//66// Classification   : Public67// Distribution     : Public repository; use is governed by the project license68// Security Contact : admin@spboucher.ai69//70// Access to this source does not independently grant permission71// to redistribute or disclose confidential materials.72//73// Do not embed credentials, private keys, access tokens,74// certificates or confidential personal information.75//76// =============================================================================77// LICENSE & MAINTENANCE78// =============================================================================79//80// License          : None specified (no license file in this repository)81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import ClaudeRemoteModels89import ClaudeRemoteTerminal90import Foundation91import Testing9293private let esc = "\u{1B}"9495@Suite("Key encoding (parity with the web keys.ts)")96struct KeyEncodingTests {97    @Test func arrowsNormalAndApplicationCursor() {98        #expect(KeyEncoder.sequence(for: .up) == "\(esc)[A")99        #expect(KeyEncoder.sequence(for: .down) == "\(esc)[B")100        #expect(KeyEncoder.sequence(for: .right) == "\(esc)[C")101        #expect(KeyEncoder.sequence(for: .left) == "\(esc)[D")102        #expect(KeyEncoder.sequence(for: .home) == "\(esc)[H")103        #expect(KeyEncoder.sequence(for: .end) == "\(esc)[F")104        #expect(KeyEncoder.sequence(for: .up, applicationCursor: true) == "\(esc)OA")105        #expect(KeyEncoder.sequence(for: .left, applicationCursor: true) == "\(esc)OD")106        #expect(KeyEncoder.sequence(for: .end, applicationCursor: true) == "\(esc)OF")107    }108109    @Test func modifiedArrowsIgnoreApplicationCursor() {110        #expect(KeyEncoder.sequence(for: .up, modifiers: StickyModifiers(ctrl: .oneShot), applicationCursor: true) == "\(esc)[1;5A")111        #expect(KeyEncoder.sequence(for: .right, modifiers: StickyModifiers(shift: .locked)) == "\(esc)[1;2C")112        #expect(KeyEncoder.sequence(for: .left, modifiers: StickyModifiers(alt: .oneShot)) == "\(esc)[1;3D")113        #expect(KeyEncoder.sequence(for: .down, modifiers: StickyModifiers(ctrl: .oneShot, alt: .oneShot, shift: .oneShot)) == "\(esc)[1;8B")114    }115116    @Test func tildeKeys() {117        #expect(KeyEncoder.sequence(for: .pageUp) == "\(esc)[5~")118        #expect(KeyEncoder.sequence(for: .pageDown) == "\(esc)[6~")119        #expect(KeyEncoder.sequence(for: .delete) == "\(esc)[3~")120        #expect(KeyEncoder.sequence(for: .insert) == "\(esc)[2~")121        #expect(KeyEncoder.sequence(for: .delete, modifiers: StickyModifiers(ctrl: .oneShot)) == "\(esc)[3;5~")122        #expect(KeyEncoder.sequence(for: .pageUp, modifiers: StickyModifiers(shift: .oneShot)) == "\(esc)[5;2~")123    }124125    @Test func namedKeys() {126        #expect(KeyEncoder.sequence(for: .esc) == esc)127        #expect(KeyEncoder.sequence(for: .esc, modifiers: StickyModifiers(alt: .oneShot)) == esc + esc)128        #expect(KeyEncoder.sequence(for: .tab) == "\t")129        #expect(KeyEncoder.sequence(for: .tab, modifiers: StickyModifiers(shift: .oneShot)) == "\(esc)[Z")130        #expect(KeyEncoder.sequence(for: .tab, modifiers: StickyModifiers(alt: .oneShot, shift: .oneShot)) == "\(esc)\(esc)[Z")131        #expect(KeyEncoder.sequence(for: .enter) == "\r")132        #expect(KeyEncoder.sequence(for: .enter, modifiers: StickyModifiers(alt: .oneShot)) == "\(esc)\r")133        #expect(KeyEncoder.sequence(for: .backspace) == "\u{7F}")134        #expect(KeyEncoder.sequence(for: .backspace, modifiers: StickyModifiers(ctrl: .oneShot)) == "\u{08}")135        #expect(KeyEncoder.sequence(for: .backspace, modifiers: StickyModifiers(alt: .oneShot)) == "\(esc)\u{7F}")136        #expect(KeyEncoder.sequence(for: .ctrl) == "")137    }138139    @Test func controlCharacters() {140        #expect(KeyEncoder.ctrlChar("c") == "\u{03}")141        #expect(KeyEncoder.ctrlChar("C") == "\u{03}")142        #expect(KeyEncoder.ctrlChar("a") == "\u{01}")143        #expect(KeyEncoder.ctrlChar("z") == "\u{1A}")144        #expect(KeyEncoder.ctrlChar("[") == esc)145        #expect(KeyEncoder.ctrlChar("@") == "\u{00}")146        #expect(KeyEncoder.ctrlChar(" ") == "\u{00}")147        #expect(KeyEncoder.ctrlChar("2") == "\u{00}")148        #expect(KeyEncoder.ctrlChar("?") == "\u{7F}")149        #expect(KeyEncoder.ctrlChar("8") == "\u{7F}")150        #expect(KeyEncoder.ctrlChar("/") == "\u{1F}")151        #expect(KeyEncoder.ctrlChar("-") == "\u{1F}")152        #expect(KeyEncoder.ctrlChar("3") == esc)153        #expect(KeyEncoder.ctrlChar("7") == "\u{1F}")154        #expect(KeyEncoder.ctrlChar("é") == nil)155        #expect(KeyEncoder.ctrlChar("ab") == nil)156        #expect(KeyEncoder.ctrlChar("1") == nil)157    }158159    @Test func literalKeysAndInputModifiers() {160        #expect(KeyEncoder.sequence(for: "/") == "/")161        #expect(KeyEncoder.sequence(for: "c", modifiers: StickyModifiers(ctrl: .oneShot)) == "\u{03}")162        #expect(KeyEncoder.sequence(for: "x", modifiers: StickyModifiers(alt: .oneShot)) == "\(esc)x")163        #expect(KeyEncoder.sequence(for: "x", modifiers: StickyModifiers(shift: .oneShot)) == "X")164        #expect(KeyEncoder.sequence(for: "d", modifiers: StickyModifiers(ctrl: .oneShot, alt: .oneShot)) == "\(esc)\u{04}")165        let ctrl = StickyModifiers(ctrl: .oneShot)166        #expect(KeyEncoder.applyModifiers(toInput: "c", modifiers: ctrl) == "\u{03}")167        // Pastes, escape sequences and IME commits are left untouched.168        #expect(KeyEncoder.applyModifiers(toInput: "cd", modifiers: ctrl) == "cd")169        #expect(KeyEncoder.applyModifiers(toInput: "\(esc)[A", modifiers: ctrl) == "\(esc)[A")170        #expect(KeyEncoder.applyModifiers(toInput: "c", modifiers: .none) == "c")171        #expect(KeyEncoder.applyModifiers(toInput: "\t", modifiers: StickyModifiers(shift: .oneShot)) == "\(esc)[Z")172        #expect(KeyEncoder.applyModifiers(toInput: "é", modifiers: ctrl) == "é")173    }174175    @Test func stickyModifierStateMachine() {176        var m = StickyModifiers()177        #expect(!m.hasAny)178        m.tap(.ctrl)179        #expect(m.ctrl == .oneShot && m.hasAny)180        m.tap(.ctrl)181        #expect(m.ctrl == .off)182        m.tap(.ctrl)183        m.tap(.ctrl, doubleTap: true)184        #expect(m.ctrl == .locked)185        m.tap(.alt)186        m.consume()187        #expect(m.ctrl == .locked, "locked modifiers survive a key press")188        #expect(m.alt == .off, "one-shot modifiers are consumed")189        m.tap(.ctrl)190        #expect(m.ctrl == .off, "tap while locked turns it off")191        #expect(ModifierState.off.tapped(doubleTap: true) == .oneShot)192    }193194    @Test func keySequencesConsumeOneShotOnce() {195        let s = KeyEncoder.sequence(for: ["c", "c"], modifiers: StickyModifiers(ctrl: .oneShot))196        #expect(s == "\u{03}c")197        let locked = KeyEncoder.sequence(for: ["c", "shift", "d"], modifiers: StickyModifiers(ctrl: .locked))198        #expect(locked == "\u{03}\u{04}")199    }200201    @Test func descriptorsAndDefaultToolbar() {202        #expect(TerminalKey.defaultToolbar.map(\.id) == UserSettings.defaultMobileToolbar)203        for key in TerminalKey.allToolbarKeys {204            let d = TerminalKeyDescriptor(key)205            #expect(!d.label.isEmpty && !d.accessibilityLabel.isEmpty)206            #expect((d.kind == .modifier) == (key.modifier != nil))207        }208        #expect(TerminalKeyDescriptor(.up).repeats)209        #expect(!TerminalKeyDescriptor(.esc).repeats)210        #expect(TerminalKeyDescriptor("|").accessibilityLabel == "Character |")211        #expect(Set(TerminalKey.allToolbarKeys).count == TerminalKey.allToolbarKeys.count)212        #expect(TerminalKey.ctrl.modifier == .ctrl && TerminalKey.up.modifier == nil)213    }214}215216@Suite("Terminal themes")217struct ThemeTests {218    @Test func everyThemeIsComplete() {219        #expect(TerminalTheme.all.count >= 8)220        #expect(Set(TerminalTheme.all.map(\.id)).count == TerminalTheme.all.count)221        for theme in TerminalTheme.all {222            #expect(theme.ansi.count == 16)223            #expect(!theme.name.isEmpty)224            #expect(TerminalTheme.theme(id: theme.id) == theme)225            #expect((theme.background.luminance < 0.5) == theme.isDark, "\(theme.id)")226            #expect(theme.foreground.contrast(with: theme.background) >= 4.0, "\(theme.id)") // Solarized Light is 4.1 by design227        }228        #expect(TerminalTheme.theme(id: "nope") == nil)229    }230231    @Test func webPalettesMatchTheWebApplication() {232        let dark = TerminalTheme.claudeRemoteDark233        #expect(dark.background.hexString == "#080B10" && dark.foreground.hexString == "#F4F7FB" && dark.cursor.hexString == "#8F8FFF")234        #expect(dark.ansi.map(\.hexString) == ["#161D27", "#ED7777", "#42C995", "#E7B86B", "#729FFF", "#B692F6", "#5CC6D6", "#A1ADBD",235                                              "#7F8CA0", "#F59C9C", "#72DDB4", "#F1CF95", "#9DBCFF", "#CDB4FA", "#8DDCE8", "#F4F7FB"])236        #expect(dark.selection.hexString == "#8F8FFF" && abs(dark.selection.alpha - 0.349) < 0.01)237        let light = TerminalTheme.claudeRemoteLight238        #expect(light.background.hexString == "#FCFCFA" && light.foreground.hexString == "#1F2328" && light.cursor.hexString == "#5151E8")239        #expect(light.ansi.map(\.hexString) == ["#24292F", "#C4232D", "#116329", "#7D4E00", "#0860C7", "#7C3FD6", "#0F6E75", "#57606A",240                                               "#636C76", "#A40E26", "#1A7F37", "#8F5F00", "#0550AE", "#6639BA", "#0B5D63", "#3D444D"])241    }242243    @Test func lightWebPaletteIsReadable() {244        let light = TerminalTheme.claudeRemoteLight245        for (i, colour) in light.ansi.enumerated() {246            #expect(colour.contrast(with: light.background) >= 4.5, "ANSI \(i) \(colour.hexString)")247        }248    }249250    @Test func codexAccentOverridesCursorAndSelectionOnly() {251        let dark = TerminalTheme.web(dark: true, agent: .codex)252        #expect(dark.cursor.hexString == "#2DD4BF" && dark.selection.hexString == "#2DD4BF")253        #expect(dark.ansi == TerminalTheme.claudeRemoteDark.ansi && dark.background == TerminalTheme.claudeRemoteDark.background)254        let light = TerminalTheme.web(dark: false, agent: .codex)255        #expect(light.cursor.hexString == "#0B7268")256        #expect(TerminalTheme.web(dark: true, agent: .claude) == .claudeRemoteDark)257    }258259    @Test func resolutionFromSharedSettings() {260        #expect(TerminalTheme.resolve(.auto, systemIsDark: true) == .claudeRemoteDark)261        #expect(TerminalTheme.resolve(.auto, systemIsDark: false) == .claudeRemoteLight)262        #expect(TerminalTheme.resolve(.light, systemIsDark: true) == .claudeRemoteLight)263        #expect(TerminalTheme.resolve(.dark, systemIsDark: false) == .claudeRemoteDark)264        #expect(TerminalTheme.resolve(.unknown("sepia"), systemIsDark: true) == .claudeRemoteDark)265        var settings = UserSettings()266        settings.terminalFontSize = 15267        settings.mobileFontSize = 11268        settings.scrollback = 9000269        let desktop = TerminalAppearance(settings: settings, agent: .codex, systemIsDark: true)270        #expect(desktop.fontSize == 15 && desktop.scrollback == 9000 && desktop.theme.cursor.hexString == "#2DD4BF")271        #expect(TerminalAppearance(settings: settings, systemIsDark: false, compact: true).fontSize == 11)272        #expect(TerminalAppearance(fontSize: 500, scrollback: 1).fontSize == 48)273        #expect(TerminalAppearance.fontCandidates(preferred: "Fira Code").first == "Fira Code")274        #expect(TerminalAppearance.fontCandidates(preferred: "JetBrains Mono").filter { $0 == "JetBrains Mono" }.count == 1)275    }276}277
Files main
Menu

Theme