Skip to content

Commit

Permalink
add tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dbcfd committed Nov 16, 2023
1 parent e023b55 commit 5194792
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
13 changes: 11 additions & 2 deletions packages/cli/src/commands/composite/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command, type CommandFlags } from '../../command.js'
import { Args } from '@oclif/core'
import {Args, Flags} from '@oclif/core'
import { readEncodedComposite } from '@composedb/devtools-node'
import { Composite } from '@composedb/devtools'
import { EncodedCompositeDefinition } from '@composedb/types'
Expand All @@ -17,13 +17,22 @@ export default class CompositeDeploy extends Command<
}),
}

static flags = {
...Command.flags,
index: Flags.boolean({
char: 'i',
description: 'Adds the composite to indexing on the ceramic node',
default: true,
}),
}

async run(): Promise<void> {
try {
this.spinner.start('Deploying the composite...')
let composite: Composite | undefined = undefined
if (this.stdin !== undefined) {
const definition = JSON.parse(this.stdin) as EncodedCompositeDefinition
composite = await Composite.fromJSON({ ceramic: this.ceramic, definition, index: true })
composite = await Composite.fromJSON({ ceramic: this.ceramic, definition, index: this.flags.index as boolean })
} else if (this.args.compositePath !== undefined) {
composite = await readEncodedComposite(this.ceramic, this.args.compositePath, true)
} else {
Expand Down
67 changes: 60 additions & 7 deletions packages/cli/test/composites.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import fs from 'fs-extra'
import stripAnsi from 'strip-ansi'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { TEST_OUTPUT_DIR_PATH } from '../globalConsts.js' // not a module
import { TEST_OUTPUT_DIR_PATH } from '../globalConsts.js'
import {StreamID} from "@ceramicnetwork/streamid";
import {done} from "@oclif/core/lib/cli-ux"; // not a module

const { readFile, readJSON } = fs

Expand All @@ -18,6 +20,11 @@ const MODEL1_JSON =
const MODEL2_JSON =
'{"version": "1.0","name":"Model2","accountRelation":{"type":"list"},"schema":{"$schema":"https://json-schema.org/draft/2020-12/schema","type":"object","properties":{"stringPropName":{"type":"string","maxLength":80}},"additionalProperties":false,"required":["stringPropName"]}}'

async function checkIfModelIndexed(ceramic: CeramicClient, streamId: string) {
const models = await ceramic.admin.getIndexedModels()
return models.includes(StreamID.fromString(streamId))
}

describe('composites', () => {
const seed = '3a6de55a5ef33d110a5a37438704b0f0cb77ca5977131775a70ffd1c23779c8c'

Expand Down Expand Up @@ -56,17 +63,37 @@ describe('composites', () => {
).toBe(true)
}, 60000)

test('composite creation succeeds', async () => {
test('composite creation succeeds but model is not deployed', async () => {
const ceramic = new CeramicClient()
const create = await execa('bin/run.js', [
'composite:create',
'test/mocks/composite.profile.post.schema',
`--did-private-key=${seed}`,
])
const output = create.stdout.toString()
const def: EncodedCompositeDefinition = JSON.parse(output)
expect(def.version).toBe('1.1')
expect(Object.keys(def.version).length).not.toBe(0)
expect(def.aliases).toBeNull()
expect(def.views).toBeNull()
await expect(checkIfModelIndexed(ceramic, Object.keys(def.models)[0])).toBeFalsy()
}, 60000)

test('composite creation succeeds and model is deployed', async () => {
const ceramic = new CeramicClient()
const create = await execa('bin/run.js', [
'composite:create',
'test/mocks/composite.profile.post.schema',
`--did-private-key=${seed}`,
'--deploy',
])
const output = create.stdout.toString()
expect(output.includes('"version":"1.1"')).toBe(true)
expect(output.includes('"indices":{"')).toBe(true)
expect(output.includes('"aliases":')).toBe(true)
expect(output.includes('"views":')).toBe(true)
const def: EncodedCompositeDefinition = JSON.parse(output)
expect(def.version).toBe('1.1')
expect(Object.keys(def.version).length).not.toBe(0)
expect(def.aliases).toBeNull()
expect(def.views).toBeNull()
await expect(checkIfModelIndexed(ceramic, Object.keys(def.models)[0])).toBeTruthy()
}, 60000)
})

Expand All @@ -91,6 +118,7 @@ describe('composites', () => {
return model
}),
])

return wasModelLoaded
}

Expand All @@ -105,7 +133,7 @@ describe('composites', () => {
).toBe(true)
}, 60000)

test('composite deployment succeeds', async () => {
test('composite deployment succeeds and is indexed by default', async () => {
const nonExistentModelStreamID = Object.keys(
(undeployedComposite as EncodedCompositeDefinition).models,
)[0]
Expand All @@ -125,6 +153,31 @@ describe('composites', () => {

const doesModelExistNow = await checkIfModelExist(ceramic, nonExistentModelStreamID)
expect(doesModelExistNow).toBeTruthy()
await expect(checkIfModelIndexed(ceramic, nonExistentModelStreamID)).toBeTruthy()
}, 60000)

test('composite deployment succeeds but is not indexed when no-index is specified', async () => {
const nonExistentModelStreamID = Object.keys(
(undeployedComposite as EncodedCompositeDefinition).models,
)[0]
const ceramic = new CeramicClient()
// The following check fails in CI as the same tests are running in parallel in different environments
// const doesModelExist = await checkIfModelExist(ceramic, nonExistentModelStreamID)
// expect(doesModelExist).toBeFalsy()
const deploy = await execa('bin/run.js', [
'composite:deploy',
'test/mocks/encoded.composite.undeployed.json',
`--did-private-key=${seed}`,
'--no-index',
])

expect(deploy.stderr.toString().includes(`Deploying the composite... Done!`)).toBe(true)

expect(deploy.stdout.toString().includes(nonExistentModelStreamID)).toBe(true)

const doesModelExistNow = await checkIfModelExist(ceramic, nonExistentModelStreamID)
expect(doesModelExistNow).toBeTruthy()
await expect(checkIfModelIndexed(ceramic, nonExistentModelStreamID)).toBeFalsy()
}, 60000)
})

Expand Down
4 changes: 4 additions & 0 deletions website/docs/api/commands/cli.composite.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ OPTIONS
-c, --ceramic-url Ceramic API URL
-k, --did-private-key DID Private Key (you can generate a fresh private key using composedb did:generate-private-key)
-o, --output a path to file where the resulting encoded composite definition should be saved
-d, --deploy Deploy the composite to the ceramic node, which will start indexing on the composite
```

### `composedb composite:models`
Expand Down Expand Up @@ -126,6 +127,9 @@ ARGUMENTS
OPTIONS
-c, --ceramic-url Ceramic API URL
-k, --did-private-key DID Private Key (you can generate a fresh private key using composedb did:generate-private-key)
-i, --index Adds the composite to indexing on the ceramic node (default)
--no-index Prevents the composite from being indexed on the ceramic node. Useful if composite has already been deployed and indexed
```

### `composedb composite:compile`
Expand Down

0 comments on commit 5194792

Please sign in to comment.