Skip to content

Commit d15beeb

Browse files
test: add module import tests and update existing test files
- Add new module-import.test.ts to verify ESM compatibility - Update functional and unit tests for improved coverage - Modify documentation files and hash-server sample - Ensure proper module loading across different environments
1 parent cf57f68 commit d15beeb

7 files changed

Lines changed: 146 additions & 22 deletions

File tree

__tests__/functional/main.functional.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('ClassifierSdk Functional Tests', () => {
5656

5757
const input: ClassifyImageInput = {
5858
data: fs.createReadStream(imagePath),
59-
format: ImageFormat.PNG,
59+
format: ImageFormat.IMAGE_FORMAT_PNG,
6060
};
6161

6262
const response = await sdk.classifySingle(input);
@@ -97,7 +97,7 @@ describe('ClassifierSdk Functional Tests', () => {
9797
const inputs: ClassifyImageInput[] = correlationIds.map(
9898
(correlationId) => ({
9999
data: fs.createReadStream(imagePath),
100-
format: ImageFormat.PNG,
100+
format: ImageFormat.IMAGE_FORMAT_PNG,
101101
correlationId,
102102
}),
103103
);
@@ -300,7 +300,7 @@ describe('ClassifierSdk Functional Tests', () => {
300300
const options: ClassifyImageInput = {
301301
data,
302302
correlationId,
303-
format: ImageFormat.JPEG,
303+
format: ImageFormat.IMAGE_FORMAT_JPEG,
304304
};
305305
try {
306306
await sdk.sendClassifyRequest(options);

__tests__/unit/hashing.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ describe('hashing', () => {
1313

1414
//assert error is thrown for invalid image
1515
const { data, format, md5, sha1 } = await computeHashesFromStream(imageStream,
16-
RequestEncoding.UNCOMPRESSED,
17-
ImageFormat.JPEG,
16+
RequestEncoding.REQUEST_ENCODING_UNCOMPRESSED,
17+
ImageFormat.IMAGE_FORMAT_JPEG,
1818
false,
19-
[HashType.MD5, HashType.SHA1]
19+
[HashType.HASH_TYPE_MD5, HashType.HASH_TYPE_SHA1]
2020
);
2121

2222
expect(data).toBeDefined();
23-
expect(format).toBe(ImageFormat.JPEG);
23+
expect(format).toBe(ImageFormat.IMAGE_FORMAT_JPEG);
2424

2525
expect(md5).toEqual('eb3a95fdd86ce28d9a63a68328783874');
2626
expect(sha1).toEqual('b972b222bc91c457d904ebff16134dc79b67d1c9');
@@ -35,14 +35,14 @@ describe('hashing', () => {
3535

3636
//assert error is thrown for invalid image
3737
const { data, format, md5, sha1 } = await computeHashesFromStream(imageStream,
38-
RequestEncoding.UNCOMPRESSED,
39-
ImageFormat.JPEG,
38+
RequestEncoding.REQUEST_ENCODING_UNCOMPRESSED,
39+
ImageFormat.IMAGE_FORMAT_JPEG,
4040
true,
41-
[HashType.MD5, HashType.SHA1]
41+
[HashType.HASH_TYPE_MD5, HashType.HASH_TYPE_SHA1]
4242
);
4343

4444
expect(data).toBeDefined();
45-
expect(format).toBe(ImageFormat.RAW_UINT8);
45+
expect(format).toBe(ImageFormat.IMAGE_FORMAT_RAW_UINT8);
4646

4747
expect(md5).toEqual('d390b6cd7436fd41d1bcd005e7e3e652');
4848
expect(sha1).toEqual('7f979ca35cfe390bd92f5dfa4a05919da76f0e43');

__tests__/unit/main.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ describe('ClassifierSdk', () => {
6464

6565
describe('ImageFormat enum', () => {
6666
it('should have all expected image formats', () => {
67-
expect(ImageFormat.PNG).toBeDefined();
68-
expect(ImageFormat.JPEG).toBeDefined();
69-
expect(ImageFormat.RAW_UINT8).toBeDefined();
67+
expect(ImageFormat.IMAGE_FORMAT_PNG).toBeDefined();
68+
expect(ImageFormat.IMAGE_FORMAT_JPEG).toBeDefined();
69+
expect(ImageFormat.IMAGE_FORMAT_RAW_UINT8).toBeDefined();
7070
});
7171

7272
it('should have correct values for image formats', () => {
73-
expect(typeof ImageFormat.PNG).toBe('number');
74-
expect(typeof ImageFormat.JPEG).toBe('number');
75-
expect(typeof ImageFormat.RAW_UINT8).toBe('number');
73+
expect(typeof ImageFormat.IMAGE_FORMAT_PNG).toBe('number');
74+
expect(typeof ImageFormat.IMAGE_FORMAT_JPEG).toBe('number');
75+
expect(typeof ImageFormat.IMAGE_FORMAT_RAW_UINT8).toBe('number');
7676
});
7777
});
7878

@@ -129,7 +129,7 @@ describe('ClassifierSdk', () => {
129129
it('should throw error when sendClassifyRequest called without open', async () => {
130130
const input = {
131131
data: Buffer.from('test'),
132-
format: ImageFormat.PNG,
132+
format: ImageFormat.IMAGE_FORMAT_PNG,
133133
};
134134

135135
await expect(sdk.sendClassifyRequest(input)).rejects.toThrow(
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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('Static Import', () => {
76+
it('should properly reject CommonJS require() for ESM-only package', () => {
77+
// This test verifies that require() correctly fails for ESM-only packages
78+
expect(() => {
79+
// This should throw ERR_REQUIRE_ESM since we're an ESM-only package
80+
const { ClassifierSdk, HashType, ImageFormat, RequestEncoding, ErrorCode } = require('../../dist/index.js');
81+
return { ClassifierSdk, HashType, ImageFormat, RequestEncoding, ErrorCode };
82+
}).toThrow(/ERR_REQUIRE_ESM|require\(\) of ES Module.*not supported/);
83+
});
84+
});
85+
86+
describe('Compatibility', () => {
87+
it('should be compatible with Node.js ESM module resolution', async () => {
88+
// Test that all relative imports resolve correctly
89+
const module = await import('../../src/index.js');
90+
91+
// Verify we can access nested exports
92+
expect(module.computeHashesFromStream).toBeDefined();
93+
expect(typeof module.computeHashesFromStream).toBe('function');
94+
});
95+
96+
it('should export the correct package structure for consumers', async () => {
97+
const module = await import('../../src/index.js');
98+
99+
// Test that the module exports match what consumers expect
100+
const exportedKeys = Object.keys(module);
101+
102+
// Should include the main SDK class
103+
expect(exportedKeys).toContain('ClassifierSdk');
104+
105+
// Should include the gRPC client
106+
expect(exportedKeys).toContain('ClassifierServiceClient');
107+
108+
// Should include all enums
109+
expect(exportedKeys).toContain('HashType');
110+
expect(exportedKeys).toContain('ImageFormat');
111+
expect(exportedKeys).toContain('RequestEncoding');
112+
expect(exportedKeys).toContain('ErrorCode');
113+
114+
// Should include utility functions
115+
expect(exportedKeys).toContain('computeHashesFromStream');
116+
117+
// Should include generated types/interfaces (these are exported for TypeScript)
118+
expect(exportedKeys).toContain('ClassifyRequest');
119+
expect(exportedKeys).toContain('ClassifyResponse');
120+
expect(exportedKeys).toContain('ClassificationInput');
121+
expect(exportedKeys).toContain('ClassificationOutput');
122+
});
123+
});
124+
});

docs/api_reference.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Usage Example
2424
deploymentId: 'your-deployment-id',
2525
affiliate: 'your-affiliate',
2626
authentication: {
27-
issuer: 'https://issuer.example.com',
27+
issuerUrl: 'https://issuer.example.com',
2828
clientId: 'your-client-id',
2929
clientSecret: 'your-client-secret',
3030
scope: 'manage:classify'

docs/authenticationManager.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ Manages OAuth authentication and token refresh for the Athena gRPC client. Hand
1616
const auth = new AuthenticationManager({
1717
clientId: 'your-client-id',
1818
clientSecret: 'your-client-secret',
19-
issuer: 'https://issuer.example.com',
19+
issuerUrl: 'https://issuer.example.com',
2020
scope: 'manage:classify',
2121
});
2222
2323
**Options:**
2424

2525
* `clientId`: OAuth client ID
2626
* `clientSecret`: OAuth client secret
27-
* `issuer`: URL of the OAuth issuer
27+
* `issuerUrl`: URL of the OAuth issuer
2828
* `autoRefresh`: Whether to automatically refresh the access token
2929
* `scope`: OAuth scope to request
3030

samples/hash-server/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const sdk = new ClassifierSdk({
88
authentication: {
99
clientId: process.env.VITE_ATHENA_CLIENT_ID,
1010
clientSecret: process.env.VITE_ATHENA_CLIENT_SECRET,
11-
issuer: process.env.VITE_OAUTH_ISSUER,
11+
issuerUrl: process.env.VITE_OAUTH_ISSUER,
1212
scope: "manage:classify"
1313
},
1414
grpcAddress: 'csam-classification-messages.crispdev.com:443',

0 commit comments

Comments
 (0)