Module: Browserctl::Recording::LogWriter

Defined in:
lib/browserctl/recording/log_writer.rb

Overview

Owns recording-log file I/O: path resolution, header initialisation, JSONL append, raw read, deletion, and format-version validation.

All paths are resolved against the parent ‘Recording::RECORDINGS_DIR` constant on each call so RSpec `stub_const` calls remain effective.

Class Method Summary collapse

Class Method Details

.append_entry(name, entry) ⇒ Object

Appends a single JSONL entry to the log for ‘name`.



45
46
47
48
49
# File 'lib/browserctl/recording/log_writer.rb', line 45

def append_entry(name, entry)
  File.open(log_path(name), "a") do |f|
    f.puts JSON.generate(entry)
  end
end

.delete_log(name) ⇒ Object

Removes the log file for ‘name` if present.



57
58
59
# File 'lib/browserctl/recording/log_writer.rb', line 57

def delete_log(name)
  FileUtils.rm_f(log_path(name))
end

.init_log(name) ⇒ Object

Truncates (or creates) the log for ‘name`, locks it down to user permissions, and writes the `_meta` header line. Returns the path.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/browserctl/recording/log_writer.rb', line 26

def init_log(name)
  FileUtils.mkdir_p(Browserctl::Recording::RECORDINGS_DIR, mode: 0o700)
  path = log_path(name)
  FileUtils.rm_f(path)
  FileUtils.touch(path)
  File.chmod(0o600, path)
  File.open(path, "a") do |f|
    f.puts JSON.generate(
      cmd: "_meta",
      format_version: Browserctl::Recording::RECORDING_FORMAT_VERSION,
      log_format: Browserctl::Recording::LOG_FORMAT,
      recording: name,
      started_at: Time.now.utc.iso8601
    )
  end
  path
end

.log_path(name) ⇒ Object

Returns the on-disk JSONL path for a named recording.



20
21
22
# File 'lib/browserctl/recording/log_writer.rb', line 20

def log_path(name)
  File.join(Browserctl::Recording::RECORDINGS_DIR, "#{name}.jsonl")
end

.read_entries(name) ⇒ Object

Returns the parsed lines for ‘name`, with symbolised keys.



52
53
54
# File 'lib/browserctl/recording/log_writer.rb', line 52

def read_entries(name)
  File.readlines(log_path(name)).map { |l| JSON.parse(l, symbolize_names: true) }
end

.verify_format_version!(raw_lines, path: nil) ⇒ Object

Raises Browserctl::ProtocolMismatch when the recording log’s ‘_meta` header is missing or declares a `format_version` that this build does not support. Mirrors `Browserctl::State::Bundle`.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/browserctl/recording/log_writer.rb', line 64

def verify_format_version!(raw_lines, path: nil)
  meta = raw_lines.first
  version = meta && meta[:cmd] == "_meta" ? meta[:format_version] : nil
  supported = Browserctl::Recording::SUPPORTED_FORMAT_VERSIONS
  return if version && supported.include?(version)

  where = path ? " at #{path}" : ""
  msg = if version.nil?
          "recording log#{where} is missing format_version " \
            "(supported: #{supported.inspect})"
        else
          "recording log#{where} declares format_version=#{version.inspect}, " \
            "this build supports #{supported.inspect}"
        end
  raise Browserctl::ProtocolMismatch.new(msg, code: Browserctl::Error::Codes::PROTOCOL_MISMATCH)
end