|
| 1 | +import { describe, expect, test } from 'vitest'; |
| 2 | +import { lintFromString } from '../lint.js'; |
| 3 | +import { createConfig } from '../config/index.js'; |
| 4 | +import { detectSpec } from '../detect-spec.js'; |
| 5 | + |
| 6 | +describe('Open-RPC support', () => { |
| 7 | + const openRpcDocument = JSON.stringify({ |
| 8 | + openrpc: '1.2.6', |
| 9 | + info: { |
| 10 | + title: 'Petstore', |
| 11 | + version: '1.0.0', |
| 12 | + }, |
| 13 | + methods: [ |
| 14 | + { |
| 15 | + name: 'listPets', |
| 16 | + params: [], |
| 17 | + result: { |
| 18 | + name: 'pets', |
| 19 | + schema: { |
| 20 | + type: 'array', |
| 21 | + items: { $ref: '#/components/schemas/Pet' }, |
| 22 | + }, |
| 23 | + }, |
| 24 | + }, |
| 25 | + ], |
| 26 | + components: { |
| 27 | + schemas: { |
| 28 | + Pet: { |
| 29 | + type: 'object', |
| 30 | + properties: { |
| 31 | + id: { type: 'integer' }, |
| 32 | + name: { type: 'string' }, |
| 33 | + }, |
| 34 | + required: ['id', 'name'], |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }); |
| 39 | + |
| 40 | + test('detects Open-RPC 1.x', () => { |
| 41 | + const parsed = JSON.parse(openRpcDocument); |
| 42 | + expect(detectSpec(parsed)).toBe('openrpc1'); |
| 43 | + }); |
| 44 | + |
| 45 | + test('lints valid Open-RPC document', async () => { |
| 46 | + const config = await createConfig({ |
| 47 | + extends: ['recommended'], |
| 48 | + rules: { 'info-license': 'off' }, |
| 49 | + }); |
| 50 | + const result = await lintFromString({ |
| 51 | + source: openRpcDocument, |
| 52 | + config, |
| 53 | + }); |
| 54 | + |
| 55 | + expect(result).toEqual([]); |
| 56 | + }); |
| 57 | + |
| 58 | + test('lints invalid Open-RPC document (structural error)', async () => { |
| 59 | + const invalidDocument = JSON.stringify({ |
| 60 | + openrpc: '1.2.6', |
| 61 | + info: { |
| 62 | + // missing version |
| 63 | + title: 'Petstore', |
| 64 | + }, |
| 65 | + methods: [], |
| 66 | + }); |
| 67 | + |
| 68 | + const config = await createConfig({ |
| 69 | + extends: ['recommended'], |
| 70 | + rules: { 'info-license': 'off' }, |
| 71 | + }); |
| 72 | + const result = await lintFromString({ |
| 73 | + source: invalidDocument, |
| 74 | + config, |
| 75 | + }); |
| 76 | + |
| 77 | + expect(result).toHaveLength(1); |
| 78 | + expect(result[0].ruleId).toBe('struct'); |
| 79 | + expect(result[0].message).toContain('The field `version` must be present on this level'); |
| 80 | + }); |
| 81 | + |
| 82 | + test('lints invalid Open-RPC document (missing required field)', async () => { |
| 83 | + const invalidDocument = JSON.stringify({ |
| 84 | + openrpc: '1.2.6', |
| 85 | + // missing info |
| 86 | + methods: [], |
| 87 | + }); |
| 88 | + |
| 89 | + const config = await createConfig({ |
| 90 | + extends: ['recommended'], |
| 91 | + rules: { 'info-license': 'off' }, |
| 92 | + }); |
| 93 | + const result = await lintFromString({ |
| 94 | + source: invalidDocument, |
| 95 | + config, |
| 96 | + }); |
| 97 | + |
| 98 | + expect(result.length).toBeGreaterThan(0); |
| 99 | + expect(result[0].ruleId).toBe('struct'); |
| 100 | + expect(result[0].message).toContain('The field `info` must be present on this level'); |
| 101 | + }); |
| 102 | + |
| 103 | + test('reports unused components', async () => { |
| 104 | + const documentWithUnused = JSON.stringify({ |
| 105 | + openrpc: '1.2.6', |
| 106 | + info: { |
| 107 | + title: 'Petstore', |
| 108 | + version: '1.0.0', |
| 109 | + }, |
| 110 | + methods: [ |
| 111 | + { |
| 112 | + name: 'listPets', |
| 113 | + params: [], |
| 114 | + result: { |
| 115 | + name: 'pets', |
| 116 | + schema: { |
| 117 | + type: 'array', |
| 118 | + items: { $ref: '#/components/schemas/Pet' }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + ], |
| 123 | + components: { |
| 124 | + schemas: { |
| 125 | + Pet: { |
| 126 | + type: 'object', |
| 127 | + properties: { |
| 128 | + id: { type: 'integer' }, |
| 129 | + name: { type: 'string' }, |
| 130 | + }, |
| 131 | + required: ['id', 'name'], |
| 132 | + }, |
| 133 | + UnusedPet: { |
| 134 | + type: 'object', |
| 135 | + properties: { |
| 136 | + id: { type: 'integer' }, |
| 137 | + name: { type: 'string' }, |
| 138 | + }, |
| 139 | + required: ['id', 'name'], |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }); |
| 144 | + |
| 145 | + const config = await createConfig({ |
| 146 | + rules: { 'no-unused-components': 'error' }, |
| 147 | + }); |
| 148 | + const result = await lintFromString({ |
| 149 | + source: documentWithUnused, |
| 150 | + config, |
| 151 | + }); |
| 152 | + |
| 153 | + expect(result).toHaveLength(1); |
| 154 | + expect(result[0].ruleId).toBe('no-unused-components'); |
| 155 | + expect(result[0].message).toContain('Component: "UnusedPet" is never used.'); |
| 156 | + }); |
| 157 | +}); |
0 commit comments