-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
217 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
export { | ||
CreateDataEventParams, | ||
CreateInitEventParams, | ||
type CreateDataEventParams, | ||
type CreateInitEventParams, | ||
createDataEvent, | ||
createDataEventPayload, | ||
createInitEvent, | ||
getDeterministicInitEvent, | ||
} from './events.js' | ||
export type { UnknowContent } from './types.js' | ||
export type { UnknownContent } from './types.js' | ||
export { getPatchOperations } from './utils.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export type UnknowContent = Record<string, unknown> | ||
export type UnknownContent = Record<string, unknown> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { | ||
DocumentDataEventPayload, | ||
DocumentInitEventPayload, | ||
} from '@ceramic-sdk/document-protocol' | ||
import { assertSignedEvent, getSignedEventPayload } from '@ceramic-sdk/events' | ||
import { CommitID, randomCID, randomStreamID } from '@ceramic-sdk/identifiers' | ||
import { getAuthenticatedDID } from '@ceramic-sdk/key-did' | ||
|
||
import { | ||
createDataEvent, | ||
createInitEvent, | ||
getDeterministicInitEvent, | ||
} from '../src/index.js' | ||
|
||
const authenticatedDID = await getAuthenticatedDID(new Uint8Array(32)) | ||
|
||
describe('getDeterministicInitEvent()', () => { | ||
test('returns the deterministic event payload without unique value by default', () => { | ||
const model = randomStreamID() | ||
const event = getDeterministicInitEvent(model, 'did:key:123') | ||
expect(event.data).toBeNull() | ||
expect(event.header.controllers).toEqual(['did:key:123']) | ||
expect(event.header.model).toBe(model) | ||
expect(event.header.unique).toBeUndefined() | ||
}) | ||
|
||
test('returns the deterministic event payload with the provided unique value', () => { | ||
const model = randomStreamID() | ||
const unique = new Uint8Array([0, 1, 2]) | ||
const event = getDeterministicInitEvent(model, 'did:key:123', unique) | ||
expect(event.data).toBeNull() | ||
expect(event.header.controllers).toEqual(['did:key:123']) | ||
expect(event.header.model).toBe(model) | ||
expect(event.header.unique).toBe(unique) | ||
}) | ||
}) | ||
|
||
describe('createInitEvent()', () => { | ||
test('creates unique events by adding a random unique value', async () => { | ||
const model = randomStreamID() | ||
const event1 = await createInitEvent({ | ||
content: { hello: 'world' }, | ||
controller: authenticatedDID, | ||
model, | ||
}) | ||
assertSignedEvent(event1) | ||
|
||
const event2 = await createInitEvent({ | ||
content: { hello: 'world' }, | ||
controller: authenticatedDID, | ||
model, | ||
}) | ||
expect(event2).not.toEqual(event1) | ||
}) | ||
|
||
test('adds the context and shouldIndex when if provided', async () => { | ||
const model = randomStreamID() | ||
const event1 = await createInitEvent({ | ||
content: { hello: 'world' }, | ||
controller: authenticatedDID, | ||
model, | ||
}) | ||
const payload1 = await getSignedEventPayload( | ||
DocumentInitEventPayload, | ||
event1, | ||
) | ||
expect(payload1.header.context).toBeUndefined() | ||
expect(payload1.header.shouldIndex).toBeUndefined() | ||
|
||
const context = randomStreamID() | ||
const event2 = await createInitEvent({ | ||
content: { hello: 'world' }, | ||
controller: authenticatedDID, | ||
model, | ||
context, | ||
shouldIndex: true, | ||
}) | ||
const payload2 = await getSignedEventPayload( | ||
DocumentInitEventPayload, | ||
event2, | ||
) | ||
expect(payload2.header.context?.equals(context)).toBe(true) | ||
expect(payload2.header.shouldIndex).toBe(true) | ||
}) | ||
}) | ||
|
||
describe('createDataEvent()', () => { | ||
const commitID = CommitID.fromStream(randomStreamID(), randomCID()) | ||
|
||
test('creates the JSON patch payload', async () => { | ||
const event = await createDataEvent({ | ||
currentID: commitID, | ||
currentContent: { hello: 'test' }, | ||
content: { hello: 'world', test: true }, | ||
controller: authenticatedDID, | ||
}) | ||
const payload = await getSignedEventPayload(DocumentDataEventPayload, event) | ||
expect(payload.data).toEqual([ | ||
{ op: 'replace', path: '/hello', value: 'world' }, | ||
{ op: 'add', path: '/test', value: true }, | ||
]) | ||
expect(payload.header).toBeUndefined() | ||
}) | ||
|
||
test('adds the shouldIndex header when provided', async () => { | ||
const event = await createDataEvent({ | ||
currentID: commitID, | ||
content: { hello: 'world' }, | ||
controller: authenticatedDID, | ||
shouldIndex: true, | ||
}) | ||
const payload = await getSignedEventPayload(DocumentDataEventPayload, event) | ||
expect(payload.header).toEqual({ shouldIndex: true }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { randomStreamID } from '@ceramic-sdk/identifiers' | ||
import { equals } from 'uint8arrays' | ||
|
||
import { createInitHeader } from '../src/utils.js' | ||
|
||
describe('createInitHeader()', () => { | ||
test('adds random unique bytes by default or when explcitly set to false', () => { | ||
const controller = 'did:key:123' | ||
const model = randomStreamID() | ||
|
||
const header1 = createInitHeader({ controller, model }) | ||
expect(header1.unique).toBeInstanceOf(Uint8Array) | ||
const header2 = createInitHeader({ controller, model }) | ||
expect(header2.unique).toBeInstanceOf(Uint8Array) | ||
expect( | ||
equals(header1.unique as Uint8Array, header2.unique as Uint8Array), | ||
).toBe(false) | ||
|
||
const header3 = createInitHeader({ controller, model, unique: false }) | ||
expect(header3.unique).toBeInstanceOf(Uint8Array) | ||
expect( | ||
equals(header1.unique as Uint8Array, header3.unique as Uint8Array), | ||
).toBe(false) | ||
}) | ||
|
||
test('adds the specified unique bytes', () => { | ||
const controller = 'did:key:123' | ||
const model = randomStreamID() | ||
const unique = new Uint8Array([0, 1, 2]) | ||
|
||
const header1 = createInitHeader({ controller, model, unique }) | ||
expect(header1.unique).toBeInstanceOf(Uint8Array) | ||
const header2 = createInitHeader({ controller, model, unique }) | ||
expect(header2.unique).toBeInstanceOf(Uint8Array) | ||
|
||
expect( | ||
equals(header1.unique as Uint8Array, header2.unique as Uint8Array), | ||
).toBe(true) | ||
}) | ||
|
||
test('does not add unique bytes if set to true', () => { | ||
const controller = 'did:key:123' | ||
const model = randomStreamID() | ||
const header = createInitHeader({ controller, model, unique: true }) | ||
expect(header.unique).toBeUndefined() | ||
}) | ||
|
||
test('does not add context and shouldIndex by default', () => { | ||
const controller = 'did:key:123' | ||
const model = randomStreamID() | ||
const header = createInitHeader({ controller, model }) | ||
expect(header.context).toBeUndefined() | ||
expect(header.shouldIndex).toBeUndefined() | ||
}) | ||
|
||
test('adds context and shouldIndex if specified', () => { | ||
const controller = 'did:key:123' | ||
const model = randomStreamID() | ||
const context = randomStreamID() | ||
const header = createInitHeader({ | ||
controller, | ||
model, | ||
context, | ||
shouldIndex: true, | ||
}) | ||
expect(header.context?.equals(context)).toBe(true) | ||
expect(header.shouldIndex).toBe(true) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.