summaryrefslogtreecommitdiffstats
path: root/nixos/tests/hister.nix
blob: 9958581f180a4ee8cbb89e0950857c97ab871f29 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
{ lib, pkgs, ... }:
let
  configPathConfig = pkgs.writeText "hister-config.yml" ''
    app:
      title: NixOS Hister Config Path
      search_url: https://config.example.invalid/?q={query}
    hotkeys:
      web:
        alt+c: open_query_in_search_engine
  '';
in
{
  name = "hister";

  meta = {
    maintainers = with lib.maintainers; [ _4evy ];
  };

  nodes.machine = {
    environment.systemPackages = [ pkgs.jq ];

    systemd.tmpfiles.settings."10-hister-env"."/run/hister.env"."f" = {
      mode = "0600";
      user = "root";
      group = "root";
      argument = "HISTER__APP__ACCESS_TOKEN=test-token";
    };

    specialisation = {
      inline_settings.configuration.services.hister = {
        enable = true;
        port = 4433;
        environmentFile = "/run/hister.env";
        settings = {
          app = {
            log_level = "debug";
            title = "NixOS Hister";
            search_url = "https://search.example.invalid/?q={query}";
            open_results_on_new_tab = true;
          };
          hotkeys.web."alt+n" = "open_query_in_search_engine";
        };
      };

      config_file.configuration.services.hister = {
        enable = true;
        port = 4434;
        configPath = configPathConfig;
      };

      custom_data_dir.configuration.services.hister = {
        enable = true;
        port = 4435;
        dataDir = "/srv/hister-data";
        settings.app.title = "NixOS Hister Custom Data";
      };
    };
  };

  testScript =
    { nodes, ... }:
    let
      switchTo =
        name:
        "${nodes.machine.system.build.toplevel}/specialisation/${name}/bin/switch-to-configuration test";
    in
    ''
      start_all()

      with subtest("inline settings"):
        machine.succeed("${switchTo "inline_settings"}")
        machine.systemctl("restart hister.service")
        machine.wait_for_unit("hister.service")
        machine.wait_for_open_port(4433)
        machine.succeed("curl -fsS http://localhost:4433/ | grep -F '<title>Hister</title>'")
        machine.succeed("test $(stat -c %a /var/lib/hister) = 750")
        machine.succeed("test $(stat -c %a /run/hister) = 750")
        machine.succeed("test -s /run/hister/tui.yaml")
        machine.succeed("test -s /var/lib/hister/db.sqlite3")
        machine.succeed("test -s /var/lib/hister/.secret_key")
        machine.succeed("test -s /var/lib/hister/rules.json")
        machine.succeed(
            "curl -fsS http://localhost:4433/api/config"
            + " | jq -e "
            + "'"
            + '.baseUrl == "http://127.0.0.1:4433"'
            + ' and .title == "NixOS Hister"'
            + ' and .searchUrl == "https://search.example.invalid/?q={query}"'
            + ' and .openResultsOnNewTab == true'
            + ' and .hotkeys."alt+n" == "open_query_in_search_engine"'
            + ' and .authMode == "token"'
            + "'"
        )
        machine.fail("journalctl -u hister.service | grep -F 'Failed to create tui.yaml'")

      with subtest("configPath"):
        machine.succeed("${switchTo "config_file"}")
        machine.systemctl("restart hister.service")
        machine.wait_for_unit("hister.service")
        machine.wait_for_open_port(4434)
        machine.succeed("curl -fsS http://localhost:4434/ >/dev/null")
        machine.succeed("test $(stat -c %a /run/hister) = 750")
        machine.succeed("test -s /run/hister/tui.yaml")
        machine.succeed("test -s /var/lib/hister/db.sqlite3")
        machine.succeed("test -s /var/lib/hister/.secret_key")
        machine.succeed("test -s /var/lib/hister/rules.json")
        machine.succeed(
            "curl -fsS http://localhost:4434/api/config"
            + " | jq -e "
            + "'"
            + '.baseUrl == "http://127.0.0.1:4434"'
            + ' and .title == "NixOS Hister Config Path"'
            + ' and .searchUrl == "https://config.example.invalid/?q={query}"'
            + ' and .hotkeys."alt+c" == "open_query_in_search_engine"'
            + ' and .authMode == "none"'
            + "'"
        )
        machine.fail("journalctl -u hister.service | grep -F 'Failed to create tui.yaml'")

      with subtest("custom dataDir"):
        machine.systemctl("stop hister.service")
        machine.succeed("rm -rf /var/lib/hister")
        machine.succeed("${switchTo "custom_data_dir"}")
        machine.systemctl("restart hister.service")
        machine.wait_for_unit("hister.service")
        machine.wait_for_open_port(4435)
        machine.succeed("curl -fsS http://localhost:4435/ >/dev/null")
        machine.succeed("test $(stat -c %U:%G:%a /srv/hister-data) = hister:hister:750")
        machine.succeed("test $(stat -c %a /run/hister) = 750")
        machine.succeed("test -s /run/hister/tui.yaml")
        machine.succeed("test -s /srv/hister-data/db.sqlite3")
        machine.succeed("test -s /srv/hister-data/.secret_key")
        machine.succeed("test -s /srv/hister-data/rules.json")
        machine.succeed("test ! -e /var/lib/hister")
        machine.succeed(
            "curl -fsS http://localhost:4435/api/config"
            + " | jq -e "
            + "'"
            + '.baseUrl == "http://127.0.0.1:4435"'
            + ' and .title == "NixOS Hister Custom Data"'
            + ' and .authMode == "none"'
            + "'"
        )
        machine.fail("journalctl -u hister.service | grep -F 'Failed to create tui.yaml'")
    '';
}