Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Le Cam committed Apr 16, 2024
1 parent ad6bbb6 commit 410a332
Show file tree
Hide file tree
Showing 16 changed files with 848 additions and 296 deletions.
1 change: 0 additions & 1 deletion composites/points/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export const SimplePointsAggregationID = definition.models.SimplePointsAggregati
export const SimplePointsAllocationID = definition.models.SimplePointsAllocation.id

export type PointsContent = {
issuer: string // DID
recipient: string // DID
points: number
}
2 changes: 1 addition & 1 deletion demo/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"uint8arrays": "^5.0.3"
},
"dependencies": {
"@ceramicnetwork/http-client": "^5.6.0",
"@ceramicnetwork/http-client": "^5.7.0",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
Expand Down
2 changes: 1 addition & 1 deletion demo/simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "demo",
"version": "1.0.0",
"dependencies": {
"@ceramicnetwork/http-client": "^5.6.0",
"@ceramic-solutions/did-utils": "workspace:^",
"@ceramic-solutions/points": "workspace:^",
"@ceramicnetwork/http-client": "^5.7.0",
"uint8arrays": "^5.0.3"
}
}
10 changes: 5 additions & 5 deletions libraries/points/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@
"prepublishOnly": "package-check"
},
"dependencies": {
"@ceramicnetwork/http-client": "^5.6.0",
"@composedb/loader": "^0.7.1",
"@ceramic-solutions/points-composite": "workspace:^",
"@ceramicnetwork/http-client": "^5.7.0",
"@composedb/loader": "^0.7.1",
"dids": "^5.0.2",
"key-did-provider-ed25519": "^4.0.2",
"key-did-resolver": "^4.0.0"
},
"devDependencies": {
"@ceramicnetwork/common": "^5.5.0",
"@composedb/types": "^0.7.1",
"@ceramic-solutions/ceramic-utils": "workspace:^",
"@ceramic-solutions/composite-utils": "workspace:^",
"@ceramic-solutions/did-utils": "workspace:^"
"@ceramic-solutions/did-utils": "workspace:^",
"@ceramicnetwork/common": "^5.6.0",
"@composedb/types": "^0.7.1"
},
"jest": {
"extensionsToTreatAsEsm": [
Expand Down
7 changes: 7 additions & 0 deletions libraries/points/src/ceramic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CeramicClient } from '@ceramicnetwork/http-client'
import type { CeramicAPI } from '@composedb/types'
import type { DID } from 'dids'

import { getAuthenticatedDID } from './did.js'

Expand All @@ -16,3 +17,9 @@ export async function getAuthenticatedCeramic(
ceramic.did = did
return ceramic
}

export function assertAuthenticated(did?: DID): asserts did {
if (!did?.authenticated) {
throw new Error(`An authenticated DID instance must be set on the Ceramic client`)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import { getCeramic } from './ceramic.js'
import { getQueryForRecipient, queryConnection } from './query.js'
import type { QueryDocumentsOptions, QueryDocumentsResult } from './types.js'

export type PointsBaseReaderParams = {
export type GenericReaderParams = {
issuer: string
modelID: string
ceramic?: CeramicAPI | string
loader?: DocumentLoader
}

export class PointsBaseReader<Content extends PointsContent = PointsContent> {
export class GenericReader<Content extends PointsContent = PointsContent> {
#baseQuery: BaseQuery
#issuer: string
#ceramic: CeramicAPI
#loader: DocumentLoader
#modelID: string

constructor(params: PointsBaseReaderParams) {
constructor(params: GenericReaderParams) {
const ceramic = getCeramic(params.ceramic)
this.#baseQuery = { account: params.issuer, models: [params.modelID] }
this.#modelID = params.modelID
Expand Down
6 changes: 5 additions & 1 deletion libraries/points/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
* @module points
*/

export { GenericReader, type GenericReaderParams } from './generic-reader.js'
export { ListWriter, type ListWriterFromSeedParams, type ListWriterParams } from './list-writer.js'
export { PointsReader, type PointsReaderParams } from './points-reader.js'
export {
type PointsWriterFromSeedParams,
PointsWriter,
type PointsWriterFromSeedParams,
type PointsWriterParams,
} from './points-writer.js'
export { SetReader, type SetReaderParams } from './set-reader.js'
export { SetWriter, type SetWriterFromSeedParams, type SetWriterParams } from './set-writer.js'
export type { QueryDocumentsOptions, QueryDocumentsResult } from './types.js'
30 changes: 17 additions & 13 deletions libraries/points/src/list-writer.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import type { DocumentLoader } from '@composedb/loader'
import type { CeramicAPI, ModelInstanceDocument } from '@composedb/types'
import type { PointsContent } from '@ceramic-solutions/points-composite'

import { getAuthenticatedCeramic } from './ceramic.js'
import { PointsBaseReader, type PointsBaseReaderParams } from './base-reader.js'
import { assertAuthenticated, getAuthenticatedCeramic } from './ceramic.js'
import { GenericReader } from './generic-reader.js'

export type PointsListWriterFromSeedParams = PointsBaseReaderParams & {
export type ListWriterFromSeedParams = {
ceramic?: CeramicAPI | string
loader?: DocumentLoader
modelID: string
seed: Uint8Array
}

export type PointsListWriterParams = Omit<PointsBaseReaderParams, 'ceramic'> & {
export type ListWriterParams = {
ceramic: CeramicAPI
loader?: DocumentLoader
modelID: string
}

export class PointsListWriter<
export class ListWriter<
Content extends PointsContent = PointsContent,
> extends PointsBaseReader<Content> {
> extends GenericReader<Content> {
static async fromSeed<Content extends PointsContent = PointsContent>(
params: PointsListWriterFromSeedParams,
): Promise<PointsListWriter<Content>> {
params: ListWriterFromSeedParams,
): Promise<ListWriter<Content>> {
const ceramic = await getAuthenticatedCeramic(params.seed, params.ceramic)
return new PointsListWriter({ ...params, ceramic })
return new ListWriter({ ...params, ceramic })
}

constructor(params: PointsListWriterParams) {
if (!params.ceramic.did?.authenticated) {
throw new Error(`An authenticated DID instance must be set on the Ceramic client`)
}
constructor(params: ListWriterParams) {
assertAuthenticated(params.ceramic.did)
super({ ...params, issuer: params.ceramic.did.id })
}

Expand Down
104 changes: 43 additions & 61 deletions libraries/points/src/points-reader.ts
Original file line number Diff line number Diff line change
@@ -1,116 +1,98 @@
import type { BaseQuery } from '@ceramicnetwork/common'
import { type DeterministicLoadOptions, DocumentLoader } from '@composedb/loader'
import type { CeramicAPI, ModelInstanceDocument } from '@composedb/types'
import {
type PointsContent,
SimplePointsAggregationID,
SimplePointsAllocationID,
} from '@ceramic-solutions/points-composite'

import { getCeramic } from './ceramic.js'
import { getQueryForRecipient, queryConnection } from './query.js'
import { GenericReader } from './generic-reader.js'
import { SetReader } from './set-reader.js'
import type { QueryDocumentsOptions, QueryDocumentsResult } from './types.js'

export function toUniqueArg(value: string | Array<string>): Array<string> {
return Array.isArray(value) ? value : [value]
}

export type MultiplePointsContent = {
recipient: string
points: number
}
export type AllocatePointsContent = PointsContent

export type TotalPointsContent = {
recipient: string
points: number
export type TotalPointsContent = PointsContent & {
date: string
}

export type PointsReaderParams = {
export type CreatePointsReaderParams = {
issuer: string
aggregationModelID?: string
allocationModelID?: string
ceramic?: CeramicAPI | string
loader?: DocumentLoader
}

export type PointsReaderParams<
AggregationContent extends TotalPointsContent = TotalPointsContent,
AllocationContent extends AllocatePointsContent = AllocatePointsContent,
> = {
aggregationReader: SetReader<AggregationContent>
allocationReader: GenericReader<AllocationContent>
}

export class PointsReader<
AggregationContent extends TotalPointsContent = TotalPointsContent,
AllocationContent extends MultiplePointsContent = MultiplePointsContent,
AllocationContent extends AllocatePointsContent = AllocatePointsContent,
> {
#aggregationBaseQuery: BaseQuery
#aggregationModelID: string
#allocationBaseQuery: BaseQuery
#allocationModelID: string
#issuer: string
#ceramic: CeramicAPI
#loader: DocumentLoader

constructor(params: PointsReaderParams) {
static create<
AggregationContent extends TotalPointsContent = TotalPointsContent,
AllocationContent extends AllocatePointsContent = AllocatePointsContent,
>(params: CreatePointsReaderParams): PointsReader<AggregationContent, AllocationContent> {
const ceramic = getCeramic(params.ceramic)
const aggregationModelID = params.aggregationModelID ?? SimplePointsAggregationID
const allocationModelID = params.allocationModelID ?? SimplePointsAllocationID

this.#aggregationBaseQuery = { account: params.issuer, models: [aggregationModelID] }
this.#aggregationModelID = aggregationModelID
this.#allocationBaseQuery = { account: params.issuer, models: [allocationModelID] }
this.#allocationModelID = allocationModelID
this.#ceramic = ceramic
this.#issuer = params.issuer
this.#loader = params.loader ?? new DocumentLoader({ ceramic })
}

get aggregationModelID(): string {
return this.#aggregationModelID
const aggregationReader = new SetReader<AggregationContent>({
ceramic,
issuer: params.issuer,
loader: params.loader,
modelID: params.aggregationModelID ?? SimplePointsAggregationID,
})
const allocationReader = new GenericReader<AllocationContent>({
ceramic,
issuer: params.issuer,
loader: params.loader,
modelID: params.allocationModelID ?? SimplePointsAllocationID,
})
return new PointsReader({ aggregationReader, allocationReader })
}

get allocationModelID(): string {
return this.#allocationModelID
}

get ceramic(): CeramicAPI {
return this.#ceramic
}
#aggregation: SetReader<AggregationContent>
#allocation: GenericReader<AllocationContent>

get loader(): DocumentLoader {
return this.#loader
constructor(params: PointsReaderParams<AggregationContent, AllocationContent>) {
this.#aggregation = params.aggregationReader
this.#allocation = params.allocationReader
}

async loadAggregationDocumentFor(
didOrValues: string | Array<string>,
options: DeterministicLoadOptions = {},
options?: DeterministicLoadOptions,
): Promise<ModelInstanceDocument<AggregationContent> | null> {
return await this.#loader.loadSet(
this.#issuer,
this.#aggregationModelID,
toUniqueArg(didOrValues),
{ ignoreEmpty: true, ...options },
)
return await this.#aggregation.loadDocumentFor(didOrValues, options)
}

async loadAggregationDocumentsFor(
did: string,
options?: QueryDocumentsOptions,
): Promise<QueryDocumentsResult<AggregationContent>> {
const query = getQueryForRecipient(this.#aggregationBaseQuery, did)
return await queryConnection(this.#loader, query, options)
return await this.#aggregation.queryDocumentsFor(did, options)
}

async getAggregationPointsFor(didOrValues: string | Array<string>): Promise<number> {
const doc = await this.loadAggregationDocumentFor(didOrValues)
return doc?.content?.points ?? 0
return await this.#aggregation.getPointsFor(didOrValues)
}

async queryAggregationDocuments(
options?: QueryDocumentsOptions,
): Promise<QueryDocumentsResult<AggregationContent>> {
return await queryConnection(this.#loader, this.#aggregationBaseQuery, options)
return await this.#aggregation.queryDocuments(options)
}

async queryAllocationDocumentsFor(
did: string,
options?: QueryDocumentsOptions,
): Promise<QueryDocumentsResult<AllocationContent>> {
const query = getQueryForRecipient(this.#allocationBaseQuery, did)
return await queryConnection(this.#loader, query, options)
return await this.#allocation.queryDocumentsFor(did, options)
}
}
Loading

0 comments on commit 410a332

Please sign in to comment.