{ config, pkgs, ... }:
let
  hmPkgs = pkgs.extend (
    _self: _super: {
      aerospace = config.lib.test.mkStubPackage {
        name = "aerospace";
        buildScript = ''
          mkdir -p $out/bin
          touch $out/bin/aerospace
          chmod 755 $out/bin/aerospace
        '';
      };
    }
  );
in
{
  xdg.enable = false;

  programs.aerospace = {
    enable = true;
    package = hmPkgs.aerospace;

    launchd.enable = true;

    settings = {
      gaps = {
        outer.left = 8;
        outer.bottom = 8;
        outer.top = 8;
        outer.right = 8;
      };
      mode.main.binding = {
        alt-enter = ''
          exec-and-forget osascript -e '
                     tell application "Terminal"
                         do script
                         activate
                     end tell'
        '';
        alt-h = "focus left";
        alt-j = "focus down";
        alt-k = "focus up";
        alt-l = "focus right";
      };
      on-window-detected = [
        {
          "if".app-id = "com.apple.finder";
          run = "move-node-to-workspace 9";
        }

        {
          "if" = {
            app-id = "com.apple.systempreferences";
            app-name-regex-substring = "settings";
            window-title-regex-substring = "substring";
            workspace = "workspace-name";
            during-aerospace-startup = true;
          };
          check-further-callbacks = true;
          run = [
            "layout floating"
            "move-node-to-workspace S"
          ];
        }
      ];
    };
  };

  nmt.script = ''
    assertPathNotExists "home-files/.config/aerospace";
    assertFileExists "home-files/.aerospace.toml"
    assertFileContent "home-files/.aerospace.toml" ${./settings-expected.toml}
    assertFileContains activate 'list-modes --current >/dev/null 2>&1'

    serviceFile=$(normalizeStorePaths LaunchAgents/org.nix-community.home.aerospace.plist)
    assertFileExists $serviceFile
    assertFileContent "$serviceFile" ${./aerospace-service-expected.plist}
  '';
}
