Skip to content

Commit

Permalink
fix: missing migration script and exports (#1184)
Browse files Browse the repository at this point in the history
Signed-off-by: Ariel Gentile <[email protected]>
  • Loading branch information
genaris authored Dec 27, 2022
1 parent e299a03 commit 460510d
Show file tree
Hide file tree
Showing 14 changed files with 2,093 additions and 44 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type { ModulesMap, DefaultAgentModules, EmptyModuleMap } from './agent/Ag
export { EventEmitter } from './agent/EventEmitter'
export { FeatureRegistry } from './agent/FeatureRegistry'
export { MessageHandler, MessageHandlerInboundMessage } from './agent/MessageHandler'
export { MessageHandlerRegistry } from './agent/MessageHandlerRegistry'
export * from './agent/models'
export { AgentConfig } from './agent/AgentConfig'
export { AgentMessage } from './agent/AgentMessage'
Expand All @@ -31,6 +32,7 @@ export { InMemoryMessageRepository } from './storage/InMemoryMessageRepository'
export { Repository } from './storage/Repository'
export * from './storage/RepositoryEvents'
export { StorageService, Query } from './storage/StorageService'
export * from './storage/migration'
export { getDirFromFilePath } from './utils/path'
export { InjectionSymbols } from './constants'
export * from './wallet'
Expand Down
59 changes: 58 additions & 1 deletion packages/core/src/storage/migration/__tests__/0.2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const walletConfig = {
key: `Key: 0.2 Update`,
}

describe('UpdateAssistant | v0.2 - v0.3', () => {
describe('UpdateAssistant | v0.2 - v0.3.1', () => {
it(`should correctly update proof records and create didcomm records`, async () => {
// We need to mock the uuid generation to make sure we generate consistent uuids for the new records created.
let uuidCounter = 1
Expand Down Expand Up @@ -67,6 +67,11 @@ describe('UpdateAssistant | v0.2 - v0.3', () => {
toVersion: '0.3',
doUpdate: expect.any(Function),
},
{
fromVersion: '0.3',
toVersion: '0.3.1',
doUpdate: expect.any(Function),
},
])

await updateAssistant.update()
Expand Down Expand Up @@ -141,4 +146,56 @@ describe('UpdateAssistant | v0.2 - v0.3', () => {

uuidSpy.mockReset()
})

it(`should correctly update the did records`, async () => {
// We need to mock the uuid generation to make sure we generate consistent uuids for the new records created.
let uuidCounter = 1
const uuidSpy = jest.spyOn(uuid, 'uuid').mockImplementation(() => `${uuidCounter++}-4e4f-41d9-94c4-f49351b811f1`)

const aliceDidRecordsString = readFileSync(path.join(__dirname, '__fixtures__/alice-8-dids-0.2.json'), 'utf8')

const dependencyManager = new DependencyManager()
const storageService = new InMemoryStorageService()
dependencyManager.registerInstance(InjectionSymbols.StorageService, storageService)

const agent = new Agent(
{
config: {
label: 'Test Agent',
walletConfig,
autoUpdateStorageOnStartup: true,
},
dependencies: agentDependencies,
},
dependencyManager
)

const fileSystem = agent.injectionContainer.resolve<FileSystem>(InjectionSymbols.FileSystem)

// We need to manually initialize the wallet as we're using the in memory wallet service
// When we call agent.initialize() it will create the wallet and store the current framework
// version in the in memory storage service. We need to manually set the records between initializing
// the wallet and calling agent.initialize()
await agent.wallet.initialize(walletConfig)

// Set storage after initialization. This mimics as if this wallet
// is opened as an existing wallet instead of a new wallet
storageService.records = JSON.parse(aliceDidRecordsString)

await agent.initialize()

// MEDIATOR_ROUTING_RECORD recipientKeys will be different every time, and is not what we're testing here
delete storageService.records.MEDIATOR_ROUTING_RECORD

expect(storageService.records).toMatchSnapshot()

// Need to remove backupFiles after each run so we don't get IOErrors
const backupPath = `${fileSystem.basePath}/afj/migration/backup/${backupIdentifier}`
unlinkSync(backupPath)

await agent.shutdown()
await agent.wallet.delete()

uuidSpy.mockReset()
})
})
87 changes: 87 additions & 0 deletions packages/core/src/storage/migration/__tests__/0.3.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import type { FileSystem } from '../../FileSystem'

import { unlinkSync, readFileSync } from 'fs'
import path from 'path'

import { InMemoryStorageService } from '../../../../../../tests/InMemoryStorageService'
import { agentDependencies } from '../../../../tests/helpers'
import { Agent } from '../../../agent/Agent'
import { InjectionSymbols } from '../../../constants'
import { DependencyManager } from '../../../plugins'
import * as uuid from '../../../utils/uuid'
import { UpdateAssistant } from '../UpdateAssistant'

const backupDate = new Date('2022-01-21T22:50:20.522Z')
jest.useFakeTimers().setSystemTime(backupDate)
const backupIdentifier = backupDate.getTime()

const walletConfig = {
id: `Wallet: 0.3 Update`,
key: `Key: 0.3 Update`,
}

describe('UpdateAssistant | v0.3 - v0.3.1', () => {
it(`should correctly update the did records`, async () => {
// We need to mock the uuid generation to make sure we generate consistent uuids for the new records created.
let uuidCounter = 1
const uuidSpy = jest.spyOn(uuid, 'uuid').mockImplementation(() => `${uuidCounter++}-4e4f-41d9-94c4-f49351b811f1`)

const aliceDidRecordsString = readFileSync(path.join(__dirname, '__fixtures__/alice-8-dids-0.3.json'), 'utf8')

const dependencyManager = new DependencyManager()
const storageService = new InMemoryStorageService()
dependencyManager.registerInstance(InjectionSymbols.StorageService, storageService)

const agent = new Agent(
{
config: {
label: 'Test Agent',
walletConfig,
},
dependencies: agentDependencies,
},
dependencyManager
)

const fileSystem = agent.injectionContainer.resolve<FileSystem>(InjectionSymbols.FileSystem)

const updateAssistant = new UpdateAssistant(agent, {
v0_1ToV0_2: {
mediationRoleUpdateStrategy: 'doNotChange',
},
})

await updateAssistant.initialize()

// Set storage after initialization. This mimics as if this wallet
// is opened as an existing wallet instead of a new wallet
storageService.records = JSON.parse(aliceDidRecordsString)

expect(await updateAssistant.isUpToDate()).toBe(false)
expect(await updateAssistant.getNeededUpdates()).toEqual([
{
fromVersion: '0.3',
toVersion: '0.3.1',
doUpdate: expect.any(Function),
},
])

await updateAssistant.update()

expect(await updateAssistant.isUpToDate()).toBe(true)
expect(await updateAssistant.getNeededUpdates()).toEqual([])

// MEDIATOR_ROUTING_RECORD recipientKeys will be different every time, and is not what we're testing here
delete storageService.records.MEDIATOR_ROUTING_RECORD
expect(storageService.records).toMatchSnapshot()

// Need to remove backupFiles after each run so we don't get IOErrors
const backupPath = `${fileSystem.basePath}/afj/migration/backup/${backupIdentifier}`
unlinkSync(backupPath)

await agent.shutdown()
await agent.wallet.delete()

uuidSpy.mockReset()
})
})
Loading

0 comments on commit 460510d

Please sign in to comment.