Skip to content

Commit

Permalink
Merge pull request #1153 from XYOracleNetwork/feature/sentinel-tests
Browse files Browse the repository at this point in the history
Image Thumbnail Sentinel Tests
  • Loading branch information
JoelBCarter authored Oct 4, 2023
2 parents 0433c52 + 258b57b commit 26ce95d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/modules/packages/sentinel/src/MemorySentinel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assertEx } from '@xylabs/assert'
import { fulfilled, rejected } from '@xylabs/promise'
import { handleError } from '@xyo-network/error'
import { AnyConfigSchema } from '@xyo-network/module'
import { Payload } from '@xyo-network/payload-model'
import {
SentinelConfig,
Expand All @@ -14,7 +15,7 @@ import { WitnessInstance } from '@xyo-network/witness'

import { AbstractSentinel } from './AbstractSentinel'

export type MemorySentinelParams<TConfig extends SentinelConfig = SentinelConfig> = SentinelParams<TConfig>
export type MemorySentinelParams<TConfig extends AnyConfigSchema<SentinelConfig> = AnyConfigSchema<SentinelConfig>> = SentinelParams<TConfig>

export class MemorySentinel<
TParams extends MemorySentinelParams = MemorySentinelParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
return this.createThumbnailDataUrl(imageBuffer)
}

// eslint-disable-next-line complexity
private async fromHttp(url: string, sourceUrl?: string): Promise<ImageThumbnail> {
let response: AxiosResponse
let dnsResult: string[]
Expand Down Expand Up @@ -254,8 +255,8 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
}

if (response.status >= 200 && response.status < 300) {
const contentType: string = response.headers['content-type']?.toString()
const [mediaType, fileType] = contentType.split('/')
const contentType: string | undefined = response.headers['content-type']?.toString()
const [mediaType, fileType] = contentType?.split('/') ?? ['', '']
result.mime = result.mime ?? {}
result.mime.returned = mediaType
const sourceBuffer = Buffer.from(response.data, 'binary')
Expand Down Expand Up @@ -308,7 +309,8 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
break
}
default: {
switch (result.mime.detected?.mime) {
const [detectedMediaType] = result.mime.detected?.mime?.split('/') ?? ['', '']
switch (detectedMediaType) {
case 'image': {
await processImage()
result.mime.type = result.mime.detected?.mime
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { HDWallet } from '@xyo-network/account'
import { isImageThumbnail } from '@xyo-network/image-thumbnail-payload-plugin'
import { MemoryNode } from '@xyo-network/node-memory'
import { PayloadBuilder } from '@xyo-network/payload-builder'
import { MemorySentinel } from '@xyo-network/sentinel'
import { UrlSchema } from '@xyo-network/url-payload-plugin'
import { isTimestamp, TimestampWitness } from '@xyo-network/witness-timestamp'
import { mock, MockProxy } from 'jest-mock-extended'

import { ImageThumbnailWitness } from '../Witness'

describe('Witness', () => {
describe('when behind sentinel', () => {
let thumbnailWitness: ImageThumbnailWitness
let timestampWitness: TimestampWitness
let sentinel: MemorySentinel
let node: MemoryNode
const logger = mock<Console>()

beforeAll(async () => {
thumbnailWitness = await ImageThumbnailWitness.create({
config: { schema: ImageThumbnailWitness.configSchema },
logger,
wallet: await HDWallet.random(),
})
timestampWitness = await TimestampWitness.create({
config: { schema: TimestampWitness.configSchema },
logger,
wallet: await HDWallet.random(),
})
sentinel = await MemorySentinel.create({
config: { schema: MemorySentinel.configSchema },
logger,
wallet: await HDWallet.random(),
})
const modules = [timestampWitness, thumbnailWitness, sentinel]
node = await MemoryNode.create({
config: { schema: MemoryNode.configSchema },
logger,
wallet: await HDWallet.random(),
})
await node.start()
await Promise.all(
modules.map(async (mod) => {
await node.register(mod)
await node.attach(mod.address, true)
}),
)
})
it('witnesses thumbnail for url', async () => {
// TODO: Replace with data url for speed
// const url =
// "data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='100' height='100' viewBox='0 0 100 100'><circle cx='50' cy='50' r='48' fill='yellow' stroke='black' stroke-width='2'/><circle cx='35' cy='35' r='5' fill='black'/><circle cx='65' cy='35' r='5' fill='black'/><path d='M 35 70 Q 50 85, 65 70' fill='none' stroke='black' stroke-width='2'/></svg>"
const url = 'https://placekitten.com/200/300'
const query = new PayloadBuilder({ schema: UrlSchema }).fields({ url }).build()
const values = await sentinel.report([query])
const timestamps = values.filter(isTimestamp)
expect(timestamps.length).toBe(1)
const thumbnails = values.filter(isImageThumbnail)
expect(thumbnails.length).toBe(1)
const thumbnail = thumbnails[0]
expect(thumbnail.sourceUrl).toBe(url)
})
})
})

0 comments on commit 26ce95d

Please sign in to comment.