From 519479282057624f5b5a05f99253d45f780bd497 Mon Sep 17 00:00:00 2001 From: Danny Browning Date: Thu, 16 Nov 2023 14:13:28 -0700 Subject: [PATCH] add tests and docs --- packages/cli/src/commands/composite/deploy.ts | 13 +++- packages/cli/test/composites.test.ts | 67 +++++++++++++++++-- website/docs/api/commands/cli.composite.md | 4 ++ 3 files changed, 75 insertions(+), 9 deletions(-) diff --git a/packages/cli/src/commands/composite/deploy.ts b/packages/cli/src/commands/composite/deploy.ts index c549442fc..840d5c5bd 100644 --- a/packages/cli/src/commands/composite/deploy.ts +++ b/packages/cli/src/commands/composite/deploy.ts @@ -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' @@ -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 { 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 { diff --git a/packages/cli/test/composites.test.ts b/packages/cli/test/composites.test.ts index f0596656e..132a2e83e 100644 --- a/packages/cli/test/composites.test.ts +++ b/packages/cli/test/composites.test.ts @@ -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 @@ -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' @@ -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) }) @@ -91,6 +118,7 @@ describe('composites', () => { return model }), ]) + return wasModelLoaded } @@ -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] @@ -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) }) diff --git a/website/docs/api/commands/cli.composite.md b/website/docs/api/commands/cli.composite.md index 9710a35b0..cf6d9e093 100644 --- a/website/docs/api/commands/cli.composite.md +++ b/website/docs/api/commands/cli.composite.md @@ -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` @@ -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`