Skip to content

Commit

Permalink
test(cu): implement tests and fixes #98
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Oct 19, 2023
1 parent 6b537d9 commit bfaba47
Show file tree
Hide file tree
Showing 12 changed files with 509 additions and 263 deletions.
15 changes: 8 additions & 7 deletions servers/cu/src/domain/client/ao-su.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fromPromise, of } from 'hyper-async'
import { always, applySpec, compose, evolve, filter, isNotNil, last, map, path, pipe, pluck, prop, transduce } from 'ramda'

export const loadMessagesWith = ({ fetch, SU_URL, logger: _logger, pageSize }) => {
const logger = _logger.child('loadSequencedMessages')
const logger = _logger.child('ao-su:loadMessages')

/**
* Pad the block height portion of the sortKey to 12 characters
Expand Down Expand Up @@ -187,11 +187,12 @@ export const loadTimestampWith = ({ fetch, SU_URL }) => {

export const loadMessageMetaWith = ({ fetch, SU_URL }) => {
return async ({ messageTxId }) => {
// TODO: implement fetching from su endpoint (need endpoint from Vince)

return {
processId: 'process-123',
sortKey: '000012345667,3423523532,fasdfsdf87slfja3rfaflj'
}
// TODO: implement fetching from su endpoint (need endpoint and shape from Vince)
return fetch(`${SU_URL}/message/${messageTxId}`, { method: 'GET' })
.then(res => res.json())
.then(res => ({
processId: res.processId,
sortKey: res.sortKey
}))
}
}
34 changes: 34 additions & 0 deletions servers/cu/src/domain/client/ao-su.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable no-throw-literal */
import { describe, test } from 'node:test'
import assert from 'node:assert'

import { loadMessageMetaSchema } from '../dal.js'
import { loadMessageMetaWith } from './ao-su.js'

describe('ao-su', () => {
describe('loadMessageMetaWith', () => {
test('return the message meta', async () => {
const loadMessageMeta = loadMessageMetaSchema.implement(
loadMessageMetaWith({
fetch: async (url, options) => {
assert.equal(url, 'https://ao-su-1.onrender.com/message/message-tx-123')
assert.deepStrictEqual(options, { method: 'GET' })

return new Response(JSON.stringify({
processId: 'process-123',
sortKey: 'block-123,time-456,hash-789'
}))
},
SU_URL: 'https://ao-su-1.onrender.com'
})
)

const res = await loadMessageMeta({ messageTxId: 'message-tx-123' })

assert.deepStrictEqual(res, {
processId: 'process-123',
sortKey: 'block-123,time-456,hash-789'
})
})
})
})
13 changes: 10 additions & 3 deletions servers/cu/src/domain/client/gateway.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { describe, test } from 'node:test'
import * as assert from 'node:assert'

import { loadTransactionDataSchema, loadTransactionMetaSchema } from '../dal.js'
import { loadTransactionDataWith, loadTransactionMetaWith } from './gateway.js'
import { createLogger } from '../logger.js'
import { loadBlocksMetaSchema, loadTransactionDataSchema, loadTransactionMetaSchema } from '../dal.js'
import { loadBlocksMetaWith, loadTransactionDataWith, loadTransactionMetaWith } from './gateway.js'
import { prop, uniqBy } from 'ramda'

const GATEWAY_URL = globalThis.GATEWAY || 'https://arweave.net'
const PROCESS = 'zc24Wpv_i6NNCEdxeKt7dcNrqL5w0hrShtSCcFGGL24'
const logger = createLogger('ao-cu:readState')

describe('gateway', () => {
describe('loadTransactionMetaWith', () => {
Expand Down Expand Up @@ -80,8 +83,12 @@ describe('gateway', () => {
})

describe('loadBlocksMeta', () => {
test('load the block data', async () => {
test('load the block data across multiple pages', async () => {
const loadBlocksMeta = loadBlocksMetaSchema.implement(loadBlocksMetaWith({ fetch, GATEWAY_URL, pageSize: 10, logger }))

const res = await loadBlocksMeta({ min: 1276343, max: 1276343 + 50 })
assert.equal(res.length, 51)
assert.equal(res.length, uniqBy(prop('height'), res).length)
})
})

Expand Down
2 changes: 1 addition & 1 deletion servers/cu/src/domain/client/pouchdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export function saveEvaluationWith ({ pouchDb }) {
message: prop('message'),
output: prop('output'),
evaluatedAt: prop('evaluatedAt'),
type: z.literal('evaluation')
type: always('evaluation')
}))
/**
* Ensure the expected shape before writing to the db
Expand Down
144 changes: 142 additions & 2 deletions servers/cu/src/domain/client/pouchdb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
import { describe, test } from 'node:test'
import assert from 'node:assert'

import { findLatestEvaluationSchema, findProcessSchema, saveProcessSchema } from '../dal.js'
import { findEvaluationsSchema, findLatestEvaluationSchema, findProcessSchema, saveEvaluationSchema, saveProcessSchema } from '../dal.js'
import {
COLLATION_SEQUENCE_MAX_CHAR,
findEvaluationsWith,
findLatestEvaluationWith,
findProcessWith,
saveEvaluationWith,
saveProcessWith
} from './pouchdb.js'

Expand Down Expand Up @@ -231,6 +233,144 @@ describe('pouchdb', () => {
})

describe('saveEvaluation', () => {
// TODO: write tests
test('save the evaluation to pouchdb', async () => {
const evaluatedAt = new Date().toISOString()
const saveEvaluation = saveEvaluationSchema.implement(
saveEvaluationWith({
pouchDb: {
get: async () => undefined,
put: (doc) => {
assert.equal(doc._id, 'process-123,sortkey-890')
assert.equal(doc.sortKey, 'sortkey-890')
assert.equal(doc.parent, 'process-123')
assert.deepStrictEqual(doc.message, { target: 'process-456', owner: 'owner-123', tags: [] })
assert.deepStrictEqual(doc.output, { state: { foo: 'bar' } })
assert.equal(doc.evaluatedAt.toISOString(), evaluatedAt)
return Promise.resolve(true)
}
}
})
)

await saveEvaluation({
sortKey: 'sortkey-890',
processId: 'process-123',
message: { target: 'process-456', owner: 'owner-123', tags: [] },
output: { state: { foo: 'bar' } },
evaluatedAt
})
})

test('noop if the interaction already exists', async () => {
const saveEvaluation = saveEvaluationSchema.implement(
saveEvaluationWith({
pouchDb: {
get: async () => ({
_id: 'process-123,sortkey-890',
sortKey: 'sortkey-890',
parent: 'process-123',
message: { target: 'process-456', owner: 'owner-123', tags: [] },
output: { state: { foo: 'bar' } },
evaluatedAt: new Date()
}),
put: assert.fail
}
})
)

await saveEvaluation({
sortKey: 'sortkey-890',
processId: 'process-123',
message: { target: 'process-456', owner: 'owner-123', tags: [] },
output: { state: { foo: 'bar' } },
evaluatedAt: new Date()
})
})
})

describe('findEvaluations', () => {
test('return the list of all evaluations', async () => {
const evaluatedAt = new Date().toISOString()
const mockEval = {
_id: 'process-123,sortkey-890',
sortKey: 'sortkey-890',
parent: 'process-123',
message: { target: 'process-456', owner: 'owner-123', tags: [] },
output: { state: { foo: 'bar' } },
evaluatedAt,
type: 'evaluation'
}
const findEvaluations = findEvaluationsSchema.implement(
findEvaluationsWith({
pouchDb: {
find: async (op) => {
assert.deepStrictEqual(op, {
selector: {
_id: {
$gte: 'process-123,',
$lte: `process-123,${COLLATION_SEQUENCE_MAX_CHAR}`
}
},
sort: [{ _id: 'desc' }],
limit: Number.MAX_SAFE_INTEGER
})
return {
docs: [
mockEval,
mockEval
]
}
}
}
}))

const res = await findEvaluations({ processId: 'process-123' })

assert.equal(res.length, 2)
})

test("return the evaluations between 'from' and 'to'", async () => {
const evaluatedAt = new Date().toISOString()
const mockEval = {
_id: 'process-123,sortkey-890',
sortKey: 'sortkey-890',
parent: 'process-123',
message: { target: 'process-456', owner: 'owner-123', tags: [] },
output: { state: { foo: 'bar' } },
evaluatedAt,
type: 'evaluation'
}
const findEvaluations = findEvaluationsSchema.implement(
findEvaluationsWith({
pouchDb: {
find: async (op) => {
assert.deepStrictEqual(op, {
selector: {
_id: {
$gte: 'process-123,sortkey-123,',
$lte: `process-123,sortkey-456,${COLLATION_SEQUENCE_MAX_CHAR}`
}
},
sort: [{ _id: 'desc' }],
limit: Number.MAX_SAFE_INTEGER
})
return {
docs: [
mockEval,
mockEval
]
}
}
}
}))

const res = await findEvaluations({
processId: 'process-123',
from: 'sortkey-123',
to: 'sortkey-456'
})

assert.equal(res.length, 2)
})
})
})
2 changes: 1 addition & 1 deletion servers/cu/src/domain/dal.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const loadTransactionDataSchema = z.function()
.returns(z.promise(z.any()))

export const loadBlocksMetaSchema = z.function()
.args(z.object({ left: z.number(), right: z.number() }))
.args(z.object({ min: z.number(), max: z.number() }))
.returns(z.promise(
z.array(
rawBlockSchema.passthrough()
Expand Down
2 changes: 1 addition & 1 deletion servers/cu/src/domain/lib/evaluate.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-throw-literal */
import { describe, test } from 'node:test'

describe('evaluate', () => {
describe('TODO evaluate', () => {
test('evaluates the messages', async () => {
// TODO: implement tests
})
Expand Down
7 changes: 2 additions & 5 deletions servers/cu/src/domain/lib/gatherScheduledMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ const ctxSchema = z.object({
* @returns {GatherScheduledMessages}
*/
export function gatherScheduledMessagesWith (env) {
const logger = env.logger.child('loadMessageMeta')
env = { ...env, logger }

const findEvaluations = fromPromise(findEvaluationsSchema.implement(env.findEvaluations))

return (ctx) => {
Expand All @@ -45,8 +42,8 @@ export function gatherScheduledMessagesWith (env) {
* scheduled messages have the interval and idx appended to their "sortKey"
* and so can be distinguished by how many parts after splitting on ','
*/
filter(evaluation => evaluation.sortKey.split(',').length > 4),
map(pathOr([], 'output', 'result', 'messages'))
filter(evaluation => evaluation.sortKey.split(',').length > 3),
map(pathOr([], ['output', 'result', 'messages']))
),
(acc, messages) => {
acc.push.apply(acc, messages)
Expand Down
62 changes: 60 additions & 2 deletions servers/cu/src/domain/lib/gatherScheduledMessages.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,66 @@
/* eslint-disable no-throw-literal */
import { describe, test } from 'node:test'
import assert from 'node:assert'

import { gatherScheduledMessagesWith } from './gatherScheduledMessages.js'

describe('gatherScheduledMessages', () => {
test('flatten and append all messages within the range', async () => {
// TODO: implement tests
test('should filter down to only the scheduled messages and append to ctx', async () => {
const scheduledSortKey = 'block-123,time-456,hash-789,10_blocks1'
const notScheduledSortKey = 'block-123,time-456,hash-789'

const mockEval = {
processId: 'process-123',
message: { target: 'process-456', owner: 'owner-123', tags: [] },
evaluatedAt: new Date()
}
const gatherScheduledMessages = gatherScheduledMessagesWith({
findEvaluations: async ({ processId, from, to }) => {
assert.equal(processId, 'process-123')
assert.equal(from, 'block-122,time-456,hash-789')
assert.equal(to, 'block-124,time-456,hash-789')

return [
{
...mockEval,
sortKey: scheduledSortKey,
output: { state: { foo: 'bar' }, result: { messages: [{ foo: '1' }, { fizz: '2' }] } }
},
// Scheduled message, but no output.result.messages
{
sortKey: scheduledSortKey,
...mockEval,
output: { state: { foo: 'bar' } }
},
// Not a scheduled message
{
sortKey: notScheduledSortKey,
...mockEval,
output: { state: { foo: 'bar' }, result: { messages: [{ foo: '3' }, { fizz: '4' }] } }
},
{
...mockEval,
sortKey: scheduledSortKey,
output: { state: { foo: 'bar' }, result: { messages: [{ foo: '5' }] } }
}
]
}
})

const res = await gatherScheduledMessages({
processId: 'process-123',
from: 'block-122,time-456,hash-789',
to: 'block-124,time-456,hash-789'
}).toPromise()

assert.equal(res.processId, 'process-123')
assert.equal(res.from, 'block-122,time-456,hash-789')
assert.equal(res.to, 'block-124,time-456,hash-789')
assert(res.messages)
assert.equal(res.messages.length, 3)
const [one, two, five] = res.messages
assert.equal(one.foo, '1')
assert.equal(two.fizz, '2')
assert.equal(five.foo, '5')
})
})
Loading

0 comments on commit bfaba47

Please sign in to comment.