Implement DDD refactoring phases 1-4 with domain layer and ubiquitous language
Phase 1-3: Domain Layer Foundation - Add value objects: IdentityId, PermissionId, RelayId, WalletId, Nickname, NostrKeyPair - Add rich domain entities: Identity, Permission, Relay with behavior - Add domain events: IdentityCreated, IdentityRenamed, IdentitySelected, etc. - Add repository interfaces for Identity, Permission, Relay - Add infrastructure layer with repository implementations - Add EncryptionService abstraction Phase 4: Ubiquitous Language Cleanup - Rename BrowserSyncData → EncryptedVault (encrypted vault storage) - Rename BrowserSessionData → VaultSession (decrypted session state) - Rename SignerMetaData → ExtensionSettings (extension configuration) - Rename Identity_ENCRYPTED → StoredIdentity (storage DTO) - Rename Identity_DECRYPTED → IdentityData (session DTO) - Similar renames for Permission, Relay, NwcConnection, CashuMint - Add backwards compatibility aliases with @deprecated markers Test Coverage - Add comprehensive tests for all value objects - Add tests for domain entities and their behavior - Add tests for domain events - Fix PermissionChecker to prioritize kind-specific rules over blanket rules - Fix pre-existing component test issues (IconButton, Pubkey) All 113 tests pass. Both Chrome and Firefox builds succeed. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
81
projects/common/src/lib/domain/events/domain-event.spec.ts
Normal file
81
projects/common/src/lib/domain/events/domain-event.spec.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { DomainEvent, AggregateRoot } from './domain-event';
|
||||
|
||||
// Concrete implementation for testing
|
||||
class TestEvent extends DomainEvent {
|
||||
readonly eventType = 'test.event';
|
||||
|
||||
constructor(readonly testData: string) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
class TestAggregate extends AggregateRoot {
|
||||
doSomething(data: string): void {
|
||||
this.addDomainEvent(new TestEvent(data));
|
||||
}
|
||||
}
|
||||
|
||||
describe('DomainEvent', () => {
|
||||
describe('base properties', () => {
|
||||
it('should have occurredAt timestamp', () => {
|
||||
const before = new Date();
|
||||
const event = new TestEvent('test');
|
||||
const after = new Date();
|
||||
|
||||
expect(event.occurredAt.getTime()).toBeGreaterThanOrEqual(before.getTime());
|
||||
expect(event.occurredAt.getTime()).toBeLessThanOrEqual(after.getTime());
|
||||
});
|
||||
|
||||
it('should have unique eventId', () => {
|
||||
const event1 = new TestEvent('test1');
|
||||
const event2 = new TestEvent('test2');
|
||||
|
||||
expect(event1.eventId).not.toEqual(event2.eventId);
|
||||
});
|
||||
|
||||
it('should have eventType from subclass', () => {
|
||||
const event = new TestEvent('test');
|
||||
|
||||
expect(event.eventType).toEqual('test.event');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('AggregateRoot', () => {
|
||||
describe('domain events', () => {
|
||||
it('should collect domain events', () => {
|
||||
const aggregate = new TestAggregate();
|
||||
|
||||
aggregate.doSomething('first');
|
||||
aggregate.doSomething('second');
|
||||
|
||||
const events = aggregate.pullDomainEvents();
|
||||
|
||||
expect(events.length).toBe(2);
|
||||
expect((events[0] as TestEvent).testData).toEqual('first');
|
||||
expect((events[1] as TestEvent).testData).toEqual('second');
|
||||
});
|
||||
|
||||
it('should clear events after pulling', () => {
|
||||
const aggregate = new TestAggregate();
|
||||
aggregate.doSomething('test');
|
||||
|
||||
aggregate.pullDomainEvents();
|
||||
const secondPull = aggregate.pullDomainEvents();
|
||||
|
||||
expect(secondPull.length).toBe(0);
|
||||
});
|
||||
|
||||
it('should preserve event order', () => {
|
||||
const aggregate = new TestAggregate();
|
||||
|
||||
aggregate.doSomething('1');
|
||||
aggregate.doSomething('2');
|
||||
aggregate.doSomething('3');
|
||||
|
||||
const events = aggregate.pullDomainEvents();
|
||||
|
||||
expect(events.map(e => (e as TestEvent).testData)).toEqual(['1', '2', '3']);
|
||||
});
|
||||
});
|
||||
});
|
||||
55
projects/common/src/lib/domain/events/domain-event.ts
Normal file
55
projects/common/src/lib/domain/events/domain-event.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Base class for all domain events.
|
||||
* Domain events capture significant occurrences in the domain that
|
||||
* domain experts care about.
|
||||
*/
|
||||
export abstract class DomainEvent {
|
||||
readonly occurredAt: Date;
|
||||
readonly eventId: string;
|
||||
|
||||
constructor() {
|
||||
this.occurredAt = new Date();
|
||||
this.eventId = crypto.randomUUID();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the event type identifier.
|
||||
* Used for event routing and serialization.
|
||||
*/
|
||||
abstract get eventType(): string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface for entities that can raise domain events.
|
||||
*/
|
||||
export interface EventRaiser {
|
||||
/**
|
||||
* Pull all pending domain events from the entity.
|
||||
* This clears the internal event list.
|
||||
*/
|
||||
pullDomainEvents(): DomainEvent[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Base class for aggregate roots that can raise domain events.
|
||||
*/
|
||||
export abstract class AggregateRoot implements EventRaiser {
|
||||
private _domainEvents: DomainEvent[] = [];
|
||||
|
||||
protected addDomainEvent(event: DomainEvent): void {
|
||||
this._domainEvents.push(event);
|
||||
}
|
||||
|
||||
pullDomainEvents(): DomainEvent[] {
|
||||
const events = [...this._domainEvents];
|
||||
this._domainEvents = [];
|
||||
return events;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if there are any pending domain events.
|
||||
*/
|
||||
hasPendingEvents(): boolean {
|
||||
return this._domainEvents.length > 0;
|
||||
}
|
||||
}
|
||||
110
projects/common/src/lib/domain/events/identity-events.spec.ts
Normal file
110
projects/common/src/lib/domain/events/identity-events.spec.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import {
|
||||
IdentityCreated,
|
||||
IdentityRenamed,
|
||||
IdentitySelected,
|
||||
IdentitySigned,
|
||||
IdentityDeleted,
|
||||
} from './identity-events';
|
||||
|
||||
describe('Identity Domain Events', () => {
|
||||
describe('IdentityCreated', () => {
|
||||
it('should store identity creation data', () => {
|
||||
const event = new IdentityCreated('id-123', 'pubkey-abc', 'Alice');
|
||||
|
||||
expect(event.identityId).toEqual('id-123');
|
||||
expect(event.publicKey).toEqual('pubkey-abc');
|
||||
expect(event.nickname).toEqual('Alice');
|
||||
});
|
||||
|
||||
it('should have correct event type', () => {
|
||||
const event = new IdentityCreated('id', 'pubkey', 'name');
|
||||
|
||||
expect(event.eventType).toEqual('identity.created');
|
||||
});
|
||||
|
||||
it('should have inherited base properties', () => {
|
||||
const event = new IdentityCreated('id', 'pubkey', 'name');
|
||||
|
||||
expect(event.eventId).toBeTruthy();
|
||||
expect(event.occurredAt).toBeInstanceOf(Date);
|
||||
});
|
||||
});
|
||||
|
||||
describe('IdentityRenamed', () => {
|
||||
it('should store rename data', () => {
|
||||
const event = new IdentityRenamed('id-123', 'OldName', 'NewName');
|
||||
|
||||
expect(event.identityId).toEqual('id-123');
|
||||
expect(event.oldNickname).toEqual('OldName');
|
||||
expect(event.newNickname).toEqual('NewName');
|
||||
});
|
||||
|
||||
it('should have correct event type', () => {
|
||||
const event = new IdentityRenamed('id', 'old', 'new');
|
||||
|
||||
expect(event.eventType).toEqual('identity.renamed');
|
||||
});
|
||||
});
|
||||
|
||||
describe('IdentitySelected', () => {
|
||||
it('should store selection data with previous identity', () => {
|
||||
const event = new IdentitySelected('id-new', 'id-old');
|
||||
|
||||
expect(event.identityId).toEqual('id-new');
|
||||
expect(event.previousIdentityId).toEqual('id-old');
|
||||
});
|
||||
|
||||
it('should handle null previous identity', () => {
|
||||
const event = new IdentitySelected('id-new', null);
|
||||
|
||||
expect(event.identityId).toEqual('id-new');
|
||||
expect(event.previousIdentityId).toBeNull();
|
||||
});
|
||||
|
||||
it('should have correct event type', () => {
|
||||
const event = new IdentitySelected('id', null);
|
||||
|
||||
expect(event.eventType).toEqual('identity.selected');
|
||||
});
|
||||
});
|
||||
|
||||
describe('IdentitySigned', () => {
|
||||
it('should store signing data', () => {
|
||||
const event = new IdentitySigned('id-123', 1, 'event-id-abc');
|
||||
|
||||
expect(event.identityId).toEqual('id-123');
|
||||
expect(event.eventKind).toBe(1);
|
||||
expect(event.signedEventId).toEqual('event-id-abc');
|
||||
});
|
||||
|
||||
it('should have correct event type', () => {
|
||||
const event = new IdentitySigned('id', 1, 'event-id');
|
||||
|
||||
expect(event.eventType).toEqual('identity.signed');
|
||||
});
|
||||
|
||||
it('should handle various event kinds', () => {
|
||||
const kindExamples = [0, 1, 3, 4, 7, 30023, 10002];
|
||||
|
||||
kindExamples.forEach(kind => {
|
||||
const event = new IdentitySigned('id', kind, 'event');
|
||||
expect(event.eventKind).toBe(kind);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('IdentityDeleted', () => {
|
||||
it('should store deletion data', () => {
|
||||
const event = new IdentityDeleted('id-123', 'pubkey-abc');
|
||||
|
||||
expect(event.identityId).toEqual('id-123');
|
||||
expect(event.publicKey).toEqual('pubkey-abc');
|
||||
});
|
||||
|
||||
it('should have correct event type', () => {
|
||||
const event = new IdentityDeleted('id', 'pubkey');
|
||||
|
||||
expect(event.eventType).toEqual('identity.deleted');
|
||||
});
|
||||
});
|
||||
});
|
||||
74
projects/common/src/lib/domain/events/identity-events.ts
Normal file
74
projects/common/src/lib/domain/events/identity-events.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { DomainEvent } from './domain-event';
|
||||
|
||||
/**
|
||||
* Event raised when a new identity is created.
|
||||
*/
|
||||
export class IdentityCreated extends DomainEvent {
|
||||
readonly eventType = 'identity.created';
|
||||
|
||||
constructor(
|
||||
readonly identityId: string,
|
||||
readonly publicKey: string,
|
||||
readonly nickname: string
|
||||
) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Event raised when an identity is renamed.
|
||||
*/
|
||||
export class IdentityRenamed extends DomainEvent {
|
||||
readonly eventType = 'identity.renamed';
|
||||
|
||||
constructor(
|
||||
readonly identityId: string,
|
||||
readonly oldNickname: string,
|
||||
readonly newNickname: string
|
||||
) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Event raised when an identity is selected (made active).
|
||||
*/
|
||||
export class IdentitySelected extends DomainEvent {
|
||||
readonly eventType = 'identity.selected';
|
||||
|
||||
constructor(
|
||||
readonly identityId: string,
|
||||
readonly previousIdentityId: string | null
|
||||
) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Event raised when an identity signs an event.
|
||||
*/
|
||||
export class IdentitySigned extends DomainEvent {
|
||||
readonly eventType = 'identity.signed';
|
||||
|
||||
constructor(
|
||||
readonly identityId: string,
|
||||
readonly eventKind: number,
|
||||
readonly signedEventId: string
|
||||
) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Event raised when an identity is deleted.
|
||||
*/
|
||||
export class IdentityDeleted extends DomainEvent {
|
||||
readonly eventType = 'identity.deleted';
|
||||
|
||||
constructor(
|
||||
readonly identityId: string,
|
||||
readonly publicKey: string
|
||||
) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
9
projects/common/src/lib/domain/events/index.ts
Normal file
9
projects/common/src/lib/domain/events/index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export { DomainEvent, AggregateRoot } from './domain-event';
|
||||
export type { EventRaiser } from './domain-event';
|
||||
export {
|
||||
IdentityCreated,
|
||||
IdentityRenamed,
|
||||
IdentitySelected,
|
||||
IdentitySigned,
|
||||
IdentityDeleted,
|
||||
} from './identity-events';
|
||||
Reference in New Issue
Block a user