{ inputs, pkgs, ... }:
let
  sharedModule = {
    # Since it's common for CI not to have $DISPLAY available, explicitly disable graphics support
    virtualisation.graphics = false;
  };
in
pkgs.nixosTest {
  name = "hetzner-sets-ipv6";
  nodes = {
    machine =
      { ... }:
      {
        imports = [
          inputs.self.nixosModules.hetzner
          inputs.self.nixosModules.systemd-boot
          inputs.self.nixosModules."virtualisation/qemu-guest"
          inputs.disko.nixosModules.disko
          sharedModule
        ];
        khscodes.hetzner = {
          enable = true;
          metadataApiUri = "http://metadata/metadata.yml";
        };
        system.stateVersion = "25.05";
      };
    metadata =
      { ... }:
      {
        imports = [ sharedModule ];
        services.nginx = {
          enable = true;
          virtualHosts = {
            "metafata" = {
              root = ./root;
            };
          };
        };
        networking.firewall.allowedTCPPorts = [ 80 ];
        system.stateVersion = "25.05";
      };
  };
  testScript = ''
    metadata.start()
    metadata.wait_for_unit("nginx.service")
    metadata.wait_for_open_port(80)
    machine.start()
    machine.wait_for_unit("hetzner-static-ip.service")
    ipv6 = machine.succeed("ip addr")
    assert "dead:beef:cafe::1" in ipv6
    # Ensure that rerunning the service works, even when the ip address is already set
    machine.succeed("systemctl restart hetzner-static-ip.service")
  '';
}
