Ship Operations console polish, PocketBase social fixtures, and portal join-leads without local DBs or probe scripts. Co-authored-by: Cursor <cursoragent@cursor.com>
83 lines
3.1 KiB
Python
83 lines
3.1 KiB
Python
"""Isolated PocketBase adapter fixture; never connects to an external service."""
|
|
import copy
|
|
import json
|
|
import re
|
|
import uuid
|
|
from unittest.mock import patch
|
|
|
|
|
|
class MemoryPocketBase:
|
|
base_url = 'http://isolated.invalid'
|
|
|
|
def __init__(self):
|
|
self.rows = {}
|
|
|
|
def _matches(self, row, expression):
|
|
if not expression:
|
|
return True
|
|
if ' || ' in expression:
|
|
return any(self._matches(row, part) for part in expression.split(' || '))
|
|
if ' && ' in expression:
|
|
return all(self._matches(row, part) for part in expression.split(' && '))
|
|
match = re.fullmatch(r'([\w.]+)\s*(=|>|<)\s*(.+)', expression)
|
|
if not match:
|
|
raise AssertionError(f'Unsupported test filter: {expression}')
|
|
key, operator, raw = match.groups()
|
|
expected = json.loads(raw)
|
|
actual = row
|
|
for segment in key.split('.'):
|
|
actual = actual.get(segment) if isinstance(actual, dict) else None
|
|
if operator == '=':
|
|
return actual == expected
|
|
return actual is not None and (actual > expected if operator == '>' else actual < expected)
|
|
|
|
def list_all(self, collection, *, filter=None, sort=None, hard_limit=5000):
|
|
rows = [copy.deepcopy(row) for row in self.rows.get(collection, {}).values() if self._matches(row, filter)]
|
|
if sort:
|
|
for key in reversed(sort.split(',')):
|
|
rows.sort(key=lambda row: row.get(key.lstrip('-')) or 0, reverse=key.startswith('-'))
|
|
return rows[:hard_limit]
|
|
|
|
def first(self, collection, *, filter):
|
|
rows = self.list_all(collection, filter=filter, hard_limit=1)
|
|
return rows[0] if rows else None
|
|
|
|
def create(self, collection, payload):
|
|
row = copy.deepcopy(payload)
|
|
row.setdefault('id', uuid.uuid4().hex[:15])
|
|
table = self.rows.setdefault(collection, {})
|
|
if row['id'] in table:
|
|
raise ValueError('duplicate id')
|
|
table[row['id']] = row
|
|
return copy.deepcopy(row)
|
|
|
|
def update(self, collection, record_id, payload):
|
|
row = self.rows[collection][record_id]
|
|
row.update(copy.deepcopy(payload))
|
|
return copy.deepcopy(row)
|
|
|
|
def upsert(self, collection, *, filter, payload):
|
|
current = self.first(collection, filter=filter)
|
|
return self.update(collection, current['id'], payload) if current else self.create(collection, payload)
|
|
|
|
def request(self, method, path, **kwargs):
|
|
assert method == 'GET', method
|
|
parts = path.split('/')
|
|
return copy.deepcopy(self.rows[parts[3]][parts[5]])
|
|
|
|
|
|
def isolate_social(test):
|
|
from social_svc import pb_data, im
|
|
adapter = MemoryPocketBase()
|
|
for target, value in [
|
|
('social_svc.pb_data.get_pb', lambda: adapter),
|
|
('social_svc.pb_data.ensure_once', lambda: None),
|
|
('nomadro_pb.ensure_once', lambda: None),
|
|
('social_svc.im.check_rate', lambda guest_id: None),
|
|
('social_svc.trial_gate.assert_chat_trial_ok', lambda guest_id: None),
|
|
]:
|
|
override = patch(target, value)
|
|
override.start()
|
|
test.addCleanup(override.stop)
|
|
return adapter
|