OverviewFilesCommitsIssuesDocsReleasesPeers

keet-daemon / test / identity-lookup-member.test.js

49 lines · javascript ref:
// identity-lookup-member.test.js — identity.lookupMember passes the MEMBER id
// (hyper-games #89 defect 8: "passes roomId where keet-core expects the member id").
//
// keet-core's lookupUserByMemberId takes ONE argument — the member key — and
// decodes it for the username registry (keet-core 4.22.28 lib/api/v1/index.js
// _lookupUserByMemberId(e): hypercoreIdEncoding.decode(e) -> userRegistry.lookupByKey).
// The router called it as (roomId, memberId), so keet-core looked up the ROOM key.
//
// No keet-core needed: the backend is a stub that records what it was called with.
//
// Run:  cd hyper-fleeky/packages/keet-daemon && bare test/identity-lookup-member.test.js

import fs from 'bare-fs'
import path from 'bare-path'
import os from 'bare-os'
import { Router } from '../lib/router.js'

let exitCode = 0
function ok (cond, msg) {
  if (cond) console.log('  ok   ' + msg)
  else { console.log('  FAIL ' + msg); exitCode = 1 }
}

const storage = path.join(os.tmpdir(), 'keet-daemon-test-lookup-' + Date.now())
fs.mkdirSync(storage, { recursive: true })

const calls = []
const backend = {
  api: {
    core: {
      lookupUserByMemberId: async (...args) => { calls.push(args); return { username: 'alice0' } }
    }
  }
}
const router = new Router(backend, null, { storage })

const res = await router._identityLookupMember({ roomId: 'ROOMKEY', memberId: 'MEMBERKEY' })
ok(calls.length === 1, 'keet-core lookupUserByMemberId called once')
ok(calls[0] && calls[0].length === 1 && calls[0][0] === 'MEMBERKEY',
  'with the MEMBER id as its only argument (got ' + JSON.stringify(calls[0]) + ')')
ok(res && res.username === 'alice0', 'and its result is returned')

let threw = false
try { await router._identityLookupMember({ roomId: 'ROOMKEY' }) } catch { threw = true }
ok(threw, 'a missing memberId is refused')

console.log(exitCode === 0 ? '\nPASS' : '\nFAIL')
Bare.exit(exitCode)