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 ([email protected])

Copyright

Copyright © 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



187
188
189
# File 'lib/baza-rb/fake.rb', line 187

def balance
  3.14
end

#csrfString

Get CSRF token from the server for authenticated requests.

Returns:

  • (String)

    Always returns ‘fake-csrf-token’ for testing



264
265
266
# File 'lib/baza-rb/fake.rb', line 264

def csrf
  'fake-csrf-token'
end

#durable_find(jname, file) ⇒ Integer

Find a single durable.

Parameters:

  • jname (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



131
132
133
134
135
# File 'lib/baza-rb/fake.rb', line 131

def durable_find(jname, file)
  assert_name(jname)
  assert_file(file)
  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 file to upload



161
162
163
164
# File 'lib/baza-rb/fake.rb', line 161

def durable_load(id, file)
  assert_id(id)
  assert_file(file)
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



170
171
172
173
# File 'lib/baza-rb/fake.rb', line 170

def durable_lock(id, owner)
  assert_id(id)
  assert_owner(owner)
end

#durable_place(jname, file) ⇒ Integer

Place a single durable file on the server.

Parameters:

  • jname (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



142
143
144
145
146
# File 'lib/baza-rb/fake.rb', line 142

def durable_place(jname, file)
  assert_name(jname)
  assert_file(file)
  42
end

#durable_save(id, file) ⇒ Object

Save a single durable from local file to server.

Parameters:

  • id (Integer)

    The ID of the durable

  • file (String)

    The file to upload



152
153
154
155
# File 'lib/baza-rb/fake.rb', line 152

def durable_save(id, file)
  assert_id(id)
  assert_file(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



179
180
181
182
# File 'lib/baza-rb/fake.rb', line 179

def durable_unlock(id, owner)
  assert_id(id)
  assert_owner(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



253
254
255
256
257
258
259
# File 'lib/baza-rb/fake.rb', line 253

def enter(name, badge, why, job)
  assert_name(name)
  raise "The badge '#{badge}' is not valid" unless badge.match?(/^[a-zA-Z0-9_-]+$/)
  raise 'The reason cannot be empty' if why.empty?
  assert_id(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



76
77
78
79
# File 'lib/baza-rb/fake.rb', line 76

def exit_code(id)
  assert_id(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)

    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



212
213
214
215
216
217
218
219
220
# File 'lib/baza-rb/fake.rb', line 212

def fee(tab, amount, summary, job)
  raise 'The "tab" is nil' if tab.nil?
  raise "The amount #{amount} must be a Float" unless amount.is_a?(Float)
  raise "The amount #{amount} must be positive" unless amount.positive?
  raise 'The "job" is nil' if job.nil?
  raise 'The "job" must be Integer' unless job.is_a?(Integer)
  raise 'The "summary" is nil' if summary.nil?
  42
end

#finish(id, zip) ⇒ Object

Submit a ZIP archive to finish a previously popped job.

Parameters:

  • id (Integer)

    The ID of the job to finish

  • zip (String)

    The path to the ZIP file containing job results



240
241
242
243
# File 'lib/baza-rb/fake.rb', line 240

def finish(id, zip)
  assert_id(id)
  assert_file(zip)
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



58
59
60
61
# File 'lib/baza-rb/fake.rb', line 58

def finished?(id)
  assert_id(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)



94
95
96
97
# File 'lib/baza-rb/fake.rb', line 94

def lock(name, owner)
  assert_name(name)
  assert_owner(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



121
122
123
124
# File 'lib/baza-rb/fake.rb', line 121

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

#pop(owner, zip) ⇒ Boolean

Pop the next available job from the server’s queue.

rubocop:disable Naming/PredicateMethod

Parameters:

  • owner (String)

    Identifier of who is taking the job

  • zip (String)

    The local file path where the job’s ZIP will be saved

Returns:

  • (Boolean)

    Always returns TRUE and creates an empty file



228
229
230
231
232
233
# File 'lib/baza-rb/fake.rb', line 228

def pop(owner, zip)
  assert_owner(owner)
  FileUtils.mkdir_p(File.dirname(zip))
  FileUtils.touch(zip)
  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



49
50
51
52
# File 'lib/baza-rb/fake.rb', line 49

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

#push(name, data, meta) ⇒ 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

Returns:

  • (Integer)

    Always returns 42 as the fake job ID



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

def push(name, data, meta)
  assert_name(name)
  raise 'The data must be non-empty' if data.empty?
  raise '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



112
113
114
115
# File 'lib/baza-rb/fake.rb', line 112

def recent(name)
  assert_name(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



67
68
69
70
# File 'lib/baza-rb/fake.rb', line 67

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

#transfer(recipient, amount, summary) ⇒ 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

Returns:

  • (Integer)

    Always returns 42 as the fake receipt ID



197
198
199
200
201
202
203
# File 'lib/baza-rb/fake.rb', line 197

def transfer(recipient, amount, summary, *)
  raise "The recipient #{recipient.inspect} is not valid" unless recipient.match?(/^[a-zA-Z0-9-]+$/)
  raise "The amount #{amount} must be a Float" unless amount.is_a?(Float)
  raise "The amount #{amount} must be positive" unless amount.positive?
  raise "The summary #{summary.inspect} is empty" if summary.empty?
  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)



103
104
105
106
# File 'lib/baza-rb/fake.rb', line 103

def unlock(name, owner)
  assert_name(name)
  assert_owner(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



85
86
87
88
# File 'lib/baza-rb/fake.rb', line 85

def verified(id)
  assert_id(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