Repository navigation
Generator: Kanata decides what is a key, so key names are never typed as letters #1329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| @testable import KeyPathAppKit | ||
| @testable import KeyPathCore | ||
| import KeyPathRulesCore | ||
| @preconcurrency import XCTest | ||
|
|
||
| /// The generator asks Kanata, through the bridge, whether a token is a key. | ||
| /// A key it knows is emitted as that key, so admission can refuse it with a | ||
| /// reason; only a word Kanata does not know is typed as a macro. | ||
| @MainActor | ||
| final class GeneratorKeyNameTests: KeyPathTestCase { | ||
| private var directory: URL! | ||
|
|
||
| override func setUp() async throws { | ||
| try await super.setUp() | ||
| try SessionBridgeTestFixture.requireAvailable() | ||
| BackendCapabilityProfile.testOverride = SessionBridgeTestFixture.profile | ||
| directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) | ||
| try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) | ||
| } | ||
|
|
||
| override func tearDown() async throws { | ||
| BackendCapabilityProfile.testOverride = nil | ||
| if let directory { try? FileManager.default.removeItem(at: directory) } | ||
| directory = nil | ||
| try await super.tearDown() | ||
| } | ||
|
|
||
| func testKanataKeysAreEmittedAsKeysAndWordsAreTyped() { | ||
| XCTAssertEqual(KanataKeyConverter.convertToKanataSequence("f21"), "f21") | ||
| XCTAssertEqual(KanataKeyConverter.convertToKanataSequence("calc"), "calc") | ||
| XCTAssertEqual(KanataKeyConverter.convertToKanataSequence("esc"), "esc") | ||
| XCTAssertEqual(KanataKeyConverter.convertToKanataSequence("hello"), "(macro h e l l o)") | ||
| XCTAssertEqual(KanataKeyConverter.convertToKanataSequence("notakey"), "(macro n o t a k e y)") | ||
| } | ||
|
|
||
| func testTheProfileTellsKeysFromWordsAndSaysWhenItCannot() { | ||
| let profile = SessionBridgeTestFixture.profile | ||
| XCTAssertEqual(profile.knowsKey("f21"), true) | ||
| XCTAssertEqual(profile.knowsKey("volu"), true) | ||
| XCTAssertEqual(profile.knowsKey("hello"), false) | ||
| XCTAssertNil(BackendCapabilityProfile(backend: .session) { _ in nil }.knowsKey("f21"), "no bridge, no answer") | ||
| } | ||
|
|
||
| /// `f21` used to become `(macro f 2 1)` and type three characters. Now it | ||
| /// is a key the session cannot send, refused before anything is written. | ||
| func testAKeyTheSessionCannotSendIsRefusedNotTyped() async throws { | ||
| let collections = RuleCollectionStore.testStore(at: directory.appendingPathComponent("RuleCollections.json")) | ||
| let rules = CustomRulesStore.testStore(at: directory.appendingPathComponent("CustomRules.json")) | ||
| let service = ConfigurationService.sessionTestService(configDirectory: directory.path, ruleCollectionStore: collections, customRulesStore: rules) | ||
| let manager = RuleCollectionsManager(ruleCollectionStore: collections, customRulesStore: rules, configurationService: service) | ||
| manager.ruleCollections = [] | ||
| manager.customRules = [] | ||
| let rule = CustomRule(title: "Twenty-one", input: "f9", action: .keystroke(key: "f21")) | ||
| XCTAssertEqual(manager.reviewAdmission(forProposed: rule).refusal, | ||
| "Twenty-one sends f21, which the current backend can't send.") | ||
| do { | ||
| try await service.saveConfiguration(ruleCollections: [], customRules: [rule]) | ||
| XCTFail("an output the session cannot send must not be saved") | ||
| } catch { | ||
| XCTAssertTrue(error.localizedDescription.contains("driverless session"), error.localizedDescription) | ||
| } | ||
| XCTAssertFalse(FileManager.default.fileExists(atPath: service.configurationPath) | ||
| && ((try? String(contentsOfFile: service.configurationPath, encoding: .utf8))?.contains("(macro f 2 1)") ?? false)) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the live bridge is available, KeyPath aliases that Kanata does not recognize verbatim—such as
escape,space,return, andcommand—produceknown == false, so this early return bypasses the fallback alias set and the laterconvertToKanataKeymappings. Existing mappings then generate(macro e s c a p e),(macro s p a c e), etc. instead ofesc,spc,ret, orlmet; canonicalize the token before querying the bridge or preserve the alias check ahead of this return.Useful? React with 👍 / 👎.