# tests for the nixos options to jsonschema converter
# run these tests via `nix-unit ./test.nix`
{
  lib ? (import <nixpkgs> { }).lib,
  slib ? (import ./. { inherit lib; } { }),
}:
let
  filterSchema =
    schema: lib.filterAttrsRecursive (name: _value: name != "$exportedModuleInfo") schema;
in
{
  testParseOptions = {
    expr = filterSchema (slib.parseModule ./example-interface.nix);
    expected = builtins.fromJSON (builtins.readFile ./example-schema.json);
  };

  testParseNestedOptions =
    let
      evaled = lib.evalModules {
        modules = [
          {
            options.foo.bar = lib.mkOption {
              type = lib.types.bool;
            };
            options.foo.baz = lib.mkOption {
              type = lib.types.bool;
              default = false;
            };
          }
        ];
      };
    in
    {
      expr = filterSchema (slib.parseOptions evaled.options { });
      expected = {
        "$schema" = "http://json-schema.org/draft-07/schema#";
        additionalProperties = false;
        properties = {
          foo = {
            additionalProperties = false;
            properties = {
              bar = {
                type = "boolean";
              };
              baz = {
                type = "boolean";
                default = false;
              };
            };
            required = [ "bar" ];
            type = "object";
          };
        };
        type = "object";
        required = [ "foo" ];
      };
    };

  testFreeFormOfInt =
    let
      default = {
        foo = 1;
        bar = 2;
      };
    in
    {
      expr = filterSchema (
        slib.parseOptions
          (lib.evalModules {
            modules = [
              {
                freeformType = with lib.types; attrsOf int;
                options = {
                  enable = lib.mkEnableOption "enable this";
                };
              }
              default
            ];
          }).options
          { }
      );
      expected = {
        "$schema" = "http://json-schema.org/draft-07/schema#";
        additionalProperties = {
          type = "integer";
        };
        properties = {
          enable = {
            default = false;
            description = "Whether to enable enable this.";
            examples = [ true ];
            type = "boolean";
          };
        };
        required = [ ];
        type = "object";
      };
    };

}
