@synet/identity
Version:
Simple and secure identity management library for Verifiable Identity
213 lines (176 loc) โข 6.77 kB
text/typescript
/**
* Test Identity + Credential Learning Edge Cases
*
* This test covers edge cases and error scenarios to ensure
* robust behavior when learning from credential units.
*/
import { Identity } from '../src/identity';
import { Unit, createUnitSchema, type TeachingContract } from '@synet/unit';
import type { BaseCredentialSubject, Result } from '@synet/credential';
class EdgeCaseTestUnit extends Unit {
constructor() {
super(createUnitSchema({
id: 'edge-case-test',
version: '1.0.0'
}));
}
whoami(): string {
return 'EdgeCaseTestUnit - Testing edge cases';
}
capabilities(): string[] {
return this._getAllCapabilities();
}
help(): void {
console.log('EdgeCaseTestUnit - Testing edge cases and error scenarios');
}
teach(): TeachingContract {
return {
unitId: 'edge-case-test',
capabilities: {
test: () => 'test capability'
}
};
}
async testWithoutLearning() {
console.log('๐ Testing execution without learning...');
const subject: BaseCredentialSubject = {
holder: {
id: 'did:example:test',
name: 'Test'
}
};
try {
// Try to execute issueCredential without learning
const result = await this.execute(
'issueCredential',
subject,
'TestCredential',
'did:example:test'
);
console.log('โ Expected error but got result:', result);
return { success: false, error: 'Expected error but got result' };
} catch (error) {
console.log('โ
Got expected error:', error instanceof Error ? error.message : error);
return { success: true, error: error instanceof Error ? error.message : 'Unknown error' };
}
}
async testWithLearning(credentialUnit: any) {
console.log('๐ Testing execution with learning...');
// Learn from credential unit
const teachingContract = credentialUnit.teach();
this.learn([teachingContract]);
console.log('๐ Learned capabilities:', this.capabilities());
const subject: BaseCredentialSubject = {
holder: {
id: 'did:example:test',
name: 'Test'
}
};
try {
// Try to execute with learned capabilities
const result = await this.execute(
'credential.issueCredential',
subject,
'TestCredential',
'did:example:test'
);
console.log('โ
Execution succeeded with Result pattern');
if (result && typeof result === 'object' && 'isSuccess' in result) {
const resultObj = result as Result<any>;
console.log('๐ Is Success:', resultObj.isSuccess);
if (resultObj.isSuccess) {
console.log('๐ Credential issued successfully');
return { success: true, credential: resultObj.value };
} else {
console.log('โ Credential issue failed:', resultObj.errorMessage);
return { success: false, error: resultObj.errorMessage };
}
} else {
console.log('โ Result does not follow Result pattern');
return { success: false, error: 'Result does not follow Result pattern' };
}
} catch (error) {
console.log('โ Execution failed:', error);
return { success: false, error: error instanceof Error ? error.message : 'Unknown error' };
}
}
async testVerification(credentialUnit: any, credential: any) {
console.log('๐ Testing credential verification...');
try {
// Try to verify the credential
const result = await this.execute(
'credential.verifyCredential',
credential
);
console.log('โ
Verification execution succeeded');
if (result && typeof result === 'object' && 'isSuccess' in result) {
const resultObj = result as Result<any>;
console.log('๐ Verification Is Success:', resultObj.isSuccess);
if (resultObj.isSuccess) {
console.log('๐ Verification result:', resultObj.value);
return { success: true, verification: resultObj.value };
} else {
console.log('โ Verification failed:', resultObj.errorMessage);
return { success: false, error: resultObj.errorMessage };
}
} else {
console.log('โ Verification result does not follow Result pattern');
return { success: false, error: 'Result does not follow Result pattern' };
}
} catch (error) {
console.log('โ Verification execution failed:', error);
return { success: false, error: error instanceof Error ? error.message : 'Unknown error' };
}
}
}
async function testEdgeCases() {
console.log('๐งช Testing Identity + Credential Learning Edge Cases\n');
try {
// 1. Generate identity
console.log('1๏ธโฃ Generating identity...'); const identityResult = await Identity.generate('edge-case-test');
if (!identityResult.isSuccess) {
throw new Error(`Failed to generate identity: ${identityResult.errorMessage}`);
}
const identity = identityResult.value;
console.log('โ
Identity generated');
// 2. Create test unit
console.log('\n2๏ธโฃ Creating edge case test unit...');
const testUnit = new EdgeCaseTestUnit();
console.log('โ
Test unit created');
// 3. Test without learning
console.log('\n3๏ธโฃ Testing execution without learning...');
const withoutLearningResult = await testUnit.testWithoutLearning();
console.log('โ
Without learning test completed');
// 4. Test with learning
console.log('\n4๏ธโฃ Testing execution with learning...');
const withLearningResult = await testUnit.testWithLearning(identity.credential());
console.log('โ
With learning test completed');
// 5. Test verification if credential was issued
if (withLearningResult.success && withLearningResult.credential) {
console.log('\n5๏ธโฃ Testing verification...');
const verificationResult = await testUnit.testVerification(
identity.credential(),
withLearningResult.credential
);
console.log('โ
Verification test completed');
}
console.log('\n๐ All edge case tests completed!');
return { success: true };
} catch (error) {
console.error('\nโ Edge case test failed:', error);
return { success: false, error: error instanceof Error ? error.message : 'Unknown error' };
}
}
// Run the edge case tests
testEdgeCases()
.then(result => {
if (result.success) {
console.log('\nโ
All edge case tests passed!');
process.exit(0);
} else {
console.log('\nโ Edge case tests failed:', result.error);
}
})
.catch(error => {
console.error('\n๐ฅ Unexpected error:', error);
});