▍ humdrum codex / ticktock v0.0.2
license AGPL-3.0
1.6 KB raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package autotrack

import (
	"os"
	"path/filepath"
	"testing"
)

func TestParseHIDIdle(t *testing.T) {
	out := `+-o IOHIDSystem  <class IOHIDSystem, id 0x100000456>
    {
      "HIDIdleTime" = 2216064029
    }`
	if got := parseHIDIdle(out); got != 2 { // 2216064029 ns → 2 s
		t.Errorf("parseHIDIdle=%d, want 2", got)
	}
	if got := parseHIDIdle("no idle info here"); got != 0 {
		t.Errorf("absent key should give 0, got %d", got)
	}
	if got := parseHIDIdle(`"HIDIdleTime" = garbage`); got != 0 {
		t.Errorf("malformed value should give 0, got %d", got)
	}
}

func TestParseLocked(t *testing.T) {
	if !parseLocked(`    "CGSSessionScreenIsLocked" = Yes`) {
		t.Error("locked output should parse true")
	}
	if parseLocked("") || parseLocked(`"SomethingElse"=Yes`) {
		t.Error("unlocked output should parse false")
	}
}

func TestParseWinctx(t *testing.T) {
	app, title := parseWinctx([]byte(`{"app":"Trace","title":"notes — repo"}`))
	if app != "Trace" || title != "notes — repo" {
		t.Errorf("got %q/%q", app, title)
	}
	if app, title := parseWinctx([]byte("garbage")); app != "" || title != "" {
		t.Errorf("bad json should give empties, got %q/%q", app, title)
	}
}

func TestHelperPathFallsBackToPATH(t *testing.T) {
	dir := t.TempDir()
	bin := filepath.Join(dir, "fakehelper")
	if err := os.WriteFile(bin, []byte("#!/bin/sh\n"), 0o755); err != nil {
		t.Fatal(err)
	}
	t.Setenv("PATH", dir)
	if got := HelperPath("fakehelper"); got != bin {
		t.Errorf("HelperPath=%q, want %q", got, bin)
	}
	if got := HelperPath("definitely-not-a-helper"); got != "" {
		t.Errorf("missing helper should give empty, got %q", got)
	}
}