|
| 1 | +import fs from 'node:fs'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 5 | + |
| 6 | +const { addSourceMock, quicktypeMock } = vi.hoisted(() => ({ |
| 7 | + addSourceMock: vi.fn(async (_source: unknown) => undefined), |
| 8 | + quicktypeMock: vi.fn(async ({ lang }: { lang: 'swift' | 'kotlin' }) => ({ |
| 9 | + lines: |
| 10 | + lang === 'swift' |
| 11 | + ? ['public struct UserProfile {}', 'public struct SessionResult {}'] |
| 12 | + : ['data class UserProfile()', 'data class SessionResult()'], |
| 13 | + })), |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock('quicktype-core', () => ({ |
| 17 | + FetchingJSONSchemaStore: class {}, |
| 18 | + InputData: class { |
| 19 | + addInput(): void {} |
| 20 | + }, |
| 21 | + JSONSchemaInput: class { |
| 22 | + async addSource(source: unknown): Promise<void> { |
| 23 | + await addSourceMock(source); |
| 24 | + } |
| 25 | + }, |
| 26 | + quicktype: quicktypeMock, |
| 27 | +})); |
| 28 | + |
| 29 | +vi.mock('quicktype-typescript-input', () => ({ |
| 30 | + schemaForTypeScriptSources: () => ({ |
| 31 | + schema: JSON.stringify({ |
| 32 | + definitions: { |
| 33 | + UserProfile: { type: 'object' }, |
| 34 | + SessionResult: { type: 'object' }, |
| 35 | + InternalOnly: { type: 'object' }, |
| 36 | + }, |
| 37 | + }), |
| 38 | + }), |
| 39 | +})); |
| 40 | + |
| 41 | +import { generateNavigationModels } from '../generators/models.js'; |
| 42 | +import type { MethodSignature } from '../types.js'; |
| 43 | + |
| 44 | +function createTempSpecFile(contents: string): string { |
| 45 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'navigation-models-')); |
| 46 | + const specPath = path.join(tempDir, 'brownfield.navigation.ts'); |
| 47 | + fs.writeFileSync(specPath, contents); |
| 48 | + return specPath; |
| 49 | +} |
| 50 | + |
| 51 | +function cleanupTempSpecFile(specPath: string): void { |
| 52 | + fs.rmSync(path.dirname(specPath), { recursive: true, force: true }); |
| 53 | +} |
| 54 | + |
| 55 | +describe('generateNavigationModels', () => { |
| 56 | + const tempSpecFiles: string[] = []; |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + for (const specPath of tempSpecFiles) { |
| 60 | + cleanupTempSpecFile(specPath); |
| 61 | + } |
| 62 | + tempSpecFiles.length = 0; |
| 63 | + vi.clearAllMocks(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('generates models for complex referenced types', async () => { |
| 67 | + const specPath = createTempSpecFile(` |
| 68 | + export interface BrownfieldNavigationSpec { |
| 69 | + openProfile(profile: UserProfile): void; |
| 70 | + } |
| 71 | +
|
| 72 | + export interface UserProfile {} |
| 73 | + `); |
| 74 | + tempSpecFiles.push(specPath); |
| 75 | + |
| 76 | + const methods: MethodSignature[] = [ |
| 77 | + { |
| 78 | + name: 'openProfile', |
| 79 | + params: [{ name: 'profile', type: 'UserProfile', optional: false }], |
| 80 | + returnType: 'void', |
| 81 | + isAsync: false, |
| 82 | + }, |
| 83 | + ]; |
| 84 | + |
| 85 | + const models = await generateNavigationModels({ |
| 86 | + specPath, |
| 87 | + methods, |
| 88 | + kotlinPackageName: 'com.callstack.nativebrownfieldnavigation', |
| 89 | + }); |
| 90 | + |
| 91 | + expect(models.modelTypeNames).toEqual(['UserProfile']); |
| 92 | + expect(models.swiftModels).toContain('public struct UserProfile'); |
| 93 | + expect(models.kotlinModels).toContain('data class UserProfile'); |
| 94 | + expect(addSourceMock).toHaveBeenCalled(); |
| 95 | + expect(quicktypeMock).toHaveBeenCalledTimes(2); |
| 96 | + }); |
| 97 | + |
| 98 | + it('skips model generation when no complex types are referenced', async () => { |
| 99 | + const specPath = createTempSpecFile(` |
| 100 | + export interface BrownfieldNavigationSpec { |
| 101 | + open(route: string): void; |
| 102 | + } |
| 103 | + `); |
| 104 | + tempSpecFiles.push(specPath); |
| 105 | + |
| 106 | + const methods: MethodSignature[] = [ |
| 107 | + { |
| 108 | + name: 'open', |
| 109 | + params: [{ name: 'route', type: 'string', optional: false }], |
| 110 | + returnType: 'void', |
| 111 | + isAsync: false, |
| 112 | + }, |
| 113 | + ]; |
| 114 | + |
| 115 | + const models = await generateNavigationModels({ |
| 116 | + specPath, |
| 117 | + methods, |
| 118 | + kotlinPackageName: 'com.callstack.nativebrownfieldnavigation', |
| 119 | + }); |
| 120 | + |
| 121 | + expect(models).toEqual({ modelTypeNames: [] }); |
| 122 | + expect(quicktypeMock).not.toHaveBeenCalled(); |
| 123 | + }); |
| 124 | +}); |
0 commit comments