Skip to content

Commit

Permalink
clear cache in tests, add check to process initial write within function
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Sep 2, 2024
1 parent 28b772a commit e9f709d
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 46 deletions.
1 change: 1 addition & 0 deletions packages/agent/tests/sync-engine-level.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ describe('SyncEngineLevel', () => {
await testHarness.dwnEventLog.clear();
await testHarness.dwnMessageStore.clear();
await testHarness.dwnResumableTaskStore.clear();
await testHarness.agent.permissions.clear();
testHarness.dwnStores.clear();
});

Expand Down
89 changes: 44 additions & 45 deletions packages/api/src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -811,10 +811,7 @@ export class Record implements RecordModel {
this._initialWrite = { ...this.rawMessage as DwnMessage[DwnInterface.RecordsWrite] };
}

// if there is an initial write and we haven't already processed it, we first process it and marked it as such.
if ((signAsOwner && !this._initialWriteSigned) || (store && !this._initialWriteStored)) {
await this.processInitialWrite({ store, signAsOwner });
}
await this.processInitialWriteIfNeeded({ store, signAsOwner });

// prepare delete options
let deleteOptions: ProcessDwnRequest<DwnInterface.RecordsDelete> = {
Expand Down Expand Up @@ -878,49 +875,54 @@ export class Record implements RecordModel {
return { status };
}

private async processInitialWrite({ store, signAsOwner }:{ store: boolean, signAsOwner: boolean }): Promise<void> {
const signAsOwnerValue = signAsOwner && this._delegateDid === undefined;
const signAsOwnerDelegate = signAsOwner && this._delegateDid !== undefined;

const initialWriteRequest: ProcessDwnRequest<DwnInterface.RecordsWrite> = {
messageType : DwnInterface.RecordsWrite,
rawMessage : this.initialWrite,
author : this._connectedDid,
target : this._connectedDid,
signAsOwner : signAsOwnerValue,
signAsOwnerDelegate,
store,
};

if (this._delegateDid) {
const { message: delegatedGrant } = await this._permissionsApi.getPermission({
connectedDid : this._connectedDid,
delegateDid : this._delegateDid,
protocol : this.protocol,
delegate : true,
cached : true,
messageType : initialWriteRequest.messageType
});
/**
* Process the initial write, if it hasn't already been processed, with the options set for storing and/or signing as the owner.
*/
private async processInitialWriteIfNeeded({ store, signAsOwner }:{ store: boolean, signAsOwner: boolean }): Promise<void> {
if (this.initialWrite && ((signAsOwner && !this._initialWriteSigned) || (store && !this._initialWriteStored))) {
const signAsOwnerValue = signAsOwner && this._delegateDid === undefined;
const signAsOwnerDelegate = signAsOwner && this._delegateDid !== undefined;

initialWriteRequest.messageParams = {
...initialWriteRequest.messageParams,
delegatedGrant
const initialWriteRequest: ProcessDwnRequest<DwnInterface.RecordsWrite> = {
messageType : DwnInterface.RecordsWrite,
rawMessage : this.initialWrite,
author : this._connectedDid,
target : this._connectedDid,
signAsOwner : signAsOwnerValue,
signAsOwnerDelegate,
store,
};

initialWriteRequest.granteeDid = this._delegateDid;
}
if (this._delegateDid) {
const { message: delegatedGrant } = await this._permissionsApi.getPermission({
connectedDid : this._connectedDid,
delegateDid : this._delegateDid,
protocol : this.protocol,
delegate : true,
cached : true,
messageType : initialWriteRequest.messageType
});

initialWriteRequest.messageParams = {
...initialWriteRequest.messageParams,
delegatedGrant
};

initialWriteRequest.granteeDid = this._delegateDid;
}

// Process the prepared initial write, with the options set for storing and/or signing as the owner.
const agentResponse = await this._agent.processDwnRequest(initialWriteRequest);
// Process the prepared initial write, with the options set for storing and/or signing as the owner.
const agentResponse = await this._agent.processDwnRequest(initialWriteRequest);

const { message, reply: { status } } = agentResponse;
const responseMessage = message;
const { message, reply: { status } } = agentResponse;
const responseMessage = message;

if (200 <= status.code && status.code <= 299) {
if (store) this._initialWriteStored = true;
if (signAsOwner) {
this._initialWriteSigned = true;
this.initialWrite.authorization = responseMessage.authorization;
if (200 <= status.code && status.code <= 299) {
if (store) this._initialWriteStored = true;
if (signAsOwner) {
this._initialWriteSigned = true;
this.initialWrite.authorization = responseMessage.authorization;
}
}
}
}
Expand All @@ -933,10 +935,7 @@ export class Record implements RecordModel {
const signAsOwnerValue = signAsOwner && this._delegateDid === undefined;
const signAsOwnerDelegate = signAsOwner && this._delegateDid !== undefined;

// if there is an initial write and we haven't already processed it, we first process it and marked it as such.
if (this.initialWrite && ((signAsOwner && !this._initialWriteSigned) || (store && !this._initialWriteStored))) {
await this.processInitialWrite({ store, signAsOwner });
}
await this.processInitialWriteIfNeeded({ store, signAsOwner });

let requestOptions: ProcessDwnRequest<DwnInterface.RecordsWrite | DwnInterface.RecordsDelete>;
// Now that we've processed a potential initial write, we can process the current record state.
Expand Down
1 change: 1 addition & 0 deletions packages/api/tests/dwn-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ describe('DwnApi', () => {
await delegateHarness.dwnEventLog.clear();
await delegateHarness.dwnMessageStore.clear();
await delegateHarness.dwnResumableTaskStore.clear();
await testHarness.agent.permissions.clear();
delegateHarness.dwnStores.clear();

// avoid seeing the security warning of no password during connect
Expand Down
1 change: 1 addition & 0 deletions packages/api/tests/permission-grant.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe('PermissionGrant', () => {
await testHarness.dwnEventLog.clear();
await testHarness.dwnMessageStore.clear();
await testHarness.dwnResumableTaskStore.clear();
await testHarness.agent.permissions.clear();
testHarness.dwnStores.clear();

// create a random protocol URI for each run
Expand Down
1 change: 1 addition & 0 deletions packages/api/tests/permission-request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('PermissionRequest', () => {
await testHarness.dwnEventLog.clear();
await testHarness.dwnMessageStore.clear();
await testHarness.dwnResumableTaskStore.clear();
await testHarness.agent.permissions.clear();
testHarness.dwnStores.clear();

// create a random protocol URI for each run
Expand Down
1 change: 1 addition & 0 deletions packages/api/tests/protocol.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('Protocol', () => {
await testHarness.dwnEventLog.clear();
await testHarness.dwnMessageStore.clear();
await testHarness.dwnResumableTaskStore.clear();
await testHarness.agent.permissions.clear();
testHarness.dwnStores.clear();
});

Expand Down
5 changes: 4 additions & 1 deletion packages/api/tests/record.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ describe('Record', () => {
await testHarness.dwnEventLog.clear();
await testHarness.dwnMessageStore.clear();
await testHarness.dwnResumableTaskStore.clear();
await testHarness.agent.permissions.clear();
testHarness.dwnStores.clear();

protocolDefinition = {
Expand All @@ -93,7 +94,7 @@ describe('Record', () => {
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);
const { status: bobProtocolSendStatus } = await bobProtocol.send(bobDid.uri);
expect(bobProtocolSendStatus.code).to.equal(202);
});

Expand Down Expand Up @@ -130,6 +131,7 @@ describe('Record', () => {
await delegateHarness.dwnEventLog.clear();
await delegateHarness.dwnMessageStore.clear();
await delegateHarness.dwnResumableTaskStore.clear();
await testHarness.agent.permissions.clear();
delegateHarness.dwnStores.clear();

// avoid seeing the security warning of no password during connect
Expand Down Expand Up @@ -1567,6 +1569,7 @@ describe('Record', () => {
await testHarnessCarol.dwnEventLog.clear();
await testHarnessCarol.dwnMessageStore.clear();
await testHarnessCarol.dwnResumableTaskStore.clear();
await testHarness.agent.permissions.clear();
testHarnessCarol.dwnStores.clear();

const { status: carolProtocolStatus, protocol: carolProtocol } = await dwnCarol.protocols.configure({ message: { definition: protocolDefinition } });
Expand Down

0 comments on commit e9f709d

Please sign in to comment.