Skip to content

Instantly share code, notes, and snippets.

@csuwildcat
Created November 16, 2023 14:19
Show Gist options
  • Save csuwildcat/b0d8452a34f72b0b0cc2296401e1abad to your computer and use it in GitHub Desktop.
Save csuwildcat/b0d8452a34f72b0b0cc2296401e1abad to your computer and use it in GitHub Desktop.
import type { PortableDid } from '@web5/dids';
import type { ManagedIdentity } from '@web5/agent';
const fs = require('fs').promises;
import { expect } from 'chai';
import { TestManagedAgent } from '@web5/agent';
import { DwnApi } from '../src/dwn-api.js';
import { testDwnUrl } from './test-config.js';
import { TestUserAgent } from './utils/test-user-agent.js';
import binaryProtocolDefinition from './fixtures/protocol-definitions/binary.json' assert { type: 'json' };
let testDwnUrls: string[] = [testDwnUrl];
let imageBuffer: any;
describe.only('LocalDwn', () => {
let alice: ManagedIdentity;
let aliceDid: PortableDid;
let dwn: DwnApi;
let testAgent: TestManagedAgent;
before(async () => {
imageBuffer = await fs.readFile('./fixtures/bitcoin-logo.jpg');
testAgent = await TestManagedAgent.create({
agentClass : TestUserAgent,
agentStores : 'memory'
});
});
beforeEach(async () => {
await testAgent.clearStorage();
// Create an Agent DID.
await testAgent.createAgentDid();
// Create a new Identity to author the DWN messages.
({ did: aliceDid } = await testAgent.createIdentity({ testDwnUrls }));
alice = await testAgent.agent.identityManager.import({
did : aliceDid,
identity : { name: 'Alice', did: aliceDid.did },
kms : 'local'
});
// Instantiate DwnApi.
dwn = new DwnApi({ agent: testAgent.agent, connectedDid: aliceDid.did });
});
after(async () => {
await testAgent.clearStorage();
await testAgent.closeStorage();
});
describe('protocols.configure()', () => {
describe('agent', () => {
it('writes a protocol definition', async () => {
const response = await dwn.protocols.configure({
message: {
definition: binaryProtocolDefinition
}
});
expect(response.status.code).to.equal(202);
expect(response.status.detail).to.equal('Accepted');
});
});
});
describe('records.create()', () => {
describe('agent', () => {
it('creates a record with a large image', async () => {
const result = await dwn.records.create({
data : imageBuffer,
message : {
protocol: binaryProtocolDefinition.protocol,
protocolPath: 'image',
dataFormat : 'image/jpeg'
}
});
expect(result.status.code).to.equal(202);
expect(result.status.detail).to.equal('Accepted');
expect(result.record).to.exist;
expect(await result.record?.data).to.be.an('object');
});
});
});
it('needs tests', () => {
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment