|
2 | 2 | import io |
3 | 3 | import json |
4 | 4 | import unittest |
| 5 | +from types import SimpleNamespace |
| 6 | +from unittest import mock |
5 | 7 | from nullius import providers |
6 | 8 |
|
7 | 9 |
|
8 | 10 | class Models(unittest.TestCase): |
| 11 | + def test_openrouter_embedding_catalogue_is_opt_in_sorted_and_deduplicated(self): |
| 12 | + seen = [] |
| 13 | + def fetch(req, timeout): |
| 14 | + seen.append((req.full_url, req.get_header('Authorization'))) |
| 15 | + rows = [{'id': 'chat', 'name': 'Z chat'}] |
| 16 | + if req.full_url.endswith('/embeddings/models'): |
| 17 | + rows = [{'id': 'embed', 'name': 'A embedding'}, {'id': 'chat', 'name': 'Z chat'}] |
| 18 | + return io.BytesIO(json.dumps({'data': rows}).encode()) |
| 19 | + result = providers.list_models('openrouter', {'OPENROUTER_API_KEY': 'synthetic-key'}, |
| 20 | + opener=fetch, include_embeddings=True) |
| 21 | + self.assertEqual([r['id'] for r in result], ['embed', 'chat']) |
| 22 | + self.assertEqual(seen, [('https://openrouter.ai/api/v1/models', 'Bearer synthetic-key'), |
| 23 | + ('https://openrouter.ai/api/v1/embeddings/models', 'Bearer synthetic-key')]) |
| 24 | + seen.clear() |
| 25 | + providers.list_models('openai', {'OPENAI_API_KEY': 'synthetic-key'}, opener=fetch, include_embeddings=True) |
| 26 | + self.assertEqual(seen, [('https://api.openai.com/v1/models', 'Bearer synthetic-key')]) |
| 27 | + |
| 28 | + def test_failed_embedding_catalogue_does_not_return_a_partial_list(self): |
| 29 | + for failure in (OSError('private details'), b'not JSON', b'{"data": {}}'): |
| 30 | + def fetch(req, timeout): |
| 31 | + if req.full_url.endswith('/embeddings/models'): |
| 32 | + if isinstance(failure, Exception): |
| 33 | + raise failure |
| 34 | + return io.BytesIO(failure) |
| 35 | + return io.BytesIO(b'{"data": [{"id": "chat"}]}') |
| 36 | + with self.subTest(failure=type(failure)), self.assertRaisesRegex( |
| 37 | + ValueError, '^Model list unavailable. Enter a model ID manually.$'): |
| 38 | + providers.list_models('openrouter', {}, opener=fetch, include_embeddings=True) |
| 39 | + |
| 40 | + def test_run_preflight_accepts_embeddings_but_still_refuses_unknown_models(self): |
| 41 | + from nullius.standard.runner.run import Run, Refused |
| 42 | + r = SimpleNamespace(base='https://openrouter.ai/api/v1', provider='openrouter', provider_env={}, |
| 43 | + embed_model='qwen/embedding', models_called=lambda: ['chat', 'qwen/embedding']) |
| 44 | + def get(url, headers, opener=None): |
| 45 | + model = 'qwen/embedding' if url.endswith('/embeddings/models') else 'chat' |
| 46 | + return json.dumps({'data': [{'id': model}]}).encode() |
| 47 | + with mock.patch.object(providers, '_get', side_effect=get): |
| 48 | + Run.known_models(r) |
| 49 | + r.models_called = lambda: ['chat', 'qwen/typo'] |
| 50 | + with self.assertRaisesRegex(Refused, 'lists no model qwen/typo'): |
| 51 | + Run.known_models(r) |
| 52 | + |
9 | 53 | def test_endpoint_key_and_exact_ids(self): |
10 | 54 | for provider, base, key_name in [('openrouter', 'https://openrouter.ai/api/v1', 'OPENROUTER_API_KEY'), |
11 | 55 | ('openai', 'https://api.openai.com/v1', 'OPENAI_API_KEY'), |
|
0 commit comments