|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | + |
| 3 | +describe('Module Import Tests', () => { |
| 4 | + describe('ESM Import', () => { |
| 5 | + it('should import the main module successfully', async () => { |
| 6 | + // Test dynamic import |
| 7 | + const module = await import('../../src/index.js'); |
| 8 | + expect(module).toBeDefined(); |
| 9 | + expect(typeof module).toBe('object'); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should export all expected classes and types', async () => { |
| 13 | + const module = await import('../../src/index.js'); |
| 14 | + |
| 15 | + // Main SDK class |
| 16 | + expect(module.ClassifierSdk).toBeDefined(); |
| 17 | + expect(typeof module.ClassifierSdk).toBe('function'); |
| 18 | + |
| 19 | + // gRPC client |
| 20 | + expect(module.ClassifierServiceClient).toBeDefined(); |
| 21 | + expect(typeof module.ClassifierServiceClient).toBe('function'); |
| 22 | + |
| 23 | + // Enums |
| 24 | + expect(module.HashType).toBeDefined(); |
| 25 | + expect(module.ImageFormat).toBeDefined(); |
| 26 | + expect(module.RequestEncoding).toBeDefined(); |
| 27 | + expect(module.ErrorCode).toBeDefined(); |
| 28 | + |
| 29 | + // Types should be available for TypeScript (interfaces don't exist at runtime) |
| 30 | + // We can't test interfaces directly, but we can test that they compile |
| 31 | + }); |
| 32 | + |
| 33 | + it('should have correct enum values', async () => { |
| 34 | + const module = await import('../../src/index.js'); |
| 35 | + |
| 36 | + // Test HashType enum values |
| 37 | + expect(module.HashType.HASH_TYPE_MD5).toBe(1); |
| 38 | + expect(module.HashType.HASH_TYPE_SHA1).toBe(2); |
| 39 | + expect(module.HashType.HASH_TYPE_UNKNOWN).toBe(0); |
| 40 | + |
| 41 | + // Test ImageFormat enum values |
| 42 | + expect(module.ImageFormat.IMAGE_FORMAT_UNSPECIFIED).toBe(0); |
| 43 | + expect(module.ImageFormat.IMAGE_FORMAT_JPEG).toBe(2); |
| 44 | + expect(module.ImageFormat.IMAGE_FORMAT_PNG).toBe(5); |
| 45 | + |
| 46 | + // Test RequestEncoding enum values |
| 47 | + expect(module.RequestEncoding.REQUEST_ENCODING_UNSPECIFIED).toBe(0); |
| 48 | + expect(module.RequestEncoding.REQUEST_ENCODING_UNCOMPRESSED).toBe(1); |
| 49 | + expect(module.RequestEncoding.REQUEST_ENCODING_BROTLI).toBe(2); |
| 50 | + |
| 51 | + // Test ErrorCode enum values |
| 52 | + expect(module.ErrorCode.ERROR_CODE_UNSPECIFIED).toBe(0); |
| 53 | + expect(module.ErrorCode.ERROR_CODE_IMAGE_TOO_LARGE).toBe(2); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should allow creating SDK instance with correct configuration', async () => { |
| 57 | + const module = await import('../../src/index.js'); |
| 58 | + |
| 59 | + const sdk = new module.ClassifierSdk({ |
| 60 | + deploymentId: 'test-deployment', |
| 61 | + affiliate: 'test-affiliate', |
| 62 | + authentication: { |
| 63 | + issuerUrl: 'https://test-issuer.com', |
| 64 | + clientId: 'test-client-id', |
| 65 | + clientSecret: 'test-client-secret', |
| 66 | + scope: 'manage:classify', |
| 67 | + }, |
| 68 | + }); |
| 69 | + |
| 70 | + expect(sdk).toBeDefined(); |
| 71 | + expect(sdk).toBeInstanceOf(module.ClassifierSdk); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('Compatibility', () => { |
| 76 | + it('should be compatible with Node.js ESM module resolution', async () => { |
| 77 | + // Test that all relative imports resolve correctly |
| 78 | + const module = await import('../../src/index.js'); |
| 79 | + |
| 80 | + // Verify we can access nested exports |
| 81 | + expect(module.computeHashesFromStream).toBeDefined(); |
| 82 | + expect(typeof module.computeHashesFromStream).toBe('function'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should export the correct package structure for consumers', async () => { |
| 86 | + const module = await import('../../src/index.js'); |
| 87 | + |
| 88 | + // Test that the module exports match what consumers expect |
| 89 | + const exportedKeys = Object.keys(module); |
| 90 | + |
| 91 | + // Should include the main SDK class |
| 92 | + expect(exportedKeys).toContain('ClassifierSdk'); |
| 93 | + |
| 94 | + // Should include the gRPC client |
| 95 | + expect(exportedKeys).toContain('ClassifierServiceClient'); |
| 96 | + |
| 97 | + // Should include all enums |
| 98 | + expect(exportedKeys).toContain('HashType'); |
| 99 | + expect(exportedKeys).toContain('ImageFormat'); |
| 100 | + expect(exportedKeys).toContain('RequestEncoding'); |
| 101 | + expect(exportedKeys).toContain('ErrorCode'); |
| 102 | + |
| 103 | + // Should include utility functions |
| 104 | + expect(exportedKeys).toContain('computeHashesFromStream'); |
| 105 | + |
| 106 | + // Should include generated types/interfaces (these are exported for TypeScript) |
| 107 | + expect(exportedKeys).toContain('ClassifyRequest'); |
| 108 | + expect(exportedKeys).toContain('ClassifyResponse'); |
| 109 | + expect(exportedKeys).toContain('ClassificationInput'); |
| 110 | + expect(exportedKeys).toContain('ClassificationOutput'); |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments