From 6b090e89afca7362d0219f1a92a5f8272aa4c481 Mon Sep 17 00:00:00 2001 From: Ilya Khait Date: Wed, 25 Oct 2023 10:53:45 +0000 Subject: [PATCH] Implement repository & tests (WiP) --- .../application/AfoRegisterService.test.ts | 0 .../application/AfoRegisterService.ts | 0 src/afo-register/domain/Record.test.ts | 0 src/afo-register/domain/Record.ts | 67 +++++++++++++++++++ .../AfoRegisterRepository.test.ts | 34 ++++++++++ .../infrastructure/AfoRegisterRepository.ts | 17 +++++ src/fragmentarium/domain/fragment.ts | 4 ++ src/test-support/utils.ts | 5 +- 8 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 src/afo-register/application/AfoRegisterService.test.ts create mode 100644 src/afo-register/application/AfoRegisterService.ts create mode 100644 src/afo-register/domain/Record.test.ts create mode 100644 src/afo-register/domain/Record.ts create mode 100644 src/afo-register/infrastructure/AfoRegisterRepository.test.ts create mode 100644 src/afo-register/infrastructure/AfoRegisterRepository.ts diff --git a/src/afo-register/application/AfoRegisterService.test.ts b/src/afo-register/application/AfoRegisterService.test.ts new file mode 100644 index 000000000..e69de29bb diff --git a/src/afo-register/application/AfoRegisterService.ts b/src/afo-register/application/AfoRegisterService.ts new file mode 100644 index 000000000..e69de29bb diff --git a/src/afo-register/domain/Record.test.ts b/src/afo-register/domain/Record.test.ts new file mode 100644 index 000000000..e69de29bb diff --git a/src/afo-register/domain/Record.ts b/src/afo-register/domain/Record.ts new file mode 100644 index 000000000..888112f68 --- /dev/null +++ b/src/afo-register/domain/Record.ts @@ -0,0 +1,67 @@ +import { immerable } from 'immer' +import { Fragment } from 'fragmentarium/domain/fragment' + +interface Record { + readonly afoNumber: string + readonly page: string + readonly text: string + readonly textNumber: string + readonly linesDiscussed?: string + readonly discussedBy?: string + readonly discussedByNotes?: string +} + +export default class AfoRegisterRecord { + [immerable] = true + + readonly afoNumber: string + readonly page: string + readonly text: string + readonly textNumber: string + readonly linesDiscussed?: string + readonly discussedBy?: string + readonly discussedByNotes?: string + + constructor({ + afoNumber, + page, + text, + textNumber, + linesDiscussed, + discussedBy, + discussedByNotes, + }: Record) { + this.afoNumber = afoNumber + this.page = page + this.text = text + this.textNumber = textNumber + this.linesDiscussed = linesDiscussed + this.discussedBy = discussedBy + this.discussedByNotes = discussedByNotes + } + + toMarkdownString(fragments: Fragment[]): string { + let result = this.text + (this.textNumber ? ' ' + this.textNumber : '') + const linkToFragment = this.findLinkToFragment(fragments) + if (linkToFragment) result += `(${linkToFragment})` + if (this.linesDiscussed) result += ', ' + this.linesDiscussed + if (this.discussedBy) result += ': ' + this.discussedBy + if (this.discussedByNotes) result += ' ' + this.discussedByNotes + + result += `${ + this.afoNumber + this.page + }` + + result = result.replace(/\^([^^]+)\^/g, '$1') + return result + } + + findLinkToFragment(fragments: Fragment[]): string { + const reference = this.text + ' ' + this.textNumber + const matchingFragments = fragments.filter((fragment) => + fragment.traditionalReferences.includes(reference) + ) + if (matchingFragments.length === 0) return '' + return matchingFragments.map((fragment) => fragment.number).join(', ') + } +} diff --git a/src/afo-register/infrastructure/AfoRegisterRepository.test.ts b/src/afo-register/infrastructure/AfoRegisterRepository.test.ts new file mode 100644 index 000000000..3528eacd8 --- /dev/null +++ b/src/afo-register/infrastructure/AfoRegisterRepository.test.ts @@ -0,0 +1,34 @@ +import AfoRegisterRecord from 'afo-register/domain/Record' +import ApiClient from 'http/ApiClient' +import AfoRegisterRepository from './AfoRegisterRepository' +import Promise from 'bluebird' +import { testDelegation, TestData } from 'test-support/utils' + +jest.mock('afo-register/domain/Record') +jest.mock('http/ApiClient') + +const apiClient = new (ApiClient as jest.Mock>)() + +const afoRegisterRepository = new AfoRegisterRepository(apiClient) +const query = '{"afoNumber": "AfO 12", "page": "321"}' +const resultStub = { + afoNumber: 'AfO 12', + page: '321', + text: 'text', + textNumber: 'text number', +} as AfoRegisterRecord +const entry = new AfoRegisterRecord(resultStub) + +const testData: TestData[] = [ + new TestData( + 'search', + [query], + apiClient.fetchJson, + [entry], + [`/afo-register?${encodeURIComponent(query)}`, false], + Promise.resolve(entry) + ), +] + +describe('AfoRegisterRepository', () => + testDelegation(afoRegisterRepository, testData)) diff --git a/src/afo-register/infrastructure/AfoRegisterRepository.ts b/src/afo-register/infrastructure/AfoRegisterRepository.ts new file mode 100644 index 000000000..7e04e6413 --- /dev/null +++ b/src/afo-register/infrastructure/AfoRegisterRepository.ts @@ -0,0 +1,17 @@ +import AfoRegisterRecord from 'afo-register/domain/Record' +import Promise from 'bluebird' +import ApiClient from 'http/ApiClient' + +export default class BibliographyRepository { + private readonly apiClient: ApiClient + + constructor(apiClient: ApiClient) { + this.apiClient = apiClient + } + + search(query: string): Promise { + return this.apiClient + .fetchJson(`/afo-register?${encodeURIComponent(query)}`, false) + .then((result) => new AfoRegisterRecord(result)) + } +} diff --git a/src/fragmentarium/domain/fragment.ts b/src/fragmentarium/domain/fragment.ts index afdff261d..e7ac075f9 100644 --- a/src/fragmentarium/domain/fragment.ts +++ b/src/fragmentarium/domain/fragment.ts @@ -88,6 +88,7 @@ export class Fragment { readonly museum: Museum, readonly references: ReadonlyArray, readonly uncuratedReferences: ReadonlyArray | null, + readonly traditionalReferences: readonly string[], readonly atf: string, readonly hasPhoto: boolean, readonly genres: Genres, @@ -117,6 +118,7 @@ export class Fragment { museum, references, uncuratedReferences, + traditionalReferences, atf, hasPhoto, genres, @@ -144,6 +146,7 @@ export class Fragment { museum: Museum references: ReadonlyArray uncuratedReferences?: ReadonlyArray | null + traditionalReferences: readonly string[] atf: string hasPhoto: boolean genres: Genres @@ -172,6 +175,7 @@ export class Fragment { museum, references, uncuratedReferences ?? null, + traditionalReferences, atf, hasPhoto, genres, diff --git a/src/test-support/utils.ts b/src/test-support/utils.ts index c84593fa6..1a109cad9 100644 --- a/src/test-support/utils.ts +++ b/src/test-support/utils.ts @@ -162,7 +162,10 @@ export function testDelegation( it('Returns', async () => { if (result instanceof Bluebird || result instanceof Promise) { - await expect(result).resolves.toEqual(expectedResult) + const resolvedResult = await result + console.log('!!!', resolvedResult.afoNumber) + console.log('!11', expectedResult.afoNumber) + await expect(resolvedResult).toEqual(expectedResult) } else { expect(result).toEqual(expectedResult) }