|
1 | 1 | import assert from 'node:assert'; |
| 2 | +import type { ResourcesEndpoint } from '@datocms/rest-api-reference'; |
2 | 3 | import { |
3 | 4 | type CmaClientProgram, |
4 | 5 | extractAllMethodNames, |
5 | 6 | extractMethodSignature, |
| 7 | + extractResourcesEndpointMethods, |
6 | 8 | extractTypeDependencies, |
7 | 9 | getCmaClientProgram, |
8 | 10 | } from '../src'; |
@@ -51,6 +53,19 @@ describe('TypeScript Compiler API - Unit Tests', () => { |
51 | 53 | expect(methodNames).toContain('updateFromUrl'); |
52 | 54 | }, 10000); |
53 | 55 |
|
| 56 | + it('should extract all method names from roles resource including special methods', () => { |
| 57 | + const { checker, clientClass } = cma; |
| 58 | + |
| 59 | + const methodNames = extractAllMethodNames(checker, clientClass, 'roles'); |
| 60 | + |
| 61 | + expect(methodNames).toBeDefined(); |
| 62 | + expect(methodNames.length).toBeGreaterThan(0); |
| 63 | + expect(methodNames).toContain('create'); |
| 64 | + expect(methodNames).toContain('update'); |
| 65 | + expect(methodNames).toContain('duplicate'); |
| 66 | + expect(methodNames).toContain('updateCurrentEnvironmentPermissions'); |
| 67 | + }, 10000); |
| 68 | + |
54 | 69 | it('should return empty array for invalid resource', () => { |
55 | 70 | const { checker, clientClass } = cma; |
56 | 71 |
|
@@ -199,6 +214,89 @@ describe('TypeScript Compiler API - Unit Tests', () => { |
199 | 214 | }, 10000); |
200 | 215 | }); |
201 | 216 |
|
| 217 | + describe('extractResourcesEndpointMethods', () => { |
| 218 | + // Minimal endpoint shape: extractResourcesEndpointMethods only reads |
| 219 | + // `namespace` and `docUrl`. Cast through unknown to avoid spelling out |
| 220 | + // every required field on ResourcesEndpoint. |
| 221 | + const makeEndpoint = ( |
| 222 | + namespace: string, |
| 223 | + docUrl: string, |
| 224 | + ): ResourcesEndpoint => ({ namespace, docUrl }) as unknown as ResourcesEndpoint; |
| 225 | + |
| 226 | + it('groups items.list/rawList/listPagedIterator/rawListPagedIterator under /item/instances', () => { |
| 227 | + const { checker, clientClass } = cma; |
| 228 | + |
| 229 | + const methods = extractResourcesEndpointMethods( |
| 230 | + checker, |
| 231 | + clientClass, |
| 232 | + makeEndpoint( |
| 233 | + 'items', |
| 234 | + 'https://www.datocms.com/docs/content-management-api/resources/item/instances', |
| 235 | + ), |
| 236 | + ); |
| 237 | + |
| 238 | + const names = methods.map((m) => m.name).sort(); |
| 239 | + expect(names).toEqual( |
| 240 | + [ |
| 241 | + 'list', |
| 242 | + 'listPagedIterator', |
| 243 | + 'rawList', |
| 244 | + 'rawListPagedIterator', |
| 245 | + ].sort(), |
| 246 | + ); |
| 247 | + |
| 248 | + for (const m of methods) { |
| 249 | + expect(m.functionDefinition).toContain(m.name); |
| 250 | + expect(m.referencedTypes.size).toBeGreaterThan(0); |
| 251 | + } |
| 252 | + }, 10000); |
| 253 | + |
| 254 | + it('groups roles.update and rawUpdate under /role/update', () => { |
| 255 | + const { checker, clientClass } = cma; |
| 256 | + |
| 257 | + const methods = extractResourcesEndpointMethods( |
| 258 | + checker, |
| 259 | + clientClass, |
| 260 | + makeEndpoint( |
| 261 | + 'roles', |
| 262 | + 'https://www.datocms.com/docs/content-management-api/resources/role/update', |
| 263 | + ), |
| 264 | + ); |
| 265 | + |
| 266 | + const names = methods.map((m) => m.name).sort(); |
| 267 | + expect(names).toContain('update'); |
| 268 | + expect(names).toContain('rawUpdate'); |
| 269 | + expect(names).toContain('updateCurrentEnvironmentPermissions'); |
| 270 | + }, 10000); |
| 271 | + |
| 272 | + it('returns empty array for an unknown docUrl', () => { |
| 273 | + const { checker, clientClass } = cma; |
| 274 | + |
| 275 | + const methods = extractResourcesEndpointMethods( |
| 276 | + checker, |
| 277 | + clientClass, |
| 278 | + makeEndpoint('items', 'https://example.invalid/does/not/exist'), |
| 279 | + ); |
| 280 | + |
| 281 | + expect(methods).toEqual([]); |
| 282 | + }, 10000); |
| 283 | + |
| 284 | + it('returns empty array for an unknown namespace', () => { |
| 285 | + const { checker, clientClass } = cma; |
| 286 | + |
| 287 | + const methods = extractResourcesEndpointMethods( |
| 288 | + checker, |
| 289 | + clientClass, |
| 290 | + makeEndpoint( |
| 291 | + 'invalid_resource_xyz', |
| 292 | + 'https://www.datocms.com/docs/content-management-api/resources/item/instances', |
| 293 | + ), |
| 294 | + ); |
| 295 | + |
| 296 | + expect(methods).toEqual([]); |
| 297 | + }, 10000); |
| 298 | + }); |
| 299 | + |
202 | 300 | describe('extractTypeDependencies', () => { |
203 | 301 | it('should handle empty type list', () => { |
204 | 302 | const { program, checker } = cma; |
|
0 commit comments