Reqord.Tasks.Helpers (reqord v0.4.0)

View Source

Shared helper functions for Reqord Mix tasks.

Provides common utilities for:

  • Resolving cassette paths (relative, absolute, short names)
  • Loading and parsing cassette entries
  • Finding cassettes in directories

Summary

Functions

Decompresses a response body if it has gzip content-encoding.

Gets the default cassette directory.

Validates that a cassette file exists.

Validates that a directory exists.

Finds all cassette files in a directory recursively.

Loads and parses entries from a cassette file.

Resolves a cassette path, handling both short names and full paths.

Writes entries back to a cassette file.

Functions

decompress_body(body, headers)

@spec decompress_body(binary(), map()) :: binary()

Decompresses a response body if it has gzip content-encoding.

Returns the decompressed body if gzipped, otherwise returns the original body. If decompression fails, returns the original body.

Examples

decompress_body(gzipped_binary, %{"content-encoding" => "gzip"})
#=> decompressed_binary

decompress_body(plain_binary, %{"content-type" => "application/json"})
#=> plain_binary

default_cassette_dir()

@spec default_cassette_dir() :: String.t()

Gets the default cassette directory.

Examples

default_cassette_dir()
#=> "test/support/cassettes"

ensure_cassette_exists!(path)

@spec ensure_cassette_exists!(String.t()) :: :ok

Validates that a cassette file exists.

Exits with error message if file doesn't exist.

Examples

ensure_cassette_exists!("test/support/cassettes/my_test.jsonl")
#=> :ok

ensure_cassette_exists!("nonexistent.jsonl")
#=> exits with error

ensure_directory_exists!(dir)

@spec ensure_directory_exists!(String.t()) :: :ok

Validates that a directory exists.

Exits with error message if directory doesn't exist.

Examples

ensure_directory_exists!("test/support/cassettes")
#=> :ok

ensure_directory_exists!("nonexistent")
#=> exits with error

find_cassettes(dir)

@spec find_cassettes(String.t()) :: [String.t()]

Finds all cassette files in a directory recursively.

Returns a list of absolute paths to .jsonl files.

Examples

find_cassettes("test/support/cassettes")
#=> [
  "test/support/cassettes/my_test.jsonl",
  "test/support/cassettes/api/users_test.jsonl"
]

load_entries(path)

@spec load_entries(String.t()) :: [map()]

Loads and parses entries from a cassette file.

Returns a list of decoded JSON entries.

Examples

load_entries("test/support/cassettes/my_test.jsonl")
#=> [%{"req" => %{...}, "resp" => %{...}}]

resolve_cassette_path(name, opts)

@spec resolve_cassette_path(
  String.t(),
  keyword()
) :: String.t()

Resolves a cassette path, handling both short names and full paths.

Examples

# Short name (relative to cassette dir)
resolve_cassette_path("my_test.jsonl", [])
#=> "test/support/cassettes/my_test.jsonl"

# Relative path from project root
resolve_cassette_path("test/support/cassettes/my_test.jsonl", [])
#=> "test/support/cassettes/my_test.jsonl"

# Absolute path
resolve_cassette_path("/full/path/to/cassette.jsonl", [])
#=> "/full/path/to/cassette.jsonl"

# Custom cassette directory
resolve_cassette_path("my_test.jsonl", dir: "test/fixtures")
#=> "test/fixtures/my_test.jsonl"

write_entries(path, entries)

@spec write_entries(String.t(), [map()]) :: :ok

Writes entries back to a cassette file.

Each entry is encoded as JSON and written on a single line.

Examples

write_entries("cassette.jsonl", [
  %{"req" => %{...}, "resp" => %{...}}
])
#=> :ok