Class: BazaRb::Fake

Inherits:
Object
  • Object
show all
Defined in:
lib/baza-rb/fake.rb

Overview

Fake implementation of the Zerocracy API client for testing.

This class implements the same public interface as BazaRb but doesn't make any network connections. Instead, it returns predefined fake values and validates inputs to help catch errors during testing.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright (c) 2024-2026 Yegor Bugayenko

License

MIT

Examples:

Using in tests

baza = BazaRb::Fake.new
assert_equal 'torvalds', baza.whoami
assert_equal 42, baza.push('test-job', 'data', [])

Instance Method Summary collapse

Instance Method Details

#balanceFloat

Get current balance of the authenticated user.

Returns:

  • (Float)

    Always returns 3.14 zents for testing



191
192
193
# File 'lib/baza-rb/fake.rb', line 191

def balance
  3.14
end

#csrfString

Get CSRF token from the server for authenticated requests.

Returns:

  • (String)

    Always returns 'fake-csrf-token' for testing



258
259
260
# File 'lib/baza-rb/fake.rb', line 258

def csrf
  'fake-csrf-token'
end

#durable_find(pname, file) ⇒ Integer

Find a single durable.

Parameters:

  • pname (String)

    The name of the job on the server

  • file (String)

    The file name

Returns:

  • (Integer)

    Always returns 42 as the fake durable ID

Raises:

  • (RuntimeError)


133
134
135
136
137
138
# File 'lib/baza-rb/fake.rb', line 133

def durable_find(pname, file)
  checkname(pname)
  raise(RuntimeError, 'The "file" is nil') if file.nil?
  raise(RuntimeError, 'The "file" may not be empty') if file.empty?
  42
end

#durable_load(id, file) ⇒ Object

Load a single durable from server to local file.

Parameters:

  • id (Integer)

    The ID of the durable

  • file (String)

    The local file path to save the downloaded durable

Raises:

  • (RuntimeError)


165
166
167
168
# File 'lib/baza-rb/fake.rb', line 165

def durable_load(id, file)
  checkid(id)
  raise(RuntimeError, 'The "file" of the durable is nil') if file.nil?
end

#durable_lock(id, owner) ⇒ Object

Lock a single durable.

Parameters:

  • id (Integer)

    The ID of the durable

  • owner (String)

    The owner of the lock



174
175
176
177
# File 'lib/baza-rb/fake.rb', line 174

def durable_lock(id, owner)
  checkid(id)
  checkowner(owner)
end

#durable_place(pname, file) ⇒ Integer

Place a single durable file on the server.

Parameters:

  • pname (String)

    The name of the job on the server

  • file (String)

    The path to the file to upload

Returns:

  • (Integer)

    Always returns 42 as the fake durable ID



145
146
147
148
149
# File 'lib/baza-rb/fake.rb', line 145

def durable_place(pname, file)
  checkname(pname)
  checkfile(file)
  42
end

#durable_save(id, file, chunk_size: BazaRb::DEFAULT_CHUNK_SIZE) ⇒ Object

Save a single durable from local file to server.

Parameters:

  • id (Integer)

    The ID of the durable

  • file (String)

    The file to upload

  • chunk_size (Integer) (defaults to: BazaRb::DEFAULT_CHUNK_SIZE)

    Size of each chunk in bytes (accepted for signature parity with BazaRb#durable_save)



156
157
158
159
# File 'lib/baza-rb/fake.rb', line 156

def durable_save(id, file, chunk_size: BazaRb::DEFAULT_CHUNK_SIZE) # rubocop:disable Lint/UnusedMethodArgument
  checkid(id)
  checkfile(file)
end

#durable_unlock(id, owner) ⇒ Object

Unlock a single durable.

Parameters:

  • id (Integer)

    The ID of the durable

  • owner (String)

    The owner of the lock



183
184
185
186
# File 'lib/baza-rb/fake.rb', line 183

def durable_unlock(id, owner)
  checkid(id)
  checkowner(owner)
end

#enter(name, badge, why, job) { ... } ⇒ String

Enter a valve to cache or retrieve a computation result.

Parameters:

  • name (String)

    Name of the job

  • badge (String)

    Unique identifier for this valve

  • why (String)

    The reason/description for entering this valve

  • job (nil|Integer)

    Optional job ID to associate with this valve

Yields:

  • Block that computes the result

Returns:

  • (String)

    Always executes and returns the block's result

Raises:

  • (RuntimeError)


247
248
249
250
251
252
253
# File 'lib/baza-rb/fake.rb', line 247

def enter(name, badge, why, job)
  checkname(name)
  raise(RuntimeError, "The badge '#{badge}' is not valid") unless badge.match?(/\A[a-zA-Z0-9_-]+\z/)
  raise(RuntimeError, 'The reason cannot be empty') if why.empty?
  checkid(job) unless job.nil?
  yield
end

#exit_code(id) ⇒ Integer

Read and return the exit code of the job.

Parameters:

  • id (Integer)

    The ID of the job on the server

Returns:

  • (Integer)

    The exit code



78
79
80
81
# File 'lib/baza-rb/fake.rb', line 78

def exit_code(id)
  checkid(id)
  0
end

#fee(tab, amount, summary, job) ⇒ Integer

Pay a fee associated with a job.

Parameters:

  • tab (String)

    The category/type of the fee

  • amount (Float, BigDecimal)

    The fee amount in ƶ (zents)

  • summary (String)

    The description/reason for the fee

  • job (Integer)

    The ID of the job this fee is for

Returns:

  • (Integer)

    Always returns 42 as the fake receipt ID

Raises:

  • (RuntimeError)


225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/baza-rb/fake.rb', line 225

def fee(tab, amount, summary, job)
  raise(RuntimeError, 'The "tab" is nil') if tab.nil?
  raise(RuntimeError, 'The "amount" is nil') if amount.nil?
  unless amount.is_a?(Float) || amount.is_a?(BigDecimal)
    raise(RuntimeError, 'The "amount" must be Float or BigDecimal')
  end
  raise(RuntimeError, 'The "amount" must be positive') unless amount.positive?
  raise(RuntimeError, 'The "job" is nil') if job.nil?
  raise(RuntimeError, 'The "job" must be Integer') unless job.is_a?(Integer)
  raise(RuntimeError, 'The "job" must be positive') unless job.positive?
  raise(RuntimeError, 'The "summary" is nil') if summary.nil?
  42
end

#finished?(id) ⇒ Boolean

Check if the job with this ID is finished already.

Parameters:

  • id (Integer)

    The ID of the job on the server

Returns:

  • (Boolean)

    Always returns TRUE for testing



60
61
62
63
# File 'lib/baza-rb/fake.rb', line 60

def finished?(id)
  checkid(id)
  true
end

#lock(name, owner) ⇒ Object

Lock the name.

Parameters:

  • name (String)

    The name of the job on the server

  • owner (String)

    The owner of the lock (any string)



96
97
98
99
# File 'lib/baza-rb/fake.rb', line 96

def lock(name, owner)
  checkname(name)
  checkowner(owner)
end

#name_exists?(name) ⇒ Boolean

Check whether the name of the job exists on the server.

Parameters:

  • name (String)

    The name of the job on the server

Returns:

  • (Boolean)

    TRUE if such name exists



123
124
125
126
# File 'lib/baza-rb/fake.rb', line 123

def name_exists?(name)
  checkname(name)
  true
end

#pull(id) ⇒ String

Pull factbase from the server.

Parameters:

  • id (Integer)

    The ID of the job on the server

Returns:

  • (String)

    Returns an empty factbase export for testing



51
52
53
54
# File 'lib/baza-rb/fake.rb', line 51

def pull(id)
  checkid(id)
  Factbase.new.export
end

#push(name, data, meta, chunk_size: BazaRb::DEFAULT_CHUNK_SIZE) ⇒ Integer

Push factbase to the server.

Parameters:

  • name (String)

    The unique name of the job on the server

  • data (String)

    The binary data to push to the server

  • meta (Array<String>)

    List of metadata strings to attach to the job

  • chunk_size (Integer) (defaults to: BazaRb::DEFAULT_CHUNK_SIZE)

    Size of each chunk in bytes (accepted for signature parity with BazaRb#push)

Returns:

  • (Integer)

    Always returns 42 as the fake job ID

Raises:

  • (RuntimeError)


39
40
41
42
43
44
45
# File 'lib/baza-rb/fake.rb', line 39

def push(name, data, meta, chunk_size: BazaRb::DEFAULT_CHUNK_SIZE) # rubocop:disable Lint/UnusedMethodArgument
  checkname(name)
  raise(RuntimeError, 'The "data" of the job is nil') if data.nil?
  raise(RuntimeError, 'The data must be non-empty') if data.empty?
  raise(RuntimeError, 'The meta must be an array') unless meta.is_a?(Array)
  42
end

#recent(name) ⇒ Integer

Get the ID of the job by the name.

Parameters:

  • name (String)

    The name of the job on the server

Returns:

  • (Integer)

    The ID of the job on the server



114
115
116
117
# File 'lib/baza-rb/fake.rb', line 114

def recent(name)
  checkname(name)
  42
end

#stdout(id) ⇒ String

Read and return the stdout of the job.

Parameters:

  • id (Integer)

    The ID of the job on the server

Returns:

  • (String)

    The stdout, as a text



69
70
71
72
# File 'lib/baza-rb/fake.rb', line 69

def stdout(id)
  checkid(id)
  'Fake stdout output'
end

#transfer(recipient, amount, summary, job: nil, badge: nil) ⇒ Integer

Transfer funds to another user.

Parameters:

  • recipient (String)

    GitHub username of the recipient

  • amount (Float)

    The amount to transfer in ƶ (zents)

  • summary (String)

    The description/reason for the payment

  • job (Integer) (defaults to: nil)

    Optional job ID to associate with this transfer

  • badge (String) (defaults to: nil)

    Optional idempotency key for deduping the payment

Returns:

  • (Integer)

    Always returns 42 as the fake receipt ID

Raises:

  • (RuntimeError)


203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/baza-rb/fake.rb', line 203

def transfer(recipient, amount, summary, job: nil, badge: nil)
  raise(RuntimeError, 'The "recipient" is nil') if recipient.nil?
  raise(RuntimeError, "The recipient #{recipient.inspect} is not valid") unless recipient.match?(/\A[a-zA-Z0-9-]+\z/)
  raise(RuntimeError, 'The "amount" is nil') if amount.nil?
  unless amount.is_a?(Float) || amount.is_a?(BigDecimal)
    raise(RuntimeError, 'The "amount" must be Float or BigDecimal')
  end
  raise(RuntimeError, 'The "amount" must be positive') unless amount.positive?
  raise(RuntimeError, 'The "summary" is nil') if summary.nil?
  raise(RuntimeError, "The summary #{summary.inspect} is empty") if summary.empty?
  checkid(job) unless job.nil?
  raise(RuntimeError, 'The "badge" must be a String') if !badge.nil? && !badge.is_a?(String)
  42
end

#unlock(name, owner) ⇒ Object

Unlock the name.

Parameters:

  • name (String)

    The name of the job on the server

  • owner (String)

    The owner of the lock (any string)



105
106
107
108
# File 'lib/baza-rb/fake.rb', line 105

def unlock(name, owner)
  checkname(name)
  checkowner(owner)
end

#verified(id) ⇒ String

Read and return the verification verdict of the job.

Parameters:

  • id (Integer)

    The ID of the job on the server

Returns:

  • (String)

    The verdict



87
88
89
90
# File 'lib/baza-rb/fake.rb', line 87

def verified(id)
  checkid(id)
  'fake-verdict'
end

#whoamiString

Get GitHub login name of the logged in user.

Returns:

  • (String)

    Always returns 'torvalds' for testing



28
29
30
# File 'lib/baza-rb/fake.rb', line 28

def whoami
  'torvalds'
end