|
| 1 | +import { describe, it, beforeAll } from 'vitest'; |
| 2 | +import { ClassifierSdk, ImageFormat, parseAudience } from '../../src'; |
| 3 | +import fs from 'fs'; |
| 4 | +import path from 'path'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Expected outputs schema from testcases |
| 8 | + */ |
| 9 | +interface ExpectedOutputs { |
| 10 | + classification_labels: string[]; |
| 11 | + images: [string, number[]][]; // [filename, weights[]] |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Determine image format from filename extension |
| 16 | + */ |
| 17 | +function getImageFormat(filename: string): ImageFormat { |
| 18 | + const ext = path.extname(filename).toLowerCase(); |
| 19 | + switch (ext) { |
| 20 | + case '.jpg': |
| 21 | + case '.jpeg': |
| 22 | + return ImageFormat.IMAGE_FORMAT_JPEG; |
| 23 | + case '.png': |
| 24 | + return ImageFormat.IMAGE_FORMAT_PNG; |
| 25 | + case '.gif': |
| 26 | + return ImageFormat.IMAGE_FORMAT_GIF; |
| 27 | + case '.bmp': |
| 28 | + return ImageFormat.IMAGE_FORMAT_BMP; |
| 29 | + case '.webp': |
| 30 | + return ImageFormat.IMAGE_FORMAT_WEBP; |
| 31 | + default: |
| 32 | + return ImageFormat.IMAGE_FORMAT_PNG; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Default tolerance for floating-point comparisons. |
| 38 | + * See athena-protobufs/testcases/README.md for guidance: |
| 39 | + * - 1e-4 (0.0001) for exact model match |
| 40 | + * - 1e-2 (0.01) for model version drift |
| 41 | + * - 0.05 (5%) for behavioral validation |
| 42 | + */ |
| 43 | +const DEFAULT_TOLERANCE = 0.0001; |
| 44 | + |
| 45 | +describe('E2E Test Cases', () => { |
| 46 | + const testcasesDir = path.resolve(__dirname, '../../athena-protobufs/testcases'); |
| 47 | + const testset = 'integrator_sample'; |
| 48 | + const tolerance = parseFloat(process.env.E2E_TOLERANCE ?? '') || DEFAULT_TOLERANCE; |
| 49 | + |
| 50 | + let sdk: ClassifierSdk; |
| 51 | + let expectedOutputs: ExpectedOutputs; |
| 52 | + let imagesDir: string; |
| 53 | + |
| 54 | + beforeAll(() => { |
| 55 | + // Initialize SDK with environment configuration |
| 56 | + sdk = new ClassifierSdk({ |
| 57 | + deploymentId: process.env.VITE_ATHENA_DEPLOYMENT_ID ?? '', |
| 58 | + affiliate: process.env.VITE_ATHENA_AFFILIATE ?? '', |
| 59 | + grpcAddress: |
| 60 | + process.env.VITE_ATHENA_GRPC_ADDRESS ?? |
| 61 | + 'trust-messages-global.crispthinking.com:443', |
| 62 | + authentication: { |
| 63 | + issuerUrl: |
| 64 | + process.env.VITE_OAUTH_ISSUER ?? 'https://crispthinking.auth0.com/', |
| 65 | + clientId: process.env.VITE_ATHENA_CLIENT_ID ?? '', |
| 66 | + clientSecret: process.env.VITE_ATHENA_CLIENT_SECRET ?? '', |
| 67 | + audience: parseAudience(process.env.VITE_ATHENA_AUDIENCE), |
| 68 | + scope: 'manage:classify', |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + // Load expected outputs |
| 73 | + const expectedOutputsPath = path.join( |
| 74 | + testcasesDir, |
| 75 | + testset, |
| 76 | + 'expected_outputs.json', |
| 77 | + ); |
| 78 | + expectedOutputs = JSON.parse( |
| 79 | + fs.readFileSync(expectedOutputsPath, 'utf-8'), |
| 80 | + ) as ExpectedOutputs; |
| 81 | + imagesDir = path.join(testcasesDir, testset, 'images'); |
| 82 | + }); |
| 83 | + |
| 84 | + describe(`${testset} testcases`, () => { |
| 85 | + it('should load expected outputs correctly', ({ expect }) => { |
| 86 | + expect(expectedOutputs).toBeDefined(); |
| 87 | + expect(expectedOutputs.classification_labels).toBeInstanceOf(Array); |
| 88 | + expect(expectedOutputs.classification_labels.length).toBeGreaterThan(0); |
| 89 | + expect(expectedOutputs.images).toBeInstanceOf(Array); |
| 90 | + expect(expectedOutputs.images.length).toBeGreaterThan(0); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should have all test images available', ({ expect }) => { |
| 94 | + for (const [filename] of expectedOutputs.images) { |
| 95 | + const imagePath = path.join(imagesDir, filename); |
| 96 | + expect( |
| 97 | + fs.existsSync(imagePath), |
| 98 | + `Image not found: ${filename}`, |
| 99 | + ).toBe(true); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + it('should classify all images and match expected outputs', async ({ |
| 104 | + expect, |
| 105 | + annotate, |
| 106 | + }) => { |
| 107 | + const labels = expectedOutputs.classification_labels; |
| 108 | + const failures: { |
| 109 | + filename: string; |
| 110 | + differences: { label: string; expected: number; actual: number; diff: number }[]; |
| 111 | + }[] = []; |
| 112 | + |
| 113 | + annotate(`Running ${expectedOutputs.images.length} test images with tolerance ${tolerance}`); |
| 114 | + |
| 115 | + for (const [filename, expectedWeights] of expectedOutputs.images) { |
| 116 | + const imagePath = path.join(imagesDir, filename); |
| 117 | + |
| 118 | + const response = await sdk.classifySingle({ |
| 119 | + data: fs.createReadStream(imagePath), |
| 120 | + format: getImageFormat(filename), |
| 121 | + }); |
| 122 | + |
| 123 | + expect(response.error, `Classification error for ${filename}`).toBeNull(); |
| 124 | + |
| 125 | + // Build actual weights map |
| 126 | + const actualWeights = new Map<string, number>(); |
| 127 | + if (response.classifications) { |
| 128 | + for (const classification of response.classifications) { |
| 129 | + actualWeights.set(classification.label, classification.weight); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // Compare each label |
| 134 | + const differences: { label: string; expected: number; actual: number; diff: number }[] = []; |
| 135 | + for (let i = 0; i < labels.length; i++) { |
| 136 | + const label = labels[i]; |
| 137 | + const expected = expectedWeights[i]; |
| 138 | + const actual = actualWeights.get(label) ?? 0; |
| 139 | + const diff = Math.abs(expected - actual); |
| 140 | + |
| 141 | + if (diff > tolerance) { |
| 142 | + differences.push({ label, expected, actual, diff }); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + if (differences.length > 0) { |
| 147 | + failures.push({ filename, differences }); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // Report all failures at once for better test output |
| 152 | + if (failures.length > 0) { |
| 153 | + const failureDetails = failures |
| 154 | + .map( |
| 155 | + (f) => |
| 156 | + `${f.filename}:\n${f.differences |
| 157 | + .map( |
| 158 | + (d) => |
| 159 | + ` - ${d.label}: expected ${d.expected.toFixed(4)}, got ${d.actual.toFixed(4)} (diff: ${d.diff.toFixed(4)})`, |
| 160 | + ) |
| 161 | + .join('\n')}`, |
| 162 | + ) |
| 163 | + .join('\n\n'); |
| 164 | + |
| 165 | + annotate(`Failures:\n${failureDetails}`); |
| 166 | + } |
| 167 | + |
| 168 | + expect( |
| 169 | + failures.length, |
| 170 | + `${failures.length} images failed classification comparison`, |
| 171 | + ).toBe(0); |
| 172 | + }, 120000); // 2 minute timeout for all images |
| 173 | + }); |
| 174 | +}); |
0 commit comments