Skip to content

Commit

Permalink
test: nostr service
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Nov 20, 2024
1 parent d3ceded commit 85ea696
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 23 deletions.
21 changes: 18 additions & 3 deletions apps/api/src/nostr/nostr.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import { Test, TestingModule } from '@nestjs/testing';
import { TestingModule } from '@nestjs/testing';
import { createTestingModuleWithValidation } from '@bitsacco/testing';

import { NostrController } from './nostr.controller';
import { NostrService } from './nostr.service';

describe('NostrController', () => {
let controller: NostrController;
let nostrService: NostrService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module: TestingModule = await createTestingModuleWithValidation({
controllers: [NostrController],
}).compile();
providers: [
{
provide: NostrService,
useValue: {
sendEncryptedNostrDm: jest.fn(),
configureNostrRelays: jest.fn(),
},
},
],
});

controller = module.get<NostrController>(NostrController);
nostrService = module.get<NostrService>(NostrService);
});

it('should be defined', () => {
expect(controller).toBeDefined();
expect(nostrService).toBeDefined();
});
});
9 changes: 2 additions & 7 deletions apps/api/src/nostr/nostr.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { ClientProxy } from '@nestjs/microservices';
import { ApiOperation, ApiBody } from '@nestjs/swagger';
import { Body, Controller, Inject, Logger, Post } from '@nestjs/common';
import { Body, Controller, Logger, Post } from '@nestjs/common';
import {
ConfigureNostrRelaysDto,
EVENTS_SERVICE_BUS,
SendEncryptedNostrDmDto,
} from '@bitsacco/common';
import { NostrService } from './nostr.service';
Expand All @@ -12,10 +10,7 @@ import { NostrService } from './nostr.service';
export class NostrController {
private readonly logger = new Logger(NostrController.name);

constructor(
private readonly nostrService: NostrService,
@Inject(EVENTS_SERVICE_BUS) private readonly eventsClient: ClientProxy,
) {
constructor(private readonly nostrService: NostrService) {
this.logger.log('NostrController initialized');
}

Expand Down
25 changes: 21 additions & 4 deletions apps/api/src/nostr/nostr.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
import { Test, TestingModule } from '@nestjs/testing';
import { TestingModule } from '@nestjs/testing';
import { ClientGrpc } from '@nestjs/microservices';
import { NostrServiceClient } from '@bitsacco/common';
import { NostrService } from './nostr.service';
import { createTestingModuleWithValidation } from '@bitsacco/testing';

describe('NostrService', () => {
let service: NostrService;
let serviceGenerator: ClientGrpc;
let mockNostrServiceClient: Partial<NostrServiceClient>;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [NostrService],
}).compile();
serviceGenerator = {
getService: jest.fn().mockReturnValue(mockNostrServiceClient),
getClientByServiceName: jest.fn().mockReturnValue(mockNostrServiceClient),
};

const module: TestingModule = await createTestingModuleWithValidation({
providers: [
{
provide: NostrService,
useFactory: () => {
return new NostrService(serviceGenerator);
},
},
],
});

service = module.get<NostrService>(NostrService);
});
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/swap/swap.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
SupportedCurrencies,
} from '@bitsacco/common';
import { createTestingModuleWithValidation } from '@bitsacco/testing';
import { ClientProxy } from '@nestjs/microservices';

import { SwapController } from './swap.controller';
import { SwapService } from './swap.service';
import { ClientProxy } from '@nestjs/microservices';

describe('SwapController', () => {
let controller: SwapController;
Expand Down
20 changes: 16 additions & 4 deletions apps/nostr/src/nostr.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import { Test, TestingModule } from '@nestjs/testing';
import { TestingModule } from '@nestjs/testing';
import { createTestingModuleWithValidation } from '@bitsacco/testing';
import { NostrController } from './nostr.controller';
import { NostrService } from './nostr.service';

describe('NostrController', () => {
let nostrController: NostrController;
let nostrService: NostrService;

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
const app: TestingModule = await createTestingModuleWithValidation({
imports: [],
controllers: [NostrController],
providers: [NostrService],
}).compile();
providers: [
{
provide: NostrService,
useValue: {
sendEncryptedDirectMessage: jest.fn(),
configureNostrRelays: jest.fn(),
},
},
],
});

nostrController = app.get<NostrController>(NostrController);
nostrService = app.get<NostrService>(NostrService);
});
});
32 changes: 28 additions & 4 deletions apps/nostr/src/nostr.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
import { Test, TestingModule } from '@nestjs/testing';
import { TestingModule } from '@nestjs/testing';
import { createTestingModuleWithValidation } from '@bitsacco/testing';
import { NostrService } from './nostr.service';
import { ConfigService } from '@nestjs/config';

describe('NostrService', () => {
let service: NostrService;
let mockCfg: jest.Mocked<ConfigService> = {
getOrThrow: jest.fn(),
} as any;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [NostrService],
}).compile();
mockCfg = {
getOrThrow: jest.fn().mockImplementation((key) => {
switch (key) {
case 'NOSTR_PUBLIC_KEY':
return 'c26253da9951d363833474e9145fa15b56876e532cf138251f9b59ea993de6a7';
case 'NOSTR_PRIVATE_KEY':
return 'nsec1yxpf8nqa7fq4vp8qkfwllpcdjkh3n2apsqhw4ee6hrn8yfg62d9sfegrqa';
default:
throw new Error('unknown config');
}
}),
} as any;

const module: TestingModule = await createTestingModuleWithValidation({
providers: [
{
provide: ConfigService,
useValue: mockCfg,
},
NostrService,
],
});

service = module.get<NostrService>(NostrService);
});
Expand Down

0 comments on commit 85ea696

Please sign in to comment.