Skip to content

Commit 45b86f5

Browse files
Copilotabraham
andcommitted
Split code into multiple files with one class/interface per file
Co-authored-by: abraham <3341+abraham@users.noreply.github.com>
1 parent 848dfcb commit 45b86f5

File tree

12 files changed

+614
-543
lines changed

12 files changed

+614
-543
lines changed

src/__tests__/generate.test.ts

Lines changed: 17 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,179 +1,28 @@
1+
// Test backward compatibility of the main generate file exports
12
import { EntityParser, EntityClass, EntityAttribute, MethodParser, ApiMethodsFile, ApiMethod, ApiParameter } from '../generate';
2-
import * as fs from 'fs';
3-
import * as path from 'path';
43

5-
describe('EntityParser', () => {
6-
let parser: EntityParser;
7-
8-
beforeEach(() => {
9-
parser = new EntityParser();
10-
});
11-
12-
test('should parse all entities without throwing errors', () => {
13-
expect(() => {
14-
const entities = parser.parseAllEntities();
15-
expect(entities).toBeInstanceOf(Array);
16-
expect(entities.length).toBeGreaterThan(0);
17-
}).not.toThrow();
18-
});
19-
20-
test('should parse entities and extract basic structure', () => {
21-
const entities = parser.parseAllEntities();
22-
23-
// Verify we found entities
24-
expect(entities.length).toBeGreaterThan(50); // Should be around 64 entities
25-
26-
// Find a specific entity to test
27-
const accountEntity = entities.find(e => e.name === 'Account');
28-
expect(accountEntity).toBeDefined();
29-
30-
if (accountEntity) {
31-
expect(accountEntity.name).toBe('Account');
32-
expect(accountEntity.description).toContain('user of Mastodon');
33-
expect(accountEntity.attributes.length).toBeGreaterThan(20); // Account has many attributes
34-
35-
// Check some specific attributes exist
36-
const idAttribute = accountEntity.attributes.find(attr => attr.name === 'id');
37-
expect(idAttribute).toBeDefined();
38-
expect(idAttribute?.type).toContain('String');
39-
40-
const usernameAttribute = accountEntity.attributes.find(attr => attr.name === 'username');
41-
expect(usernameAttribute).toBeDefined();
42-
expect(usernameAttribute?.type).toBe('String');
43-
}
44-
});
45-
46-
test('should correctly identify optional and deprecated attributes', () => {
47-
const entities = parser.parseAllEntities();
4+
describe('Generate module backward compatibility', () => {
5+
test('should export all interfaces and classes', () => {
6+
// Test that all exports are available from the main module
7+
expect(EntityParser).toBeDefined();
8+
expect(MethodParser).toBeDefined();
489

49-
// Find entities with optional/deprecated attributes
50-
let foundOptional = false;
51-
let foundDeprecated = false;
10+
// Test that interfaces can be used (they should be defined types)
11+
const parser = new EntityParser();
12+
expect(parser).toBeInstanceOf(EntityParser);
5213

53-
for (const entity of entities) {
54-
for (const attr of entity.attributes) {
55-
if (attr.optional) foundOptional = true;
56-
if (attr.deprecated) foundDeprecated = true;
57-
}
58-
}
59-
60-
expect(foundOptional).toBe(true);
61-
expect(foundDeprecated).toBe(true);
14+
const methodParser = new MethodParser();
15+
expect(methodParser).toBeInstanceOf(MethodParser);
6216
});
6317

64-
test('should parse entity with simple structure', () => {
18+
test('should maintain existing API surface', () => {
19+
// Ensure backward compatibility by testing that existing imports still work
20+
const parser = new EntityParser();
6521
const entities = parser.parseAllEntities();
22+
expect(entities).toBeInstanceOf(Array);
6623

67-
// Find Application entity which has a simpler structure
68-
const applicationEntity = entities.find(e => e.name === 'Application');
69-
expect(applicationEntity).toBeDefined();
70-
71-
if (applicationEntity) {
72-
expect(applicationEntity.name).toBe('Application');
73-
expect(applicationEntity.description).toContain('interfaces with the REST API');
74-
expect(applicationEntity.attributes.length).toBeGreaterThan(0);
75-
76-
// Check that name attribute exists
77-
const nameAttribute = applicationEntity.attributes.find(attr => attr.name === 'name');
78-
expect(nameAttribute).toBeDefined();
79-
expect(nameAttribute?.type).toBe('String');
80-
}
81-
});
82-
});
83-
84-
describe('MethodParser', () => {
85-
let methodParser: MethodParser;
86-
87-
beforeEach(() => {
88-
methodParser = new MethodParser();
89-
});
90-
91-
test('should parse all method files without throwing errors', () => {
92-
expect(() => {
93-
const methodFiles = methodParser.parseAllMethods();
94-
expect(methodFiles).toBeInstanceOf(Array);
95-
expect(methodFiles.length).toBeGreaterThan(0);
96-
}).not.toThrow();
97-
});
98-
99-
test('should parse method files and extract basic structure', () => {
100-
const methodFiles = methodParser.parseAllMethods();
101-
102-
// Verify we found method files
103-
expect(methodFiles.length).toBeGreaterThan(30); // Should be around 40 method files
104-
105-
// Find a specific method file to test
106-
const appsMethodFile = methodFiles.find(f => f.name.includes('apps'));
107-
expect(appsMethodFile).toBeDefined();
108-
109-
if (appsMethodFile) {
110-
expect(appsMethodFile.name).toContain('apps');
111-
expect(appsMethodFile.description).toContain('OAuth');
112-
expect(appsMethodFile.methods.length).toBeGreaterThan(0);
113-
114-
// Check that create app method exists
115-
const createMethod = appsMethodFile.methods.find(method =>
116-
method.name.toLowerCase().includes('create') && method.endpoint.includes('/api/v1/apps')
117-
);
118-
expect(createMethod).toBeDefined();
119-
120-
if (createMethod) {
121-
expect(createMethod.httpMethod).toBe('POST');
122-
expect(createMethod.endpoint).toBe('/api/v1/apps');
123-
expect(createMethod.description).toBeTruthy();
124-
}
125-
}
126-
});
127-
128-
test('should parse method parameters correctly', () => {
129-
const methodFiles = methodParser.parseAllMethods();
130-
131-
// Find a method with parameters
132-
let foundMethodWithParams = false;
133-
let foundRequiredParam = false;
134-
135-
for (const methodFile of methodFiles) {
136-
for (const method of methodFile.methods) {
137-
if (method.parameters && method.parameters.length > 0) {
138-
foundMethodWithParams = true;
139-
140-
for (const param of method.parameters) {
141-
expect(param.name).toBeTruthy();
142-
expect(param.description).toBeTruthy();
143-
144-
if (param.required) {
145-
foundRequiredParam = true;
146-
}
147-
}
148-
}
149-
}
150-
}
151-
152-
expect(foundMethodWithParams).toBe(true);
153-
expect(foundRequiredParam).toBe(true);
154-
});
155-
156-
test('should extract HTTP methods and endpoints correctly', () => {
24+
const methodParser = new MethodParser();
15725
const methodFiles = methodParser.parseAllMethods();
158-
159-
// Find some specific methods to verify
160-
let foundGetMethod = false;
161-
let foundPostMethod = false;
162-
let foundDeleteMethod = false;
163-
164-
for (const methodFile of methodFiles) {
165-
for (const method of methodFile.methods) {
166-
if (method.httpMethod === 'GET') foundGetMethod = true;
167-
if (method.httpMethod === 'POST') foundPostMethod = true;
168-
if (method.httpMethod === 'DELETE') foundDeleteMethod = true;
169-
170-
// Verify endpoint format (all endpoints should start with /)
171-
expect(method.endpoint).toMatch(/^\//);
172-
}
173-
}
174-
175-
expect(foundGetMethod).toBe(true);
176-
expect(foundPostMethod).toBe(true);
177-
expect(foundDeleteMethod).toBe(true);
26+
expect(methodFiles).toBeInstanceOf(Array);
17827
});
17928
});
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { EntityParser } from '../../parsers/EntityParser';
2+
import * as fs from 'fs';
3+
import * as path from 'path';
4+
5+
describe('EntityParser', () => {
6+
let parser: EntityParser;
7+
8+
beforeEach(() => {
9+
parser = new EntityParser();
10+
});
11+
12+
test('should parse all entities without throwing errors', () => {
13+
expect(() => {
14+
const entities = parser.parseAllEntities();
15+
expect(entities).toBeInstanceOf(Array);
16+
expect(entities.length).toBeGreaterThan(0);
17+
}).not.toThrow();
18+
});
19+
20+
test('should parse entities and extract basic structure', () => {
21+
const entities = parser.parseAllEntities();
22+
23+
// Verify we found entities
24+
expect(entities.length).toBeGreaterThan(50); // Should be around 64 entities
25+
26+
// Find a specific entity to test
27+
const accountEntity = entities.find(e => e.name === 'Account');
28+
expect(accountEntity).toBeDefined();
29+
30+
if (accountEntity) {
31+
expect(accountEntity.name).toBe('Account');
32+
expect(accountEntity.description).toContain('user of Mastodon');
33+
expect(accountEntity.attributes.length).toBeGreaterThan(20); // Account has many attributes
34+
35+
// Check some specific attributes exist
36+
const idAttribute = accountEntity.attributes.find(attr => attr.name === 'id');
37+
expect(idAttribute).toBeDefined();
38+
expect(idAttribute?.type).toContain('String');
39+
40+
const usernameAttribute = accountEntity.attributes.find(attr => attr.name === 'username');
41+
expect(usernameAttribute).toBeDefined();
42+
expect(usernameAttribute?.type).toBe('String');
43+
}
44+
});
45+
46+
test('should correctly identify optional and deprecated attributes', () => {
47+
const entities = parser.parseAllEntities();
48+
49+
// Find entities with optional/deprecated attributes
50+
let foundOptional = false;
51+
let foundDeprecated = false;
52+
53+
for (const entity of entities) {
54+
for (const attr of entity.attributes) {
55+
if (attr.optional) foundOptional = true;
56+
if (attr.deprecated) foundDeprecated = true;
57+
}
58+
}
59+
60+
expect(foundOptional).toBe(true);
61+
expect(foundDeprecated).toBe(true);
62+
});
63+
64+
test('should parse entity with simple structure', () => {
65+
const entities = parser.parseAllEntities();
66+
67+
// Find Application entity which has a simpler structure
68+
const applicationEntity = entities.find(e => e.name === 'Application');
69+
expect(applicationEntity).toBeDefined();
70+
71+
if (applicationEntity) {
72+
expect(applicationEntity.name).toBe('Application');
73+
expect(applicationEntity.description).toContain('interfaces with the REST API');
74+
expect(applicationEntity.attributes.length).toBeGreaterThan(0);
75+
76+
// Check that name attribute exists
77+
const nameAttribute = applicationEntity.attributes.find(attr => attr.name === 'name');
78+
expect(nameAttribute).toBeDefined();
79+
expect(nameAttribute?.type).toBe('String');
80+
}
81+
});
82+
});
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { MethodParser } from '../../parsers/MethodParser';
2+
import * as fs from 'fs';
3+
import * as path from 'path';
4+
5+
describe('MethodParser', () => {
6+
let methodParser: MethodParser;
7+
8+
beforeEach(() => {
9+
methodParser = new MethodParser();
10+
});
11+
12+
test('should parse all method files without throwing errors', () => {
13+
expect(() => {
14+
const methodFiles = methodParser.parseAllMethods();
15+
expect(methodFiles).toBeInstanceOf(Array);
16+
expect(methodFiles.length).toBeGreaterThan(0);
17+
}).not.toThrow();
18+
});
19+
20+
test('should parse method files and extract basic structure', () => {
21+
const methodFiles = methodParser.parseAllMethods();
22+
23+
// Verify we found method files
24+
expect(methodFiles.length).toBeGreaterThan(30); // Should be around 40 method files
25+
26+
// Find a specific method file to test
27+
const appsMethodFile = methodFiles.find(f => f.name.includes('apps'));
28+
expect(appsMethodFile).toBeDefined();
29+
30+
if (appsMethodFile) {
31+
expect(appsMethodFile.name).toContain('apps');
32+
expect(appsMethodFile.description).toContain('OAuth');
33+
expect(appsMethodFile.methods.length).toBeGreaterThan(0);
34+
35+
// Check that create app method exists
36+
const createMethod = appsMethodFile.methods.find(method =>
37+
method.name.toLowerCase().includes('create') && method.endpoint.includes('/api/v1/apps')
38+
);
39+
expect(createMethod).toBeDefined();
40+
41+
if (createMethod) {
42+
expect(createMethod.httpMethod).toBe('POST');
43+
expect(createMethod.endpoint).toBe('/api/v1/apps');
44+
expect(createMethod.description).toBeTruthy();
45+
}
46+
}
47+
});
48+
49+
test('should parse method parameters correctly', () => {
50+
const methodFiles = methodParser.parseAllMethods();
51+
52+
// Find a method with parameters
53+
let foundMethodWithParams = false;
54+
let foundRequiredParam = false;
55+
56+
for (const methodFile of methodFiles) {
57+
for (const method of methodFile.methods) {
58+
if (method.parameters && method.parameters.length > 0) {
59+
foundMethodWithParams = true;
60+
61+
for (const param of method.parameters) {
62+
expect(param.name).toBeTruthy();
63+
expect(param.description).toBeTruthy();
64+
65+
if (param.required) {
66+
foundRequiredParam = true;
67+
}
68+
}
69+
}
70+
}
71+
}
72+
73+
expect(foundMethodWithParams).toBe(true);
74+
expect(foundRequiredParam).toBe(true);
75+
});
76+
77+
test('should extract HTTP methods and endpoints correctly', () => {
78+
const methodFiles = methodParser.parseAllMethods();
79+
80+
// Find some specific methods to verify
81+
let foundGetMethod = false;
82+
let foundPostMethod = false;
83+
let foundDeleteMethod = false;
84+
85+
for (const methodFile of methodFiles) {
86+
for (const method of methodFile.methods) {
87+
if (method.httpMethod === 'GET') foundGetMethod = true;
88+
if (method.httpMethod === 'POST') foundPostMethod = true;
89+
if (method.httpMethod === 'DELETE') foundDeleteMethod = true;
90+
91+
// Verify endpoint format (all endpoints should start with /)
92+
expect(method.endpoint).toMatch(/^\//);
93+
}
94+
}
95+
96+
expect(foundGetMethod).toBe(true);
97+
expect(foundPostMethod).toBe(true);
98+
expect(foundDeleteMethod).toBe(true);
99+
});
100+
});

0 commit comments

Comments
 (0)