|
|
@@ -1,10 +1,8 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
- "encoding/json"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
- "strconv"
|
|
|
"strings"
|
|
|
"testing"
|
|
|
|
|
|
@@ -12,17 +10,18 @@ import (
|
|
|
intvia "netdome.biz/paul/qmk-rgb/internal/via"
|
|
|
)
|
|
|
|
|
|
-// generateStub is a keyboard that answers an effect range, so the generated
|
|
|
-// scaffold can be checked without hardware. The Vial fields are what a keyboard
|
|
|
-// running Vial firmware would answer; zero versions read as a keyboard that is
|
|
|
-// not Vial.
|
|
|
+// generateStub is a keyboard that answers an effect range, so a generated names
|
|
|
+// file can be checked without hardware. The Vial fields are what a keyboard
|
|
|
+// running Vial firmware would answer; a zero version reads as one that is not
|
|
|
+// Vial.
|
|
|
type generateStub struct {
|
|
|
tops map[intvia.Channel]int
|
|
|
vial bool
|
|
|
vialIDs []uint16
|
|
|
- vialErr error
|
|
|
}
|
|
|
|
|
|
+func (g generateStub) EffectTop(ch intvia.Channel) (int, error) { return g.tops[ch], nil }
|
|
|
+
|
|
|
func (g generateStub) VialVersion() (uint32, bool, error) {
|
|
|
if !g.vial {
|
|
|
return 0, false, nil
|
|
|
@@ -30,16 +29,7 @@ func (g generateStub) VialVersion() (uint32, bool, error) {
|
|
|
return 6, true, nil
|
|
|
}
|
|
|
|
|
|
-func (g generateStub) VialEffectIDs() ([]uint16, error) {
|
|
|
- if g.vialErr != nil {
|
|
|
- return nil, g.vialErr
|
|
|
- }
|
|
|
- return g.vialIDs, nil
|
|
|
-}
|
|
|
-
|
|
|
-func (g generateStub) EffectTop(ch intvia.Channel) (int, error) {
|
|
|
- return g.tops[ch], nil
|
|
|
-}
|
|
|
+func (g generateStub) VialEffectIDs() ([]uint16, error) { return g.vialIDs, nil }
|
|
|
|
|
|
func (g generateStub) GetValue(intvia.Channel, uint8) ([]byte, error) { return []byte{0}, nil }
|
|
|
|
|
|
@@ -51,16 +41,15 @@ func (g generateStub) DetectChannels() ([]intvia.Channel, error) { return nil, n
|
|
|
|
|
|
func (g generateStub) Close() error { return nil }
|
|
|
|
|
|
-// stubGenerateTarget makes the connected keyboard a fixed one. The identifiers
|
|
|
-// are a parameter because a board the binary carries a definition for is the
|
|
|
-// case `generate` has to refuse, and a stub for the wrong board would test
|
|
|
-// nothing.
|
|
|
-func stubGenerateTarget(t *testing.T, vendorID, productID uint16, channels []intvia.Channel, tops map[intvia.Channel]int) {
|
|
|
+// stubGenerateTarget makes the connected keyboard a fixed one. The identifiers are
|
|
|
+// a parameter because a board the binary carries a definition for is a different
|
|
|
+// case from one it does not, and a stub for the wrong board would test nothing.
|
|
|
+func stubGenerateTarget(t *testing.T, vendorID, productID uint16, channels []intvia.Channel, stub generateStub) {
|
|
|
t.Helper()
|
|
|
original := openTarget
|
|
|
t.Cleanup(func() { openTarget = original })
|
|
|
openTarget = func(string) (rgbProtocol, targetDeviceData, []intvia.Channel, error) {
|
|
|
- return generateStub{tops: tops}, stubTargetData(vendorID, productID), channels, nil
|
|
|
+ return stub, stubTargetData(vendorID, productID), channels, nil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -70,181 +59,53 @@ func forceGenerateRestore(t *testing.T) func() {
|
|
|
return func() { generateForce = original }
|
|
|
}
|
|
|
|
|
|
-// The whole point of the command: a generated file names no effect, so a board
|
|
|
-// with a generated definition reports the same "no names" it reported before one
|
|
|
-// existed. A scaffold that claimed names would be a guess the tool could not
|
|
|
-// read back off the keyboard.
|
|
|
-func TestGeneratedDefinitionNamesNoEffectUntilTheUserFillsItIn(t *testing.T) {
|
|
|
- dir := t.TempDir()
|
|
|
- t.Cleanup(definitionFlagRestore(t))
|
|
|
- t.Cleanup(forceDefinitionsDir(t, dir))
|
|
|
- t.Cleanup(forceGenerateRestore(t))
|
|
|
- stubGenerateTarget(t, 0x1234, 0x5678, []intvia.Channel{intvia.ChannelRgbMatrix}, map[intvia.Channel]int{intvia.ChannelRgbMatrix: 45})
|
|
|
-
|
|
|
- cmd := NewKeyboardDefinitionsGenerateCmd()
|
|
|
- var out, errOut strings.Builder
|
|
|
- cmd.SetOut(&out)
|
|
|
- cmd.SetErr(&errOut)
|
|
|
-
|
|
|
- if err := cmd.Execute(); err != nil {
|
|
|
- t.Fatalf("definitions generate error = %v (stderr %q)", err, errOut.String())
|
|
|
- }
|
|
|
-
|
|
|
- path := onlyDefinition(t, dir)
|
|
|
- def, err := intrgb.ParseDefinition(path, []byte(mustRead(t, path)))
|
|
|
- if err != nil {
|
|
|
- t.Fatalf("ParseDefinition(%s) error = %v; the generated file has to load", path, err)
|
|
|
- }
|
|
|
- if got := len(def.Catalog.Effects(intvia.ChannelRgbMatrix)); got != 0 {
|
|
|
- t.Errorf("effects on rgb_matrix = %d, want 0 until the names are written in", got)
|
|
|
- }
|
|
|
- // The slots are still there, which is the other half: the file states which
|
|
|
- // IDs exist and leaves the naming to the user.
|
|
|
- var file struct {
|
|
|
- Menus []struct {
|
|
|
- Content []struct {
|
|
|
- Label string `json:"label"`
|
|
|
- Type string `json:"type"`
|
|
|
- Content []any `json:"content"`
|
|
|
- Options []any `json:"options"`
|
|
|
- } `json:"content"`
|
|
|
- } `json:"menus"`
|
|
|
- }
|
|
|
- if err := json.Unmarshal([]byte(mustRead(t, path)), &file); err != nil {
|
|
|
- t.Fatalf("unmarshal generated file: %v", err)
|
|
|
- }
|
|
|
- var options []any
|
|
|
- for _, menu := range file.Menus {
|
|
|
- for _, entry := range menu.Content {
|
|
|
- if entry.Type == "dropdown" {
|
|
|
- options = entry.Options
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if len(options) != 46 {
|
|
|
- t.Fatalf("effect options = %d, want 46, one per ID from 0 to 45", len(options))
|
|
|
- }
|
|
|
- for i, option := range options {
|
|
|
- pair, ok := option.([]any)
|
|
|
- if !ok || len(pair) != 2 {
|
|
|
- t.Fatalf("option %d = %v, want a name and a number", i, option)
|
|
|
- }
|
|
|
- if name, _ := pair[0].(string); name != "" {
|
|
|
- t.Errorf("option %d is named %q, want a slot with no name", i, name)
|
|
|
- }
|
|
|
- if number, _ := pair[1].(float64); int(number) != i {
|
|
|
- t.Errorf("option %d carries ID %v, want the slot numbered %d", i, pair[1], i)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Every control has to be addressed by a value key the parser recognises. A
|
|
|
- // key built by trimming a suffix and appending without the underscore is
|
|
|
- // `id_qmk_rgb_matrixbrightness`, which names nothing, and nothing in the
|
|
|
- // output above would say so.
|
|
|
- wantKeys := map[string]bool{
|
|
|
- "id_qmk_rgb_matrix_brightness": false,
|
|
|
- "id_qmk_rgb_matrix_effect": false,
|
|
|
- "id_qmk_rgb_matrix_effect_speed": false,
|
|
|
- }
|
|
|
- for _, menu := range file.Menus {
|
|
|
- for _, entry := range menu.Content {
|
|
|
- key, _ := entry.Content[0].(string)
|
|
|
- if _, ok := wantKeys[key]; !ok {
|
|
|
- t.Errorf("control %q is addressed by %q, which is not a VIA value key", entry.Label, key)
|
|
|
- continue
|
|
|
- }
|
|
|
- wantKeys[key] = true
|
|
|
- }
|
|
|
- }
|
|
|
- for key, found := range wantKeys {
|
|
|
- if !found {
|
|
|
- t.Errorf("no control addresses %q", key)
|
|
|
- }
|
|
|
- }
|
|
|
+// forceNamesDir points the names directory at a test directory. Without it a test
|
|
|
+// that writes a names file writes into the user's real data directory, which is
|
|
|
+// what happened when this override did not exist yet.
|
|
|
+func forceNamesDir(t *testing.T, dir string) func() {
|
|
|
+ t.Helper()
|
|
|
+ original := namesDirOverride
|
|
|
+ namesDirOverride = dir
|
|
|
+ return func() { namesDirOverride = original }
|
|
|
}
|
|
|
|
|
|
-// A definition a user has written is the one command they would have run to write
|
|
|
-// it, so it must survive a second run of the command.
|
|
|
-func TestGenerateDoesNotReplaceAStoredDefinition(t *testing.T) {
|
|
|
- dir := t.TempDir()
|
|
|
+// generateSetup points every writable directory at one temporary directory, so a
|
|
|
+// test cannot reach the user's.
|
|
|
+// generateSetup points the writable directories at temporary ones and returns the
|
|
|
+// names directory. They are two directories on purpose: a names file in the
|
|
|
+// definitions directory parses as a definition — it carries the board's
|
|
|
+// identifiers and no menus — and takes the board's place, which is exactly what
|
|
|
+// TestNamesFileInTheDefinitionsDirectoryIsNotADefinition is about.
|
|
|
+func generateSetup(t *testing.T) string {
|
|
|
+ t.Helper()
|
|
|
+ names := t.TempDir()
|
|
|
t.Cleanup(definitionFlagRestore(t))
|
|
|
- t.Cleanup(forceDefinitionsDir(t, dir))
|
|
|
+ t.Cleanup(forceDefinitionsDir(t, t.TempDir()))
|
|
|
+ t.Cleanup(forceNamesDir(t, names))
|
|
|
t.Cleanup(forceGenerateRestore(t))
|
|
|
- stubGenerateTarget(t, 0x1234, 0x5678, []intvia.Channel{intvia.ChannelRgbMatrix}, map[intvia.Channel]int{intvia.ChannelRgbMatrix: 45})
|
|
|
-
|
|
|
- written := `{"name":"Test Board","vendorId":"0x1234","productId":"0x5678","menus":[],"note":"hand written"}`
|
|
|
- if err := os.WriteFile(filepath.Join(dir, "test_board.json"), []byte(written), 0o644); err != nil {
|
|
|
- t.Fatal(err)
|
|
|
- }
|
|
|
-
|
|
|
- cmd := NewKeyboardDefinitionsGenerateCmd()
|
|
|
- var out, errOut strings.Builder
|
|
|
- cmd.SetOut(&out)
|
|
|
- cmd.SetErr(&errOut)
|
|
|
-
|
|
|
- err := cmd.Execute()
|
|
|
- if err == nil {
|
|
|
- t.Fatal("definitions generate = nil error, want a refusal to replace a stored definition")
|
|
|
- }
|
|
|
- if !strings.Contains(err.Error(), "--force") {
|
|
|
- t.Errorf("error = %q, want it to offer --force", err)
|
|
|
- }
|
|
|
- if got := mustRead(t, filepath.Join(dir, "test_board.json")); got != written {
|
|
|
- t.Errorf("stored file = %q, want it untouched (%q)", got, written)
|
|
|
- }
|
|
|
+ return names
|
|
|
}
|
|
|
|
|
|
-// The note beside the file is what carries the names, because a JSON file cannot
|
|
|
-// hold them: the tool would have to read comments and VIA's parser would reject
|
|
|
-// them. It has to say who wrote what, or a name is a guess wearing a count.
|
|
|
-func TestGeneratedNoteCarriesTheSpellingsAndWhoWroteThem(t *testing.T) {
|
|
|
- dir := t.TempDir()
|
|
|
- t.Cleanup(definitionFlagRestore(t))
|
|
|
- t.Cleanup(forceDefinitionsDir(t, dir))
|
|
|
- t.Cleanup(forceGenerateRestore(t))
|
|
|
- stubGenerateTarget(t, 0x1234, 0x5678, []intvia.Channel{intvia.ChannelRgbMatrix}, map[intvia.Channel]int{intvia.ChannelRgbMatrix: 45})
|
|
|
-
|
|
|
+func runGenerate(t *testing.T) (string, string) {
|
|
|
+ t.Helper()
|
|
|
cmd := NewKeyboardDefinitionsGenerateCmd()
|
|
|
var out, errOut strings.Builder
|
|
|
cmd.SetOut(&out)
|
|
|
cmd.SetErr(&errOut)
|
|
|
if err := cmd.Execute(); err != nil {
|
|
|
- t.Fatalf("definitions generate error = %v", err)
|
|
|
- }
|
|
|
-
|
|
|
- note := mustRead(t, strings.TrimSuffix(onlyDefinition(t, dir), ".json")+spottedNoteSuffix)
|
|
|
- if !strings.Contains(note, "rgb_matrix (channel 3), 46 slots, IDs 0 to 45") {
|
|
|
- t.Errorf("note = %q, want it to name the channel, the count and the ID range", note)
|
|
|
- }
|
|
|
- if !strings.Contains(note, string(slotsMeasured)) {
|
|
|
- t.Errorf("note = %q, want it to say where the slots came from", note)
|
|
|
- }
|
|
|
- // The measurement behind the names: a spelling, how many boards wrote it, and
|
|
|
- // which manufacturer most of them were.
|
|
|
- if !strings.Contains(note, "rainbow_moving_chevron") {
|
|
|
- t.Errorf("note = %q, want the spellings other definitions use", note)
|
|
|
- }
|
|
|
- if !strings.Contains(note, "boards:") {
|
|
|
- t.Errorf("note = %q, want a count of boards next to every name", note)
|
|
|
- }
|
|
|
- if !strings.Contains(note, "keychron") {
|
|
|
- t.Errorf("note = %q, want the manufacturer behind most of a name's spellings", note)
|
|
|
- }
|
|
|
- // Effect 23 is where the collection disagrees about what the number even is,
|
|
|
- // so the note has to show the runner-up rather than pick a winner.
|
|
|
- if !strings.Contains(note, "ID 23 ") {
|
|
|
- t.Errorf("note = %q, want a line for ID 23", note)
|
|
|
+ t.Fatalf("definitions generate error = %v (stderr %q)", err, errOut.String())
|
|
|
}
|
|
|
+ return out.String(), errOut.String()
|
|
|
}
|
|
|
|
|
|
-func onlyDefinition(t *testing.T, dir string) string {
|
|
|
+func onlyNamesFile(t *testing.T, dir string) string {
|
|
|
t.Helper()
|
|
|
matches, err := filepath.Glob(filepath.Join(dir, "*.json"))
|
|
|
if err != nil {
|
|
|
t.Fatal(err)
|
|
|
}
|
|
|
if len(matches) != 1 {
|
|
|
- t.Fatalf("definitions dir = %v, want one JSON file", matches)
|
|
|
+ t.Fatalf("names dir = %v, want one file", matches)
|
|
|
}
|
|
|
return matches[0]
|
|
|
}
|
|
|
@@ -258,93 +119,123 @@ func mustRead(t *testing.T, path string) string {
|
|
|
return string(data)
|
|
|
}
|
|
|
|
|
|
-// A generated scaffold names no effect, so writing one for a board the binary
|
|
|
-// carries a definition for takes that board from 46 named effects to none — and
|
|
|
-// the file in the user directory is the one that is read. This is the whole
|
|
|
-// reason the built-in set is checked and not only the directory.
|
|
|
-func TestGenerateDoesNotShadowABuiltInDefinition(t *testing.T) {
|
|
|
- dir := t.TempDir()
|
|
|
- t.Cleanup(definitionFlagRestore(t))
|
|
|
- t.Cleanup(forceDefinitionsDir(t, dir))
|
|
|
- t.Cleanup(forceGenerateRestore(t))
|
|
|
- // The Impact 80, whose definition is built into the binary.
|
|
|
- stubGenerateTarget(t, 0x36B0, 0x309F, []intvia.Channel{intvia.ChannelRgbMatrix}, map[intvia.Channel]int{intvia.ChannelRgbMatrix: 45})
|
|
|
+// A generated file names no effect, so a board reports the same "no names" it
|
|
|
+// reported before one existed. A name the tool wrote would be indistinguishable
|
|
|
+// from the manufacturer's, and nothing in a file can be read back off a keyboard
|
|
|
+// to check it.
|
|
|
+func TestGeneratedNamesFileNamesNoEffectUntilTheUserFillsItIn(t *testing.T) {
|
|
|
+ dir := generateSetup(t)
|
|
|
+ stubGenerateTarget(t, 0x1234, 0x5678, []intvia.Channel{intvia.ChannelRgbMatrix},
|
|
|
+ generateStub{tops: map[intvia.Channel]int{intvia.ChannelRgbMatrix: 45}})
|
|
|
|
|
|
- builtIn := 0
|
|
|
- for _, def := range builtInDefinitions() {
|
|
|
- if def.Matches(0x36B0, 0x309F) {
|
|
|
- builtIn = namedEffectCount(def)
|
|
|
+ runGenerate(t)
|
|
|
+
|
|
|
+ path := onlyNamesFile(t, dir)
|
|
|
+ names, err := intrgbLoadNamesFile(path)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatalf("LoadNamesFile(%s) error = %v; the generated file has to load", path, err)
|
|
|
+ }
|
|
|
+ if got := len(names.Effects(intvia.ChannelRgbMatrix)); got != 0 {
|
|
|
+ t.Errorf("effects on rgb_matrix = %d, want 0 until the names are written in", got)
|
|
|
+ }
|
|
|
+ // The slots are still there, which is the other half: the file says which IDs
|
|
|
+ // the board has and leaves the naming to the user.
|
|
|
+ body := mustRead(t, path)
|
|
|
+ for _, want := range []string{`"0": ""`, `"7": ""`, `"45": ""`} {
|
|
|
+ if !strings.Contains(body, want) {
|
|
|
+ t.Errorf("generated file has no empty slot %s", want)
|
|
|
}
|
|
|
}
|
|
|
- if builtIn == 0 {
|
|
|
- t.Fatal("the binary carries no definition for 0x36B0/0x309F, so there is nothing to shadow")
|
|
|
+ // And it is one line per effect, not a nested menu description to dig through.
|
|
|
+ if lines := strings.Count(body, "\n"); lines > 60 {
|
|
|
+ t.Errorf("generated file is %d lines, want roughly one per effect", lines)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// A names file is the one the user would have run the command to write, so a
|
|
|
+// second run must not replace it.
|
|
|
+func TestGenerateDoesNotReplaceAStoredNamesFile(t *testing.T) {
|
|
|
+ dir := generateSetup(t)
|
|
|
+ stubGenerateTarget(t, 0x1234, 0x5678, []intvia.Channel{intvia.ChannelRgbMatrix},
|
|
|
+ generateStub{tops: map[intvia.Channel]int{intvia.ChannelRgbMatrix: 45}})
|
|
|
+
|
|
|
+ written := `{"vendorId":"0x1234","productId":"0x5678","channels":{"rgb_matrix":{"7":"mine"}}}`
|
|
|
+ if err := os.WriteFile(filepath.Join(dir, "board.json"), []byte(written), 0o644); err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
}
|
|
|
|
|
|
cmd := NewKeyboardDefinitionsGenerateCmd()
|
|
|
var out, errOut strings.Builder
|
|
|
cmd.SetOut(&out)
|
|
|
cmd.SetErr(&errOut)
|
|
|
-
|
|
|
err := cmd.Execute()
|
|
|
if err == nil {
|
|
|
- t.Fatal("definitions generate = nil error, want a refusal to shadow a built-in definition")
|
|
|
+ t.Fatal("definitions generate = nil error, want a refusal to replace a stored names file")
|
|
|
}
|
|
|
- if !strings.Contains(err.Error(), "carries a definition") {
|
|
|
- t.Errorf("error = %q, want it to say the definition is one the binary carries", err)
|
|
|
+ if !strings.Contains(err.Error(), "--force") {
|
|
|
+ t.Errorf("error = %q, want it to offer --force", err)
|
|
|
}
|
|
|
- if !strings.Contains(err.Error(), strconv.Itoa(builtIn)) {
|
|
|
- t.Errorf("error = %q, want it to name how many effects would be lost (%d)", err, builtIn)
|
|
|
+ if got := mustRead(t, filepath.Join(dir, "board.json")); got != written {
|
|
|
+ t.Errorf("stored file = %q, want it untouched (%q)", got, written)
|
|
|
}
|
|
|
- if entries, _ := filepath.Glob(filepath.Join(dir, "*.json")); len(entries) != 0 {
|
|
|
- t.Errorf("definitions dir = %v, want nothing written", entries)
|
|
|
+}
|
|
|
+
|
|
|
+// The note beside the file is what carries the names, because a JSON file cannot:
|
|
|
+// the tool would have to read comments and VIA's parser would reject them. It has
|
|
|
+// to say who wrote what, or a name is a guess wearing a count.
|
|
|
+func TestGeneratedNoteCarriesTheSpellingsAndWhoWroteThem(t *testing.T) {
|
|
|
+ dir := generateSetup(t)
|
|
|
+ stubGenerateTarget(t, 0x1234, 0x5678, []intvia.Channel{intvia.ChannelRgbMatrix},
|
|
|
+ generateStub{tops: map[intvia.Channel]int{intvia.ChannelRgbMatrix: 45}})
|
|
|
+
|
|
|
+ runGenerate(t)
|
|
|
+
|
|
|
+ note := mustRead(t, strings.TrimSuffix(onlyNamesFile(t, dir), ".json")+spottedNoteSuffix)
|
|
|
+ for _, want := range []string{
|
|
|
+ "rgb_matrix (channel 3)",
|
|
|
+ "rainbow_moving_chevron",
|
|
|
+ "boards:",
|
|
|
+ "keychron",
|
|
|
+ string(slotsMeasured),
|
|
|
+ } {
|
|
|
+ if !strings.Contains(note, want) {
|
|
|
+ t.Errorf("note does not carry %q", want)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// A Vial keyboard lists its own rgb_matrix effect IDs, and the tool must use
|
|
|
-// those rather than writing above the top: the list is the firmware answering
|
|
|
-// without being touched.
|
|
|
+// A Vial keyboard lists its own rgb_matrix effect IDs, and the tool must use those
|
|
|
+// rather than writing above the top: the list is the firmware answering without
|
|
|
+// being touched.
|
|
|
func TestGenerateUsesVialsOwnListWhenTheFirmwareIsVial(t *testing.T) {
|
|
|
- dir := t.TempDir()
|
|
|
- t.Cleanup(definitionFlagRestore(t))
|
|
|
- t.Cleanup(forceDefinitionsDir(t, dir))
|
|
|
- t.Cleanup(forceGenerateRestore(t))
|
|
|
-
|
|
|
- original := openTarget
|
|
|
- t.Cleanup(func() { openTarget = original })
|
|
|
- openTarget = func(string) (rgbProtocol, targetDeviceData, []intvia.Channel, error) {
|
|
|
- stub := generateStub{
|
|
|
+ dir := generateSetup(t)
|
|
|
+ stubGenerateTarget(t, 0x1234, 0x5678,
|
|
|
+ []intvia.Channel{intvia.ChannelRgbMatrix, intvia.ChannelRgblight},
|
|
|
+ generateStub{
|
|
|
tops: map[intvia.Channel]int{intvia.ChannelRgbMatrix: 45, intvia.ChannelRgblight: 6},
|
|
|
vial: true,
|
|
|
vialIDs: []uint16{0, 1, 2, 40, 45},
|
|
|
- }
|
|
|
- return stub, stubTargetData(0x1234, 0x5678), []intvia.Channel{intvia.ChannelRgbMatrix, intvia.ChannelRgblight}, nil
|
|
|
- }
|
|
|
+ })
|
|
|
|
|
|
- cmd := NewKeyboardDefinitionsGenerateCmd()
|
|
|
- var out, errOut strings.Builder
|
|
|
- cmd.SetOut(&out)
|
|
|
- cmd.SetErr(&errOut)
|
|
|
- if err := cmd.Execute(); err != nil {
|
|
|
- t.Fatalf("definitions generate error = %v (stderr %q)", err, errOut.String())
|
|
|
- }
|
|
|
+ out, _ := runGenerate(t)
|
|
|
|
|
|
- if !strings.Contains(out.String(), "Vial firmware") {
|
|
|
- t.Errorf("stdout = %q, want it to say the keyboard is Vial", out.String())
|
|
|
+ if !strings.Contains(out, "Vial firmware") {
|
|
|
+ t.Errorf("stdout = %q, want it to say the keyboard is Vial", out)
|
|
|
}
|
|
|
- if !strings.Contains(out.String(), experimentalVial) {
|
|
|
- t.Errorf("stdout = %q, want the Vial path marked %s", out.String(), experimentalVial)
|
|
|
+ if !strings.Contains(out, experimentalVial) {
|
|
|
+ t.Errorf("stdout = %q, want the Vial path marked %s", out, experimentalVial)
|
|
|
}
|
|
|
- // rgb_matrix comes from Vial and keeps the firmware's own numbering, gaps and
|
|
|
- // all; rgblight has no Vial list and falls back to the clamp.
|
|
|
- if !strings.Contains(out.String(), "0, 1, 2, 40, 45") {
|
|
|
- t.Errorf("stdout = %q, want the rgb_matrix slots in the firmware's numbering", out.String())
|
|
|
+ // rgb_matrix keeps the firmware's own numbering, gaps and all; rgblight has no
|
|
|
+ // Vial list and falls back to the clamp.
|
|
|
+ if !strings.Contains(out, "0, 1, 2, 40, 45") {
|
|
|
+ t.Errorf("stdout = %q, want the rgb_matrix slots in the firmware's numbering", out)
|
|
|
}
|
|
|
- if !strings.Contains(out.String(), string(slotsReported)) || !strings.Contains(out.String(), string(slotsMeasured)) {
|
|
|
- t.Errorf("stdout = %q, want both sources named per channel", out.String())
|
|
|
+ if !strings.Contains(out, string(slotsReported)) || !strings.Contains(out, string(slotsMeasured)) {
|
|
|
+ t.Errorf("stdout = %q, want both sources named per channel", out)
|
|
|
}
|
|
|
|
|
|
// The note must not put QMK's spellings next to numbers that are not QMK's.
|
|
|
- note := mustRead(t, strings.TrimSuffix(onlyDefinition(t, dir), ".json")+spottedNoteSuffix)
|
|
|
+ note := mustRead(t, strings.TrimSuffix(onlyNamesFile(t, dir), ".json")+spottedNoteSuffix)
|
|
|
if strings.Contains(note, "rainbow_moving_chevron") {
|
|
|
t.Errorf("note = %q, want no QMK spellings beside a Vial numbering", note)
|
|
|
}
|
|
|
@@ -356,34 +247,62 @@ func TestGenerateUsesVialsOwnListWhenTheFirmwareIsVial(t *testing.T) {
|
|
|
// A Vial keyboard built without VIALRGB_ENABLE answers nothing, and a board that
|
|
|
// cannot list its effects can still be asked by writing above the top.
|
|
|
func TestGenerateFallsBackToTheClampWhenVialListsNothing(t *testing.T) {
|
|
|
- dir := t.TempDir()
|
|
|
- t.Cleanup(definitionFlagRestore(t))
|
|
|
- t.Cleanup(forceDefinitionsDir(t, dir))
|
|
|
- t.Cleanup(forceGenerateRestore(t))
|
|
|
+ dir := generateSetup(t)
|
|
|
+ stubGenerateTarget(t, 0x1234, 0x5678, []intvia.Channel{intvia.ChannelRgbMatrix},
|
|
|
+ generateStub{tops: map[intvia.Channel]int{intvia.ChannelRgbMatrix: 45}, vial: true})
|
|
|
|
|
|
- original := openTarget
|
|
|
- t.Cleanup(func() { openTarget = original })
|
|
|
- openTarget = func(string) (rgbProtocol, targetDeviceData, []intvia.Channel, error) {
|
|
|
- stub := generateStub{
|
|
|
- tops: map[intvia.Channel]int{intvia.ChannelRgbMatrix: 45},
|
|
|
- vial: true,
|
|
|
- vialIDs: nil,
|
|
|
- }
|
|
|
- return stub, stubTargetData(0x1234, 0x5678), []intvia.Channel{intvia.ChannelRgbMatrix}, nil
|
|
|
- }
|
|
|
+ out, _ := runGenerate(t)
|
|
|
|
|
|
- cmd := NewKeyboardDefinitionsGenerateCmd()
|
|
|
- var out, errOut strings.Builder
|
|
|
- cmd.SetOut(&out)
|
|
|
- cmd.SetErr(&errOut)
|
|
|
- if err := cmd.Execute(); err != nil {
|
|
|
- t.Fatalf("definitions generate error = %v", err)
|
|
|
- }
|
|
|
- if !strings.Contains(out.String(), string(slotsMeasured)) {
|
|
|
- t.Errorf("stdout = %q, want the clamp used when Vial lists nothing", out.String())
|
|
|
+ if !strings.Contains(out, string(slotsMeasured)) {
|
|
|
+ t.Errorf("stdout = %q, want the clamp used when Vial lists nothing", out)
|
|
|
}
|
|
|
- note := mustRead(t, strings.TrimSuffix(onlyDefinition(t, dir), ".json")+spottedNoteSuffix)
|
|
|
+ note := mustRead(t, strings.TrimSuffix(onlyNamesFile(t, dir), ".json")+spottedNoteSuffix)
|
|
|
if !strings.Contains(note, "rainbow_moving_chevron") {
|
|
|
t.Errorf("note = %q, want the QMK spellings again, which is what the clamp gives", note)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+// The point of storing names apart from the definition: a name the user writes
|
|
|
+// replaces the vendor's for that one effect, and the rest of the file still stands.
|
|
|
+// The board is the Impact 80, whose definition the binary carries, so "the rest"
|
|
|
+// means real names the vendor wrote.
|
|
|
+func TestANameTheUserWroteOverridesOneEffectAndLeavesTheRest(t *testing.T) {
|
|
|
+ generateSetup(t)
|
|
|
+ names := t.TempDir()
|
|
|
+ t.Cleanup(forceNamesDir(t, names))
|
|
|
+
|
|
|
+ body := `{"name":"Impact 80","vendorId":"0x36B0","productId":"0x309F",
|
|
|
+ "channels":{"rgb_matrix":{"7":"my_chevron"}}}`
|
|
|
+ if err := os.WriteFile(filepath.Join(names, "impact80.json"), []byte(body), 0o644); err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ catalog, _, err := resolveCatalogFor(0x36B0, 0x309F)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatalf("resolveCatalogFor() error = %v", err)
|
|
|
+ }
|
|
|
+ if got := catalog.EffectName(intvia.ChannelRgbMatrix, 7); got != "my_chevron" {
|
|
|
+ t.Errorf("EffectName(7) = %q, want the user's name", got)
|
|
|
+ }
|
|
|
+ if got := catalog.EffectSource(intvia.ChannelRgbMatrix, 7); got != intrgbSourceUser {
|
|
|
+ t.Errorf("EffectSource(7) = %q, want %q", got, intrgbSourceUser)
|
|
|
+ }
|
|
|
+ // ID 8 is band_pinwheel_sat in the Impact 80's own file, and the user's file
|
|
|
+ // said nothing about it, so it has to still be the vendor's name and carry no
|
|
|
+ // source of its own.
|
|
|
+ if got := catalog.EffectName(intvia.ChannelRgbMatrix, 8); got != "band_pinwheel_sat" {
|
|
|
+ t.Errorf("EffectName(8) = %q, want the vendor's name left alone", got)
|
|
|
+ }
|
|
|
+ if got := catalog.EffectSource(intvia.ChannelRgbMatrix, 8); got != intrgbSourceVendor {
|
|
|
+ t.Errorf("EffectSource(8) = %q, want an empty source for the vendor's", got)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// intrgbLoadNamesFile and intrgbSourceUser keep the test reading through the
|
|
|
+// internal package without importing it twice under two names.
|
|
|
+func intrgbLoadNamesFile(path string) (*intrgb.Names, error) { return intrgb.LoadNamesFile(path) }
|
|
|
+
|
|
|
+const (
|
|
|
+ intrgbSourceUser = intrgb.SourceUser
|
|
|
+ intrgbSourceVendor = intrgb.SourceVendor
|
|
|
+)
|