Skip to content

Commit

Permalink
tests work
Browse files Browse the repository at this point in the history
  • Loading branch information
nichoth committed Apr 21, 2024
1 parent 17741cf commit dee0afa
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint": "eslint \"./**/*.{ts,js}\"",
"build-tests": "esbuild test/index.ts --target=es2020 --bundle --keep-names > test/test-bundle.js",
"test": "npm run lint && npm run build && npm run build-tests && npm run test-tape-run",
"test-tape-run": "cat test/index.html | tape-run --input=html --static=test | tap-spec",
"test-tape-run": "concurrently --kill-others \"npx partykit dev\" \"cat test/index.html | npx tape-run --input=html --static=test | npx tap-spec\"",
"build-cjs": "esbuild src/*.ts --format=cjs --keep-names --tsconfig=tsconfig.build.json --outdir=./dist --out-extension:.js=.cjs --sourcemap=inline",
"build-esm": "tsc --project tsconfig.build.json",
"build": "mkdir -p ./dist && rm -rf ./dist/* && npm run build-cjs && npm run build-esm",
Expand Down
20 changes: 19 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,21 @@ import {
createDeviceName
} from '@bicycle-codes/identity'
import type { DID, Crypto, Identity } from '@bicycle-codes/identity'
import { customAlphabet } from '@nichoth/nanoid'
import { numbers } from '@nichoth/nanoid-dictionary'
const debug = Debug()

/**
* Create a unique ID for the websocket connection. By default returns
* a 6 digit numeric code.
* @param {string} [alphabet] Custom alphabet, for example numbers
* @param {number} [length] integer for number of digits in the code
* @returns {ReturnType<typeof customAlphabet>}
*/
export function Code (alphabet?:string, length?:number):string {
return customAlphabet(alphabet || numbers, length ?? 6)()
}

/**
* Message from the new, incoming, device
*/
Expand Down Expand Up @@ -164,7 +177,10 @@ export async function Child (oddCrypto:Crypto, {
exchangeKey: toString(exchangeKey)
}))

return new Promise((resolve, reject) => {
const p = new Promise<{
identity:Identity,
certificate:Certificate
}>((resolve, reject) => {
/**
* We should only get 1 message,
* containing the new identity that includes this device,
Expand All @@ -183,4 +199,6 @@ export async function Child (oddCrypto:Crypto, {
party.close()
})
})

return p
}
68 changes: 64 additions & 4 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,67 @@
import { test } from '@bicycle-codes/tapzero'
import { example } from '../src/index.js'
import { Parent, Child, Certificate } from '../src/index.js'
import { create as createID } from '@bicycle-codes/identity'

test('example', async t => {
t.ok('ok', 'should be an example')
example()
const odd = globalThis.webnative
const createProgram = odd.program

const HOST_URL = 'localhost:1999'

let _certificate:Certificate|undefined

test('link 2 devices', async t => {
t.plan(5)

const alicesProgram = await createProgram({
namespace: {
name: 'link-tests',
creator: 'tests'
}
})
const { crypto: alicesCrypto } = alicesProgram.components
const alice = await createID(alicesCrypto, {
humanName: 'alice',
humanReadableDeviceName: 'phone'
})

const alicesComputersProgram = await createProgram({
namespace: {
name: 'link-tests',
creator: 'tests-device-2'
}
})

const { crypto: alicesComputersCrypto } = alicesComputersProgram.components
// parent must be called first
Parent(alice, alicesCrypto, {
host: HOST_URL,
code: '1234'
}).then(newId => {
t.ok(newId, 'parent gets a new identity')
})

const { identity: childsID, certificate } = await Child(
alicesComputersCrypto,
{
host: HOST_URL,
code: '1234',
humanReadableDeviceName: 'computer'
}
)

_certificate = certificate

t.ok(childsID, 'child gets a new identity')
t.ok(certificate, 'child gets a certificate')

t.equal(alice.username, childsID.username,
'Both IDs should have the same username')

t.ok(Object.keys(alice.devices).every(deviceName => {
return childsID.devices[deviceName]
}), 'devices should be equal in both IDs')
})

test('the certificate', async t => {
t.equal(_certificate?.recipient)
})

0 comments on commit dee0afa

Please sign in to comment.