import { describe, it, expect } from 'vitest' import { parseRequest } from '../../src/parser/request.js' describe('parseRequest', () => { it('parses a minimal GET request', () => { const yaml = ` info: name: Get Users type: http seq: 1 http: method: GET url: https://api.example.com/users ` const result = parseRequest(yaml) expect(result.info.name).toBe('Get Users') expect(result.info.type).toBe('http') expect(result.info.seq).toBe(1) expect(result.http.method).toBe('GET') expect(result.http.url).toBe('https://api.example.com/users') }) it('parses query params', () => { const yaml = ` info: name: Test type: http http: method: GET url: https://api.example.com params: - name: page value: "1" type: query - name: debug value: "true" type: query disabled: true ` const result = parseRequest(yaml) expect(result.http.params).toHaveLength(2) expect(result.http.params![0]).toEqual({ name: 'page', value: '1', type: 'query', disabled: false }) expect(result.http.params![1].disabled).toBe(true) }) it('parses POST with JSON body and basic auth', () => { const yaml = ` info: name: Create User type: http http: method: POST url: https://api.example.com/users body: type: json data: | {"name": "John"} auth: type: basic basic: username: admin password: secret ` const result = parseRequest(yaml) expect(result.http.method).toBe('POST') expect(result.http.body?.type).toBe('json') expect(result.http.auth?.type).toBe('basic') expect(result.http.auth?.basic?.username).toBe('admin') }) it('parses runtime scripts', () => { const yaml = ` info: name: Scripted type: http http: method: GET url: https://example.com runtime: scripts: - type: before-request code: "console.log('before')" - type: after-response code: "console.log('after')" - type: tests code: "test('ok', () => {})" ` const result = parseRequest(yaml) expect(result.runtime?.scripts).toHaveLength(3) expect(result.runtime?.scripts![0].type).toBe('before-request') expect(result.runtime?.scripts![1].type).toBe('after-response') expect(result.runtime?.scripts![2].type).toBe('tests') }) it('throws on missing info.name', () => { const yaml = ` info: type: http http: method: GET url: https://example.com ` expect(() => parseRequest(yaml)).toThrow("Missing required field 'info.name'") }) it('throws on missing http.method', () => { const yaml = ` info: name: Test type: http http: url: https://example.com ` expect(() => parseRequest(yaml)).toThrow("Missing required field 'http.method'") }) it('throws on missing http.url', () => { const yaml = ` info: name: Test type: http http: method: GET ` expect(() => parseRequest(yaml)).toThrow("Missing required field 'http.url'") }) })