Skip to content

Commit

Permalink
more exhaustive tests of delete + subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Sep 1, 2024
1 parent b2313ce commit f4394ed
Show file tree
Hide file tree
Showing 3 changed files with 469 additions and 156 deletions.
2 changes: 1 addition & 1 deletion packages/api/src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ export class Record implements RecordModel {
// RecordsWriteMessage properties.
this._attestation = options.attestation;
this._authorization = options.authorization;
this._contextId = options.contextId;
this._contextId = options.contextId ?? options.initialWrite?.contextId;
this._descriptor = options.descriptor;
this._encryption = options.encryption;
this._initialWrite = options.initialWrite;
Expand Down
71 changes: 61 additions & 10 deletions packages/api/tests/dwn-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,13 +833,28 @@ describe('DwnApi', () => {
});

describe('records.delete()', () => {
beforeEach(async() => {
// Configure the protocol on both DWNs
const { status: aliceProtocolStatus, protocol: aliceProtocol } = await dwnAlice.protocols.configure({ message: { definition: protocolDefinition } });
expect(aliceProtocolStatus.code).to.equal(202);
expect(aliceProtocol).to.exist;
const { status: aliceProtocolSendStatus } = await aliceProtocol.send(aliceDid.uri);
expect(aliceProtocolSendStatus.code).to.equal(202);
const { status: bobProtocolStatus, protocol: bobProtocol } = await dwnBob.protocols.configure({ message: { definition: protocolDefinition } });
expect(bobProtocolStatus.code).to.equal(202);
expect(bobProtocol).to.exist;
const { status: bobProtocolSendStatus } = await bobProtocol!.send(bobDid.uri);
expect(bobProtocolSendStatus.code).to.equal(202);
});

describe('agent', () => {
it('deletes a record', async () => {
const { status: writeStatus, record } = await dwnAlice.records.write({
data : 'Hello, world!',
message : {
schema : 'foo/bar',
dataFormat : 'text/plain'
protocol : protocolUri,
protocolPath : 'thread',
schema : protocolDefinition.types.thread.schema,
}
});

Expand All @@ -851,7 +866,8 @@ describe('DwnApi', () => {
expect(status.code).to.equal(202);

const deleteResult = await dwnAlice.records.delete({
message: {
protocol : protocolUri,
message : {
recordId: record!.id
}
});
Expand Down Expand Up @@ -950,12 +966,39 @@ describe('DwnApi', () => {

it('returns a 404 when the specified record does not exist', async () => {
let deleteResult = await dwnAlice.records.delete({
message: {
protocol : protocolUri,
message : {
recordId: 'abcd1234'
}
});
expect(deleteResult.status.code).to.equal(404);
});

it('stores a deleted record along with its initialWrite', async () => {
// Write a record but do not store it
const { status: initialWriteStatus, record: initialWriteRecord } = await dwnAlice.records.write({
store : false,
data : 'Hello, world!',
message : {
protocol : protocolUri,
protocolPath : 'thread',
schema : protocolDefinition.types.thread.schema,
}
});
expect(initialWriteStatus.code).to.equal(202);

// Delete the record without storing it
const { status: deleteStatus } = await initialWriteRecord.delete({ store: false });
expect(deleteStatus.code).to.equal(202);

// delete the record storing it
const { status: deleteStoreStatus } = await initialWriteRecord.delete();
expect(deleteStoreStatus.code).to.equal(202);

// try deleting it again
const { status: deleteStatus2 } = await initialWriteRecord.delete();
expect(deleteStatus2.code).to.equal(404);
});
});

describe('from: did', () => {
Expand Down Expand Up @@ -1808,12 +1851,20 @@ describe('DwnApi', () => {
it('subscribes to records from remote', async () => {
// configure a protocol
const protocolConfigure = await dwnAlice.protocols.configure({
message: { definition: { ...emailProtocolDefinition, published: true } }
message: { definition: { ...protocolDefinition, published: true } }
});
expect(protocolConfigure.status.code).to.equal(202);
const protocolSend = await protocolConfigure.protocol.send(aliceDid.uri);
expect(protocolSend.status.code).to.equal(202);

//configure the protocol on bob's DWN
const protocolConfigureBob = await dwnBob.protocols.configure({
message: { definition: { ...protocolDefinition, published: true } }
});
expect(protocolConfigureBob.status.code).to.equal(202);
const protocolSendBob = await protocolConfigureBob.protocol.send(bobDid.uri);
expect(protocolSendBob.status.code).to.equal(202);

// subscribe to all messages from the protocol
const records: Map<string, Record> = new Map();
const subscriptionHandler = async (record: Record) => {
Expand All @@ -1824,7 +1875,7 @@ describe('DwnApi', () => {
from : aliceDid.uri,
message : {
filter: {
protocol: emailProtocolDefinition.protocol
protocol: protocolUri,
}
},
subscriptionHandler
Expand All @@ -1836,9 +1887,9 @@ describe('DwnApi', () => {
data : 'Hello, world!',
message : {
recipient : bobDid.uri,
protocol : emailProtocolDefinition.protocol,
protocol : protocolUri,
protocolPath : 'thread',
schema : emailProtocolDefinition.types.thread.schema,
schema : protocolDefinition.types.thread.schema,
dataFormat : 'text/plain'
}
});
Expand Down Expand Up @@ -1872,9 +1923,9 @@ describe('DwnApi', () => {
data : 'Hello, world!',
message : {
recipient : bobDid.uri,
protocol : emailProtocolDefinition.protocol,
protocol : protocolUri,
protocolPath : 'thread',
schema : emailProtocolDefinition.types.thread.schema,
schema : protocolDefinition.types.thread.schema,
dataFormat : 'text/plain'
}
});
Expand Down
Loading

0 comments on commit f4394ed

Please sign in to comment.