|
1 | | -import { describe, it, afterEach, beforeEach, vi, expect } from 'vitest'; |
2 | | -import { classifyImage, computeHashesFromStream, ClassifyImageOptions } from '../../src/main.js'; |
3 | | - |
4 | | - |
| 1 | +import { describe, it,} from 'vitest'; |
| 2 | +import { ClassifierSdk, ClassifyImageOptions, ClassifyResponse, ImageFormat } from '../../src/main'; |
5 | 3 | import fs from 'fs'; |
| 4 | +import { randomUUID } from 'crypto'; |
| 5 | + |
| 6 | +describe('classifierHelper', () => { |
| 7 | + it('should listDeployments and return responses using stubbed api)', async ({expect}) => { |
| 8 | + // This is a smoke test. You must have a running gRPC server at localhost:50051 for this to pass. |
| 9 | + // You may want to mock the gRPC client for true unit testing. |
| 10 | + const sdk = new ClassifierSdk({ |
| 11 | + deploymentId: process.env.VITE_ATHENA_DEPLOYMENT_ID, |
| 12 | + affiliate: process.env.VITE_ATHENA_AFFILIATE, |
| 13 | + authentication: { |
| 14 | + issuerUrl: 'https://crispthinking.auth0.com/', |
| 15 | + clientId: process.env.VITE_ATHENA_CLIENT_ID, |
| 16 | + clientSecret: process.env.VITE_ATHENA_CLIENT_SECRET, |
| 17 | + scope: 'manage:classify' |
| 18 | + } |
| 19 | + }); |
| 20 | + // This will fail if no server is running, but will exercise the code path. |
| 21 | + let error: any = null; |
| 22 | + try { |
| 23 | + const responses = await sdk.listDeployments(); |
| 24 | + expect(Array.isArray(responses)).toBe(true); |
| 25 | + } catch (err) { |
| 26 | + error = err; |
| 27 | + } |
| 28 | + // Assert error is unset |
| 29 | + expect(error).toBeNull(); |
| 30 | + }, { timeout: 10000 }) |
| 31 | +}); |
6 | 32 |
|
7 | 33 | describe('classifyImage function', () => { |
8 | | - it('should classify Steamboat-willie.jpg and return responses (integration smoke test)', async () => { |
| 34 | + it('should classify Steamboat-willie.jpg and return responses (integration smoke test)', async ({expect}) => { |
9 | 35 | // This is a smoke test. You must have a running gRPC server at localhost:50051 for this to pass. |
10 | 36 | // You may want to mock the gRPC client for true unit testing. |
11 | 37 | const imagePath = __dirname + '/Steamboat-willie.jpg'; |
| 38 | + const sdk = new ClassifierSdk({ |
| 39 | + deploymentId: process.env.VITE_ATHENA_DEPLOYMENT_ID, |
| 40 | + affiliate: process.env.VITE_ATHENA_AFFILIATE, |
| 41 | + authentication: { |
| 42 | + issuerUrl: 'https://crispthinking.auth0.com/', |
| 43 | + clientId: process.env.VITE_ATHENA_CLIENT_ID, |
| 44 | + clientSecret: process.env.VITE_ATHENA_CLIENT_SECRET, |
| 45 | + scope: 'manage:classify' |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + const correlationId = randomUUID(); |
| 50 | + |
| 51 | + // Create a promise to wrap the event emitter event 'data' |
| 52 | + const promise = new Promise<ClassifyResponse>((resolve, reject) => { |
| 53 | + sdk.once('data', (data) => { |
| 54 | + const byCorrelationId = data.outputs.filter(o => o.correlationId === correlationId); |
| 55 | + if (byCorrelationId.length > 0) { |
| 56 | + resolve(data); |
| 57 | + } |
| 58 | + sdk.close(); |
| 59 | + }); |
| 60 | + sdk.once('error', (err) => { |
| 61 | + reject(err); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + // This will fail if no server is running, but will exercise the code path. |
| 66 | + let error: any = undefined; |
| 67 | + |
| 68 | + await sdk.open(); |
| 69 | + |
12 | 70 | const imageStream = fs.createReadStream(imagePath); |
13 | 71 | const options: ClassifyImageOptions = { |
14 | 72 | imageStream, |
15 | | - deploymentId: 'test-deployment', |
16 | | - affiliate: 'test-affiliate', |
17 | | - correlationId: 'test-correlation', |
18 | | - // encoding, format, grpcAddress use defaults |
| 73 | + format: ImageFormat.PNG, |
| 74 | + correlationId |
19 | 75 | }; |
20 | | - // This will fail if no server is running, but will exercise the code path. |
21 | | - let error: any = null; |
22 | 76 | try { |
23 | | - const responses = await classifyImage(options); |
24 | | - expect(Array.isArray(responses)).toBe(true); |
| 77 | + await sdk.sendClassifyRequest(options); |
25 | 78 | } catch (err) { |
26 | 79 | error = err; |
27 | 80 | } |
| 81 | + |
| 82 | + // Wait for classifier to process some data.... |
| 83 | + const first = await promise; |
| 84 | + |
| 85 | + expect(first).toBeDefined(); |
| 86 | + |
| 87 | + const byCorrelationId = first.outputs.filter(o => o.correlationId === correlationId); |
| 88 | + |
| 89 | + expect(byCorrelationId.length).toBeGreaterThan(0); |
| 90 | + |
28 | 91 | // Accept either a successful call or a connection error (for CI/dev convenience) |
29 | | - if (error) { |
30 | | - expect(error.message).toMatch(/connect|unavailable|ECONNREFUSED|14/); |
31 | | - } |
32 | | - }); |
| 92 | + expect(error).toBeUndefined(); |
| 93 | + }, { timeout: 120000 }); |
33 | 94 | }); |
0 commit comments