Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

7.8 KB
Raw Blame
Code.require_file("../generate.exs", __DIR__)

defmodule Firecrawl.GeneratorTest do
  use ExUnit.Case, async: false
  import ExUnit.CaptureIO

  # The generator pulls the OpenAPI spec over the network and writes its text
  # into Elixir source. Anything that reaches a string literal or heredoc must
  # arrive as data, never as code that runs while the SDK compiles.
  @marker Path.join(System.tmp_dir!(), "firecrawl_generator_test_pwned")

  # No double quotes anywhere, so an escaper that only handles quotes leaves
  # the interpolation intact.
  @hostile "see \#{File.write!(~c'#{@marker}', ~c'x')} for details"

  setup do
    File.rm(@marker)
    on_exit(fn -> File.rm(@marker) end)
    :ok
  end

  defp compile_quietly(source) do
    capture_io(:stderr, fn -> Code.compile_string(source) end)
  end

  describe "build_deprecated/1" do
    test "emits nothing when the operation is not deprecated" do
      assert Firecrawl.Generator.build_deprecated(%{}) == nil
      assert Firecrawl.Generator.build_deprecated(%{"deprecated" => false}) == nil
    end

    test "falls back to a generic note" do
      line = Firecrawl.Generator.build_deprecated(%{"deprecated" => true})
      assert line =~ ~r/^  @deprecated "Deprecated in the Firecrawl API/
    end

    test "a hostile note is carried as data and cannot run at compile time" do
      line =
        Firecrawl.Generator.build_deprecated(%{
          "deprecated" => true,
          "x-deprecation-note" => @hostile
        })

      warnings =
        compile_quietly("""
        defmodule Firecrawl.GeneratorTest.Hostile do
        #{line}
          def f, do: :ok
        end

        defmodule Firecrawl.GeneratorTest.HostileCaller do
          def g, do: Firecrawl.GeneratorTest.Hostile.f()
        end
        """)

      refute File.exists?(@marker), "the note was evaluated while compiling"
      # The caller's deprecation warning must quote the note verbatim, which
      # proves it survived as a plain string rather than being interpreted.
      assert warnings =~ "is deprecated. " <> @hostile
    end

    test "quotes, backslashes and newlines cannot break out of the literal" do
      note = ~S(a "quoted" note with a \ backslash) <> "\nand a newline"

      line =
        Firecrawl.Generator.build_deprecated(%{
          "deprecated" => true,
          "x-deprecation-note" => note
        })

      warnings =
        compile_quietly("""
        defmodule Firecrawl.GeneratorTest.Escapes do
        #{line}
          def f, do: :ok
        end

        defmodule Firecrawl.GeneratorTest.EscapesCaller do
          def g, do: Firecrawl.GeneratorTest.Escapes.f()
        end
        """)

      assert warnings =~ "is deprecated. " <> note
    end
  end

  test "the hostile payload really is a live interpolation, not a reference to @marker" do
    assert String.contains?(@hostile, @marker)
    refute String.contains?(@hostile, "@marker")

    # Unescaped, it must fire, or none of the tests below prove anything.
    compile_quietly("""
    defmodule Firecrawl.GeneratorTest.Unescaped do
      @doc "#{@hostile}"
      def f, do: :ok
    end
    """)

    assert File.exists?(@marker)
  end

  describe "escape_string_literal/1" do
    test "leaves an ordinary path untouched" do
      assert Firecrawl.Generator.escape_string_literal("/search/research/papers/{id}") ==
               "/search/research/papers/{id}"
    end

    test "a hostile path inside a url literal cannot run or break out" do
      path = Firecrawl.Generator.escape_string_literal(~S(/x" <> ) <> @hostile <> ~S( <> "/y))

      [{mod, _}] =
        Code.compile_string("""
        defmodule Firecrawl.GeneratorTest.HostilePath do
          def url, do: "#{path}"
        end
        """)

      refute File.exists?(@marker), "the path was evaluated while compiling"
      assert mod.url() =~ "firecrawl_generator_test_pwned"
    end
  end

  describe "escape_source_text/1" do
    test "leaves ordinary spec text untouched" do
      assert Firecrawl.Generator.escape_source_text("Scrape a single URL") ==
               "Scrape a single URL"
    end

    test "a hostile summary inside a @doc heredoc cannot run at compile time" do
      summary = Firecrawl.Generator.escape_source_text(@hostile)

      compile_quietly("""
      defmodule Firecrawl.GeneratorTest.HostileDoc do
        @doc \"\"\"
        #{summary}
        \"\"\"
        def f, do: :ok
      end
      """)

      refute File.exists?(@marker), "the summary was evaluated while compiling"
    end

    test "a heredoc terminator in the summary does not end the docstring early" do
      summary =
        Firecrawl.Generator.escape_source_text(~S(ends the doc """ def injected, do: :owned))

      [{mod, _}] =
        Code.compile_string("""
        defmodule Firecrawl.GeneratorTest.Terminator do
          @doc \"\"\"
          #{summary}
          \"\"\"
          def f, do: :ok
        end
        """)

      refute function_exported?(mod, :injected, 0)
      assert function_exported?(mod, :f, 0)
    end
  end

  describe "regeneration" do
    @lib_path Path.expand("../lib/firecrawl.ex", __DIR__)
    @spec_path Path.expand("../openapi.json", __DIR__)

    defp hand_written, do: @lib_path |> File.read!() |> Firecrawl.Generator.hand_written_region()

    test "lib/firecrawl.ex is exactly what generate.exs produces from openapi.json" do
      spec = @spec_path |> File.read!() |> Jason.decode!()
      lib = File.read!(@lib_path)

      assert Firecrawl.Generator.generate_module(spec, hand_written()) == lib,
             "lib/firecrawl.ex drifted from the generator. Edit generate.exs or the " <>
               "HAND-WRITTEN region, then run: FIRECRAWL_OPENAPI_SPEC=openapi.json mix run generate.exs"
    end

    test "get_parse_formats is emitted once even when the spec declares GET /parse/formats" do
      spec = %{
        "paths" => %{
          "/parse/formats" => %{
            "get" => %{"operationId" => "getParseFormats", "summary" => "List formats"}
          }
        }
      }

      code = Firecrawl.Generator.generate_module(spec, hand_written())

      assert length(Regex.scan(~r/^  def get_parse_formats\(/m, code)) == 1
      assert length(Regex.scan(~r/^  def get_parse_formats!\(/m, code)) == 1
    end

    test "a spec operation that clashes with the HAND-WRITTEN region stops generation" do
      spec = %{
        "paths" => %{
          "/agent/{jobId}/trace-v2" => %{
            "get" => %{
              "operationId" => "getAgentTrace",
              "parameters" => [%{"name" => "jobId", "in" => "path"}]
            }
          }
        }
      }

      assert_raise RuntimeError, ~r/clash with the HAND-WRITTEN region: get_agent_trace/, fn ->
        Firecrawl.Generator.generate_module(spec, hand_written())
      end
    end

    test "$ref path parameters become interpolations, never literal holes" do
      spec = %{
        "components" => %{"parameters" => %{"ThingId" => %{"name" => "thingId", "in" => "path"}}},
        "paths" => %{
          "/things/{thingId}" => %{
            "get" => %{
              "operationId" => "getThing",
              "parameters" => [%{"$ref" => "#/components/parameters/ThingId"}]
            }
          }
        }
      }

      code = Firecrawl.Generator.generate_module(spec, hand_written())

      assert code =~ ~S|def get_thing(thing_id, opts \\ []) do|
      assert code =~ ~S|url: "/things/#{thing_id}"|
    end

    test "a path parameter the spec never declares stops generation" do
      spec = %{"paths" => %{"/things/{thingId}" => %{"get" => %{"operationId" => "getThing"}}}}

      assert_raise ArgumentError, ~r/unresolved path parameter/, fn ->
        Firecrawl.Generator.generate_module(spec, hand_written())
      end
    end

    test "a lib without a HAND-WRITTEN region is refused" do
      assert_raise RuntimeError, ~r/exactly one HAND-WRITTEN region/, fn ->
        Firecrawl.Generator.hand_written_region("defmodule Firecrawl do\nend\n")
      end
    end
  end
end