from k1link.notifications import NotificationHistory def test_manual_rollback_message_names_failed_and_restored_channels(tmp_path): history = NotificationHistory(tmp_path) for i, (source, target, name) in enumerate([ ('ethernet', 'wifi', 'Wi-Fi'), ('wifi', 'ethernet', 'Ethernet')]): event = dict(id=f'{i:032x}', kind='manual_rollback', source=source, target=target, at=1000) history.uplink({'node_id': str(i), 'name': 'Rover'}, [event]) message = history.listing()['items'][0]['description'] assert f'Восстановлено подключение по {name}.' in message def test_history_read_dismiss_separation_and_repeated_node_event(tmp_path): history=NotificationHistory(tmp_path) row={'node_id':'board','name':'Test rover'} event=dict(id='a'*32,kind='fallback',source='ethernet',target='wifi',at=1000) history.uplink(row,[event]) history.uplink(row,[event]) page=history.listing() assert len(page['items'])==1 and page['unread']==1 assert 'Wi-Fi' in page['items'][0]['description'] history.read(page['latest']) assert history.listing()['unread']==0 # Restart replays the bounded board receipt without another notification. history=NotificationHistory(tmp_path) history.uplink(row,[event]) assert len(history.listing()['items'])==1 def test_ui_retries_and_pagination_are_idempotent(tmp_path): history=NotificationHistory(tmp_path) for i in range(105): history.add('ui:'+str(i),'error','Error',str(i)) history.add('ui:104','error','Error','104') newest=history.listing() assert len(newest['items'])==100 and newest['latest']==105 older=history.listing(before=newest['items'][-1]['sequence']) assert len(older['items'])==5 assert len(history.listing(after=103)['items'])==2 def test_history_api_requires_local_origin_and_bounds_cursors(tmp_path): from types import SimpleNamespace from fastapi import FastAPI from fastapi.testclient import TestClient from k1link.web.notifications_api import router app = FastAPI() app.include_router(router) app.state.fleet = SimpleNamespace(notifications=NotificationHistory(tmp_path)) with TestClient(app, base_url='http://127.0.0.1', client=('127.0.0.1', 12345)) as client: path = '/api/v1/notifications' message = dict(identity='ui:'+'a'*36, tone='success', title='Saved') rejected = client.post(path, json=message, headers={'Origin':'https://other.invalid'}) assert rejected.status_code == 403 assert client.get(path+'?after=999999999999999999999').status_code == 422 assert client.post(path, json={**message, 'tone':'loading'}).status_code == 422 assert client.post(path, json=message).status_code == 200 page = client.get(path) assert page.headers['cache-control'] == 'no-store' assert len(page.json()['items']) == 1 assert client.post(path+'/read', json={'through':page.json()['latest']}).status_code == 200 assert client.get(path).json()['unread'] == 0