Skip to content

Commit

Permalink
Fixing agent tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul1 committed May 15, 2024
1 parent 671d627 commit cfe057c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 60 deletions.
62 changes: 7 additions & 55 deletions packages/agent/src/app.test.ts
Expand Up @@ -8,9 +8,11 @@ import {
allOk,
createReference,
getReferenceString,
indexStructureDefinitionBundle,
sleep,
} from '@medplum/core';
import { Agent, Bot, Endpoint, Resource } from '@medplum/fhirtypes';
import { readJson } from '@medplum/definitions';
import { Agent, Bot, Bundle, Endpoint, Resource } from '@medplum/fhirtypes';
import { Hl7Client, Hl7Server } from '@medplum/hl7';
import { MockClient } from '@medplum/mock';
import { Client, Server } from 'mock-socket';
Expand All @@ -24,6 +26,10 @@ const medplum = new MockClient();

describe('App', () => {
beforeAll(async () => {
indexStructureDefinitionBundle(readJson('fhir/r4/profiles-types.json') as Bundle);
indexStructureDefinitionBundle(readJson('fhir/r4/profiles-resources.json') as Bundle);
indexStructureDefinitionBundle(readJson('fhir/r4/profiles-medplum.json') as Bundle);

console.log = jest.fn();
medplum.router.router.add('POST', ':resourceType/:id/$execute', async () => {
return [allOk, {} as Resource];
Expand Down Expand Up @@ -155,60 +161,6 @@ describe('App', () => {
mockServer2.stop();
});

test('Empty endpoint URL', async () => {
medplum.router.router.add('POST', ':resourceType/:id/$execute', async () => {
return [allOk, {} as Resource];
});

const bot = await medplum.createResource<Bot>({ resourceType: 'Bot' });

const endpoint = await medplum.createResource<Endpoint>({
resourceType: 'Endpoint',
status: 'active',
address: '', // invalid empty address
connectionType: { code: ContentType.HL7_V2 },
payloadType: [{ coding: [{ code: ContentType.HL7_V2 }] }],
});

const mockServer = new Server('wss://example.com/ws/agent');

mockServer.on('connection', (socket) => {
socket.on('message', (data) => {
const command = JSON.parse((data as Buffer).toString('utf8'));
if (command.type === 'agent:connect:request') {
socket.send(
Buffer.from(
JSON.stringify({
type: 'agent:connect:response',
})
)
);
}
});
});

const agent = await medplum.createResource<Agent>({
resourceType: 'Agent',
status: 'active',
name: 'Test Agent',
channel: [
{
name: 'test',
endpoint: createReference(endpoint),
targetReference: createReference(bot),
},
],
});

const app = new App(medplum, agent.id as string, LogLevel.INFO);
await expect(app.start()).rejects.toThrow(new Error("Invalid empty endpoint address for channel 'test'"));

await app.stop();
await new Promise<void>((resolve) => {
mockServer.stop(resolve);
});
});

test('Unknown endpoint protocol', async () => {
const originalConsoleLog = console.log;
const originalConsoleError = console.error;
Expand Down
14 changes: 12 additions & 2 deletions packages/agent/src/dicom.test.ts
@@ -1,5 +1,6 @@
import { LogLevel, allOk, createReference } from '@medplum/core';
import { Agent, Bot, Endpoint, Resource } from '@medplum/fhirtypes';
import { ContentType, LogLevel, allOk, createReference, indexStructureDefinitionBundle } from '@medplum/core';
import { readJson } from '@medplum/definitions';
import { Agent, Bot, Bundle, Endpoint, Resource } from '@medplum/fhirtypes';
import { MockClient } from '@medplum/mock';
import * as dimse from 'dcmjs-dimse';
import { Server } from 'mock-socket';
Expand All @@ -14,6 +15,10 @@ let endpoint: Endpoint;

describe('DICOM', () => {
beforeAll(async () => {
indexStructureDefinitionBundle(readJson('fhir/r4/profiles-types.json') as Bundle);
indexStructureDefinitionBundle(readJson('fhir/r4/profiles-resources.json') as Bundle);
indexStructureDefinitionBundle(readJson('fhir/r4/profiles-medplum.json') as Bundle);

console.log = jest.fn();
dimse.log.transports.forEach((t) => (t.silent = true));

Expand All @@ -25,7 +30,10 @@ describe('DICOM', () => {

endpoint = await medplum.createResource<Endpoint>({
resourceType: 'Endpoint',
status: 'active',
address: 'dicom://0.0.0.0:8104',
connectionType: { code: ContentType.DICOM },
payloadType: [{ coding: [{ code: ContentType.DICOM }] }],
} as Endpoint);
});

Expand All @@ -49,6 +57,8 @@ describe('DICOM', () => {

const agent = await medplum.createResource<Agent>({
resourceType: 'Agent',
name: 'Test Agent',
status: 'active',
channel: [
{
name: 'test',
Expand Down
16 changes: 14 additions & 2 deletions packages/agent/src/hl7.test.ts
@@ -1,5 +1,14 @@
import { allOk, ContentType, createReference, Hl7Message, LogLevel, sleep } from '@medplum/core';
import { Agent, Bot, Endpoint, Resource } from '@medplum/fhirtypes';
import {
ContentType,
Hl7Message,
LogLevel,
allOk,
createReference,
indexStructureDefinitionBundle,
sleep,
} from '@medplum/core';
import { readJson } from '@medplum/definitions';
import { Agent, Bot, Bundle, Endpoint, Resource } from '@medplum/fhirtypes';
import { Hl7Client, Hl7Server } from '@medplum/hl7';
import { MockClient } from '@medplum/mock';
import { Client, Server } from 'mock-socket';
Expand All @@ -13,6 +22,9 @@ let endpoint: Endpoint;

describe('HL7', () => {
beforeAll(async () => {
indexStructureDefinitionBundle(readJson('fhir/r4/profiles-types.json') as Bundle);
indexStructureDefinitionBundle(readJson('fhir/r4/profiles-resources.json') as Bundle);
indexStructureDefinitionBundle(readJson('fhir/r4/profiles-medplum.json') as Bundle);
console.log = jest.fn();

medplum.router.router.add('POST', ':resourceType/:id/$execute', async () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/agent/src/net-utils.test.ts
Expand Up @@ -5,10 +5,12 @@ import {
allOk,
ContentType,
generateId,
indexStructureDefinitionBundle,
LogLevel,
sleep,
} from '@medplum/core';
import { Agent, Resource } from '@medplum/fhirtypes';
import { readJson } from '@medplum/definitions';
import { Agent, Bundle, Resource } from '@medplum/fhirtypes';
import { MockClient } from '@medplum/mock';
import { Client, Server } from 'mock-socket';
import child_process, { ChildProcess } from 'node:child_process';
Expand All @@ -22,6 +24,10 @@ describe('Agent Net Utils', () => {
let originalLog: typeof console.log;

beforeAll(() => {
indexStructureDefinitionBundle(readJson('fhir/r4/profiles-types.json') as Bundle);
indexStructureDefinitionBundle(readJson('fhir/r4/profiles-resources.json') as Bundle);
indexStructureDefinitionBundle(readJson('fhir/r4/profiles-medplum.json') as Bundle);

originalLog = console.log;
console.log = jest.fn();
});
Expand Down

0 comments on commit cfe057c

Please sign in to comment.