Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions __tests__/functional/e2e.functional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ describe('E2E Test Cases', () => {
clientId: process.env.VITE_ATHENA_CLIENT_ID ?? '',
clientSecret: process.env.VITE_ATHENA_CLIENT_SECRET ?? '',
audience: parseAudience(process.env.VITE_ATHENA_AUDIENCE),
scope: 'manage:classify',
},
});

Expand Down Expand Up @@ -120,7 +119,7 @@ describe('E2E Test Cases', () => {
format: getImageFormat(filename),
});

expect(response.error, `Classification error for ${filename}`).toBeNull();
expect(response.error, `Classification error for ${filename}`).toBeUndefined();

// Build actual weights map
const actualWeights = new Map<string, number>();
Expand Down
29 changes: 19 additions & 10 deletions __tests__/functional/main.functional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ClassifierSdk,
type ClassifyImageInput,
ImageFormat,
parseAudience,
} from '../../src/index.js';
import fs from 'fs';
import { randomUUID } from 'crypto';
Expand All @@ -13,16 +14,18 @@ describe('ClassifierSdk Functional Tests', () => {
it('should listDeployments and return responses (smoke test)', async ({
expect,
}) => {
// This is a smoke test. You must have a running gRPC server at localhost:50051 for this to pass.
// This is a smoke test. The gRPC server address is taken from VITE_ATHENA_GRPC_ADDRESS,
// falling back to the SDK default if the variable is not set.
// You may want to mock the gRPC client for true unit testing.
const sdk = new ClassifierSdk({
deploymentId: process.env.VITE_ATHENA_DEPLOYMENT_ID,
affiliate: process.env.VITE_ATHENA_AFFILIATE,
grpcAddress: process.env.VITE_ATHENA_GRPC_ADDRESS,
authentication: {
issuerUrl: process.env.VITE_OAUTH_ISSUER,
clientId: process.env.VITE_ATHENA_CLIENT_ID,
clientSecret: process.env.VITE_ATHENA_CLIENT_SECRET,
scope: 'manage:classify',
audience: parseAudience(process.env.VITE_ATHENA_AUDIENCE),
},
Comment thread
corpo-iwillspeak marked this conversation as resolved.
});

Expand All @@ -46,11 +49,12 @@ describe('ClassifierSdk Functional Tests', () => {
const sdk = new ClassifierSdk({
deploymentId: process.env.VITE_ATHENA_DEPLOYMENT_ID,
affiliate: process.env.VITE_ATHENA_AFFILIATE,
grpcAddress: process.env.VITE_ATHENA_GRPC_ADDRESS,
authentication: {
issuerUrl: process.env.VITE_OAUTH_ISSUER,
clientId: process.env.VITE_ATHENA_CLIENT_ID,
clientSecret: process.env.VITE_ATHENA_CLIENT_SECRET,
scope: 'manage:classify',
audience: parseAudience(process.env.VITE_ATHENA_AUDIENCE),
},
});

Expand All @@ -60,8 +64,8 @@ describe('ClassifierSdk Functional Tests', () => {
};

const response = await sdk.classifySingle(input);
expect(response.classifications).toBe(true);
expect(response.error).toBeNull();
expect(Array.isArray(response.classifications)).toBe(true);
expect(response.error).toBeUndefined();
}, 10000);
});

Expand All @@ -70,17 +74,19 @@ describe('ClassifierSdk Functional Tests', () => {
expect,
annotate,
}) => {
// This is a smoke test. You must have a running gRPC server at localhost:50051 for this to pass.
// This is a smoke test. The gRPC server address is taken from VITE_ATHENA_GRPC_ADDRESS,
// falling back to the SDK default if the variable is not set.
// You may want to mock the gRPC client for true unit testing.
const imagePath = __dirname + '/448x448.jpg';
const sdk = new ClassifierSdk({
deploymentId: process.env.VITE_ATHENA_DEPLOYMENT_ID,
affiliate: process.env.VITE_ATHENA_AFFILIATE,
grpcAddress: process.env.VITE_ATHENA_GRPC_ADDRESS,
authentication: {
issuerUrl: process.env.VITE_OAUTH_ISSUER,
clientId: process.env.VITE_ATHENA_CLIENT_ID,
clientSecret: process.env.VITE_ATHENA_CLIENT_SECRET,
scope: 'manage:classify',
audience: parseAudience(process.env.VITE_ATHENA_AUDIENCE),
},
});

Expand Down Expand Up @@ -172,11 +178,12 @@ describe('ClassifierSdk Functional Tests', () => {
const sdk = new ClassifierSdk({
deploymentId: process.env.VITE_ATHENA_DEPLOYMENT_ID,
affiliate: process.env.VITE_ATHENA_AFFILIATE,
grpcAddress: process.env.VITE_ATHENA_GRPC_ADDRESS,
authentication: {
issuerUrl: process.env.VITE_OAUTH_ISSUER,
clientId: process.env.VITE_ATHENA_CLIENT_ID,
clientSecret: process.env.VITE_ATHENA_CLIENT_SECRET,
scope: 'manage:classify',
audience: parseAudience(process.env.VITE_ATHENA_AUDIENCE),
},
});

Expand Down Expand Up @@ -251,17 +258,19 @@ describe('ClassifierSdk Functional Tests', () => {
expect,
annotate,
}) => {
// This is a smoke test. You must have a running gRPC server at localhost:50051 for this to pass.
// This is a smoke test. The gRPC server address is taken from VITE_ATHENA_GRPC_ADDRESS,
// falling back to the SDK default if the variable is not set.
// You may want to mock the gRPC client for true unit testing.
const imagePath = __dirname + '/448x448.jpg';
const sdk = new ClassifierSdk({
deploymentId: process.env.VITE_ATHENA_DEPLOYMENT_ID,
affiliate: process.env.VITE_ATHENA_AFFILIATE,
grpcAddress: process.env.VITE_ATHENA_GRPC_ADDRESS,
authentication: {
issuerUrl: process.env.VITE_OAUTH_ISSUER,
clientId: process.env.VITE_ATHENA_CLIENT_ID,
clientSecret: process.env.VITE_ATHENA_CLIENT_SECRET,
scope: 'manage:classify',
audience: parseAudience(process.env.VITE_ATHENA_AUDIENCE),
},
});

Expand Down
3 changes: 1 addition & 2 deletions __tests__/unit/authenticationManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const options: AuthenticationOptions = {
clientId: 'client-id',
clientSecret: 'client-secret',
issuerUrl: 'https://issuer.example.com',
scope: 'manage:classify',
};

describe('AuthenticationManager', () => {
Expand All @@ -46,7 +45,7 @@ describe('AuthenticationManager', () => {
);
expect(openidClient.clientCredentialsGrant).toHaveBeenCalledWith(
mockDiscovery,
{ audience: 'crisp-athena-live', scope: options.scope },
{ audience: 'crisp-athena-live' },
);
});

Expand Down
3 changes: 0 additions & 3 deletions __tests__/unit/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ describe('ClassifierSdk', () => {
issuerUrl: 'https://test-issuer.com',
clientId: 'test-client-id',
clientSecret: 'test-client-secret',
scope: 'manage:classify',
},
});
});
Expand All @@ -57,7 +56,6 @@ describe('ClassifierSdk', () => {
issuerUrl: 'https://test-issuer.com',
clientId: 'test-client-id',
clientSecret: 'test-client-secret',
scope: 'manage:classify',
},
});

Expand All @@ -74,7 +72,6 @@ describe('ClassifierSdk', () => {
issuerUrl: 'https://test-issuer.com',
clientId: 'test-client-id',
clientSecret: 'test-client-secret',
scope: 'manage:classify',
},
});

Expand Down
1 change: 0 additions & 1 deletion __tests__/unit/module-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ describe('Module Import Tests', () => {
issuerUrl: 'https://test-issuer.com',
clientId: 'test-client-id',
clientSecret: 'test-client-secret',
scope: 'manage:classify',
},
});

Expand Down
61 changes: 59 additions & 2 deletions samples/e2e-testcases/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,61 @@ function compareResults(
return differences;
}

/**
* Format structured SDK and runtime errors for readable CLI output.
*/
function formatError(error: unknown): string {
if (error instanceof Error) {
Comment thread
corpo-iwillspeak marked this conversation as resolved.
const details = error as Error & {
code?: unknown;
details?: unknown;
};

const parts = [details.message, details.details]
.filter((value): value is string => typeof value === 'string' && value.length > 0);

if (typeof details.code === 'number' || typeof details.code === 'string') {
parts.unshift(`code ${details.code}`);
}

if (parts.length > 1 || (parts.length === 1 && parts[0] !== error.message)) {
return parts.join(' - ');
}
return error.message;
}

if (typeof error === 'string') {
return error;
}

if (error && typeof error === 'object') {
const details = error as {
code?: unknown;
message?: unknown;
details?: unknown;
};

const parts = [details.message, details.details]
.filter((value): value is string => typeof value === 'string' && value.length > 0);

if (typeof details.code === 'number' || typeof details.code === 'string') {
parts.unshift(`code ${details.code}`);
}

if (parts.length > 0) {
return parts.join(' - ');
}

try {
return JSON.stringify(error);
} catch {
return String(error);
}
}

return String(error);
}

/**
* Main execution
*/
Expand Down Expand Up @@ -293,7 +348,9 @@ async function main(): Promise<void> {
});

if (response.error) {
console.error(` ✗ ${filename} - Classification error: ${response.error}`);
console.error(
` ✗ ${filename} - Classification error: ${formatError(response.error)}`,
);
results.push({ filename, passed: false, differences: [] });
continue;
}
Expand Down Expand Up @@ -323,7 +380,7 @@ async function main(): Promise<void> {
}
}
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
const message = formatError(error);
console.error(` ✗ ${filename} - Error: ${message}`);
results.push({ filename, passed: false, differences: [] });
}
Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
AuthenticationManager,
} from './authenticationManager.js';
import { computeHashesFromStream } from './hashing.js';
import type TypedEventEmitter from 'typed-emitter';

/**
* Options for the classifyImage method.
Expand Down Expand Up @@ -90,7 +89,7 @@ export const defaultGrpcAddress = 'trust-messages-global.crispthinking.com:443';
* @fires ClassifierSdk#close
* @fires ClassifierSdk#data
*/
export class ClassifierSdk extends (EventEmitter as new () => TypedEventEmitter<ClassifierEvents>) {
export class ClassifierSdk extends EventEmitter {
Comment thread
corpo-iwillspeak marked this conversation as resolved.
private grpcAddress: string;
private client: ClassifierServiceClient;
private classifierGrpcCall: grpc.ClientDuplexStream<
Expand Down Expand Up @@ -386,6 +385,13 @@ export class ClassifierSdk extends (EventEmitter as new () => TypedEventEmitter<
): this {
return super.off(event, listener);
}

public override emit<U extends keyof ClassifierEvents>(
event: U,
...args: Parameters<ClassifierEvents[U]>
): boolean {
return super.emit(event, ...args);
}
}

export * from './generated/athena/models.js';
Expand Down
Loading