From 545b675a12ecd1cb3eaf90be768f04840a18cb3c Mon Sep 17 00:00:00 2001 From: Nathaniel Cook Date: Thu, 20 Feb 2025 13:41:35 -0700 Subject: [PATCH] feat: update SDK now that c1 validates streams We needed to fix a few tests that created invalid streams. The set relation stream need to create a init event with empty content and then set the data in an update. The stream client test needs to use a real model definition. --- tests/c1-integration/src/index.ts | 2 +- .../test/model-mid-setType.test.ts | 6 +-- .../c1-integration/test/stream-client.test.ts | 38 ++++++++++++++----- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/tests/c1-integration/src/index.ts b/tests/c1-integration/src/index.ts index 6a07c5d..7ca5244 100644 --- a/tests/c1-integration/src/index.ts +++ b/tests/c1-integration/src/index.ts @@ -110,7 +110,7 @@ export async function waitForEventState( event_cid: CID, ) { await flightClient.preparedQuery( - 'SELECT "index" FROM event_states_feed WHERE event_cid = $event_cid LIMIT 1', + 'SELECT event_state_order FROM event_states_feed WHERE event_cid = $event_cid LIMIT 1', new Array(['$event_cid', event_cid.toString(base64.encoder)]), ) } diff --git a/tests/c1-integration/test/model-mid-setType.test.ts b/tests/c1-integration/test/model-mid-setType.test.ts index ef89f8d..6c0acca 100644 --- a/tests/c1-integration/test/model-mid-setType.test.ts +++ b/tests/c1-integration/test/model-mid-setType.test.ts @@ -91,7 +91,7 @@ describe('model integration test for list model and MID', () => { test('creates instance and obtains correct state', async () => { const documentStream = await modelInstanceClient.createInstance({ model: modelStream, - content: { test: 'hello' }, + content: null, shouldIndex: true, }) // Use the flightsql stream behavior to ensure the events states have been process before querying their states. @@ -100,12 +100,12 @@ describe('model integration test for list model and MID', () => { const currentState = await modelInstanceClient.getDocumentState( documentStream.baseID, ) - expect(currentState.content).toEqual({ test: 'hello' }) + expect(currentState.content).toBeNull() }) test('updates document and obtains correct state', async () => { const documentStream = await modelInstanceClient.createInstance({ model: modelStream, - content: { test: 'hello' }, + content: null, shouldIndex: true, }) // Use the flightsql stream behavior to ensure the events states have been process before querying their states. diff --git a/tests/c1-integration/test/stream-client.test.ts b/tests/c1-integration/test/stream-client.test.ts index 07ff547..6a4801e 100644 --- a/tests/c1-integration/test/stream-client.test.ts +++ b/tests/c1-integration/test/stream-client.test.ts @@ -6,6 +6,8 @@ import { } from '@ceramic-sdk/flight-sql-client' import { CeramicClient } from '@ceramic-sdk/http-client' import { StreamID } from '@ceramic-sdk/identifiers' +import { ModelClient } from '@ceramic-sdk/model-client' +import type { ModelDefinition } from '@ceramic-sdk/model-protocol' import { StreamClient } from '@ceramic-sdk/stream-client' import { asDIDString } from '@didtools/codecs' import { getAuthenticatedDID } from '@didtools/key-did' @@ -33,10 +35,21 @@ const OPTIONS: ClientOptions = { port: CONTAINER_OPTS.flightSqlPort, } -async function getClient(): Promise { - return createFlightSqlClient(OPTIONS) +const testModel: ModelDefinition = { + version: '2.0', + name: 'SingleTestModel', + description: 'Single Test model', + accountRelation: { type: 'single' }, + interface: false, + implements: [], + schema: { + type: 'object', + properties: { + test: { type: 'string', maxLength: 10 }, + }, + additionalProperties: false, + }, } - describe('stream client', () => { let c1Container: CeramicOneContainer const ceramicClient = new CeramicClient({ @@ -46,19 +59,24 @@ describe('stream client', () => { let cid: CID beforeAll(async () => { c1Container = await CeramicOneContainer.startContainer(CONTAINER_OPTS) + const client = new CeramicClient({ + url: `http://127.0.0.1:${CONTAINER_OPTS.apiPort}`, + }) // create a new event - const model = StreamID.fromString( - 'kjzl6hvfrbw6c5he7fxl3oakeckm2kchkqboqug08inkh1tmfqpd8v3oceriml2', - ) + const modelClient = new ModelClient({ + ceramic: client, + did: authenticatedDID, + }) + const model = await modelClient.createDefinition(testModel) const eventPayload: InitEventPayload = { data: { - body: 'This is a simple message', + test: 'test message', }, header: { controllers: [asDIDString(authenticatedDID.id)], model, - sep: 'test', + sep: 'model', }, } const encodedPayload = InitEventPayload.encode(eventPayload) @@ -66,8 +84,8 @@ describe('stream client', () => { cid = await ceramicClient.postEventType(SignedEvent, signedEvent) // obtain the stream ID by waiting for the event state to be populated - const client = await getClient() - const buffer = await client.query( + const flightClient = await createFlightSqlClient(OPTIONS) + const buffer = await flightClient.query( 'SELECT stream_cid FROM event_states_feed LIMIT 1', ) const data = tableFromIPC(buffer)