|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { VbaExtractor } from '../src/extraction/vba-extractor'; |
| 3 | + |
| 4 | +function extract(filePath: string, source: string) { |
| 5 | + return new VbaExtractor(filePath, source).extract(); |
| 6 | +} |
| 7 | + |
| 8 | +describe('VbaExtractor — roadmap #26 event declarations', () => { |
| 9 | + it('models Event declarations and RaiseEvent calls as first-class graph nodes and edges', () => { |
| 10 | + const src = [ |
| 11 | + 'Attribute VB_Name = "PedidoPublisher"', |
| 12 | + 'Public Event PedidoGuardado(ByVal IdPedido As Long)', |
| 13 | + '', |
| 14 | + 'Public Sub Guardar()', |
| 15 | + ' RaiseEvent PedidoGuardado(42)', |
| 16 | + 'End Sub', |
| 17 | + ].join('\n'); |
| 18 | + |
| 19 | + const r = extract('src/classes/PedidoPublisher.cls', src); |
| 20 | + const event = r.nodes.find((n) => n.kind === 'event' && n.name === 'PedidoGuardado'); |
| 21 | + const caller = r.nodes.find((n) => n.kind === 'function' && n.name === 'Guardar'); |
| 22 | + |
| 23 | + expect(event).toBeDefined(); |
| 24 | + expect(event?.qualifiedName).toBe('PedidoPublisher.PedidoGuardado'); |
| 25 | + expect(event?.visibility).toBe('public'); |
| 26 | + expect(caller).toBeDefined(); |
| 27 | + expect(r.edges).toContainEqual( |
| 28 | + expect.objectContaining({ |
| 29 | + source: caller?.id, |
| 30 | + target: event?.id, |
| 31 | + kind: 'raises-event', |
| 32 | + metadata: expect.objectContaining({ eventName: 'PedidoGuardado' }), |
| 33 | + }), |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it('models WithEvents declarations as subscriber edges with the variable name attached', () => { |
| 38 | + const r = extract( |
| 39 | + 'src/classes/FormListener.cls', |
| 40 | + 'Private WithEvents m_Form As Form_Pedido', |
| 41 | + ); |
| 42 | + |
| 43 | + const target = r.nodes.find((n) => n.name === 'Form_Pedido'); |
| 44 | + expect(target).toBeDefined(); |
| 45 | + expect(r.edges).toContainEqual( |
| 46 | + expect.objectContaining({ |
| 47 | + target: target?.id, |
| 48 | + kind: 'subscribes-event', |
| 49 | + metadata: expect.objectContaining({ |
| 50 | + synthesizedBy: 'vba-withevents', |
| 51 | + variableName: 'm_Form', |
| 52 | + }), |
| 53 | + }), |
| 54 | + ); |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +describe('VbaExtractor — roadmap #26 Type declarations', () => { |
| 59 | + it('models Type declarations with type_member nodes and type-member edges', () => { |
| 60 | + const src = [ |
| 61 | + 'Attribute VB_Name = "Tipos"', |
| 62 | + 'Private Type TPedido', |
| 63 | + ' Id As Long', |
| 64 | + ' Nombre As String', |
| 65 | + 'End Type', |
| 66 | + ].join('\n'); |
| 67 | + |
| 68 | + const r = extract('src/modules/Tipos.bas', src); |
| 69 | + const type = r.nodes.find((n) => n.kind === 'type' && n.name === 'TPedido'); |
| 70 | + const members = r.nodes.filter((n) => n.kind === 'type_member'); |
| 71 | + |
| 72 | + expect(type).toBeDefined(); |
| 73 | + expect(type?.qualifiedName).toBe('TPedido'); |
| 74 | + expect(type?.visibility).toBe('private'); |
| 75 | + expect(members.map((n) => n.name).sort()).toEqual(['Id', 'Nombre']); |
| 76 | + expect(members.find((n) => n.name === 'Id')?.metadata?.memberType).toBe('Long'); |
| 77 | + expect(members.find((n) => n.name === 'Nombre')?.metadata?.memberType).toBe('String'); |
| 78 | + for (const member of members) { |
| 79 | + expect(r.edges).toContainEqual( |
| 80 | + expect.objectContaining({ |
| 81 | + source: type?.id, |
| 82 | + target: member.id, |
| 83 | + kind: 'type-member', |
| 84 | + }), |
| 85 | + ); |
| 86 | + } |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe('VbaExtractor — roadmap #26 Declare statements', () => { |
| 91 | + it('models Win32 Declare statements as declare nodes and keeps calls traceable', () => { |
| 92 | + const src = [ |
| 93 | + 'Attribute VB_Name = "WinApi"', |
| 94 | + 'Public Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long', |
| 95 | + '', |
| 96 | + 'Public Sub UseApi()', |
| 97 | + ' GetTickCount', |
| 98 | + 'End Sub', |
| 99 | + ].join('\n'); |
| 100 | + |
| 101 | + const r = extract('src/modules/WinApi.bas', src); |
| 102 | + const declaration = r.nodes.find((n) => n.kind === 'declare' && n.name === 'GetTickCount'); |
| 103 | + const caller = r.nodes.find((n) => n.kind === 'function' && n.name === 'UseApi'); |
| 104 | + |
| 105 | + expect(declaration).toBeDefined(); |
| 106 | + expect(declaration?.metadata).toEqual( |
| 107 | + expect.objectContaining({ |
| 108 | + dll: 'kernel32', |
| 109 | + declareKind: 'function', |
| 110 | + ptrSafe: true, |
| 111 | + }), |
| 112 | + ); |
| 113 | + expect(r.edges).toContainEqual( |
| 114 | + expect.objectContaining({ |
| 115 | + source: caller?.id, |
| 116 | + target: declaration?.id, |
| 117 | + kind: 'calls', |
| 118 | + }), |
| 119 | + ); |
| 120 | + }); |
| 121 | +}); |
0 commit comments