Skip to content

Commit

Permalink
Adds correct checks after types has been fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrej-langr committed Dec 28, 2024
1 parent de089a5 commit 608cb85
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 23 deletions.
2 changes: 1 addition & 1 deletion demos/taco-chat/src/hooks/useTeam.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { type AlertInfo, type PeerState } from '../types.js'
import { assert } from '@localfirst/shared'
import { ConnectionManager } from 'ConnectionManager.js'
import { teamContext } from 'components/TeamProvider.js'
import { randomTeamName } from 'util/randomTeamName.js'
import { randomTeamName } from '../util/randomTeamName.js'

// TODO: make this an environment var
const relayUrls = ['ws://localhost:8080']
Expand Down
19 changes: 15 additions & 4 deletions packages/auth-provider-automerge-repo/src/AuthProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
import { EventEmitter } from '@herbcaudill/eventemitter42'
import * as Auth from '@localfirst/auth'
import { hash } from '@localfirst/crypto'
import { debug, memoize, pause } from '@localfirst/shared'
import { assert, debug, memoize, pause } from '@localfirst/shared'
import { type AbstractConnection } from './AbstractConnection.js'
import { AnonymousConnection } from './AnonymousConnection.js'
import { buildServerUrl } from './buildServerUrl.js'
Expand Down Expand Up @@ -211,7 +211,11 @@ export class AuthProvider extends EventEmitter<AuthProviderEvents> {
* Creates a team and registers it with all of our sync servers.
*/
public async createTeam(teamName: string) {
const team = await Auth.createTeam(teamName, {
if (!this.#user) {
throw new Error('Cannot create team as user is missing on AuthProvider')
}

const team = Auth.createTeam(teamName, {
device: this.#device,
user: this.#user,
})
Expand Down Expand Up @@ -661,6 +665,10 @@ export class AuthProvider extends EventEmitter<AuthProviderEvents> {

const savedShares = unpack(serializedState) as SerializedState

if (!this.#user) {
throw new Error('Cannot load state as user is missing on AuthProvider')
}

await Promise.all(
Object.values(savedShares).map(async share => {
if ('encryptedTeam' in share) {
Expand All @@ -672,9 +680,12 @@ export class AuthProvider extends EventEmitter<AuthProviderEvents> {
this.#device.keys.secretKey
) as Auth.KeysetWithSecrets

const context = { device: this.#device, user: this.#user }
// By this point it is defined
assert(this.#user)

const context: Auth.LocalContext = { device: this.#device, user: this.#user }

const team = await Auth.loadTeam(encryptedTeam, context, teamKeys)
const team = Auth.loadTeam(encryptedTeam, context, teamKeys)
return this.addTeam(team)
} else {
return this.joinPublicShare(share.shareId)
Expand Down
7 changes: 6 additions & 1 deletion packages/auth-provider-automerge-repo/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ export type AuthProviderEvents = {
* this is how we get the user's keys.) This event gives the application a chance to persist the
* team graph and the user's info.
*/
joined: (payload: { shareId: ShareId; peerId: PeerId; team: Auth.Team; user: Auth.User }) => void
joined: (payload: {
shareId: ShareId
peerId: PeerId
team: Auth.Team
user: Auth.UserWithSecrets
}) => void

/**
* We're connected to a peer and have been mutually authenticated.
Expand Down
7 changes: 6 additions & 1 deletion packages/auth/src/connection/getDeviceUserFromGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ export const getDeviceUserFromGraph = ({

const userKeyring = select.keyring(state, { type: USER, name: userId }, starterKeys)
const keys = getLatestGeneration(userKeyring)
const user = { userName, userId, keys }

if (!keys) {
throw new Error('Failed to get device user from graph as there are no keys for user keyring')
}

const user: UserWithSecrets = { userName, userId, keys }

return { user, userKeyring }
}
2 changes: 1 addition & 1 deletion packages/auth/src/connection/test/identity.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { challenge, prove, verify } from '../identity.js'
import { ADMIN_SCOPE, TEAM_SCOPE } from '../../team/index.js'
import { setup } from '../../util/testing/index.js'
import 'util/testing/expect/toBeValid.js'
import '../../util/testing/expect/toBeValid.js'
import { type KeyScope, KeyType, createKeyset, redactKeys } from '@localfirst/crdx'
import { describe, expect, it } from 'vitest'

Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/team/test/members.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ADMIN } from '../../role/index.js'
import { setup } from '../../util/testing/index.js'
import 'util/testing/expect/toLookLikeKeyset.js'
import '../../util/testing/expect/toLookLikeKeyset.js'
import { describe, expect, it } from 'vitest'

describe('Team', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/crdx/src/graph/test/append.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TEST_GRAPH_KEYS as keys, setup } from '../../util/testing/setup.js'
import { describe, expect, test } from 'vitest'
import { append, createGraph, getHead, getRoot } from '../index.js'
import { validate } from '../../validator/index.js'
import 'util/testing/expect/toBeValid'
import '../../util/testing/expect/toBeValid'

const { alice } = setup('alice')
const defaultUser = alice
Expand Down
2 changes: 1 addition & 1 deletion packages/crdx/src/graph/test/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TEST_GRAPH_KEYS as keys, setup } from '../../util/testing/setup.js'
import { describe, expect, test } from 'vitest'
import { createGraph, deserialize, getHead, getRoot, serialize } from '../index.js'
import { validate } from '../../validator/index.js'
import 'util/testing/expect/toBeValid.js'
import '../../util/testing/expect/toBeValid.js'

const { alice } = setup('alice')
const defaultUser = alice
Expand Down
2 changes: 1 addition & 1 deletion packages/crdx/src/graph/test/merge.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TEST_GRAPH_KEYS as keys, setup } from '../../util/testing/setup.js'
import { clone } from 'lodash-es'
import 'util/testing/expect/toBeValid'
import '../../util/testing/expect/toBeValid'
import { describe, expect, test } from 'vitest'
import { append, createGraph, merge } from '../index.js'

Expand Down
2 changes: 1 addition & 1 deletion packages/crdx/src/store/test/createStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { asymmetric } from '@localfirst/crypto'
import { createGraph, getRoot, serialize } from '../../graph/index.js'
import { createStore } from '../index.js'
import { createUser } from '../../user/index.js'
import 'util/testing/expect/toBeValid'
import '../../util/testing/expect/toBeValid'
import { TEST_GRAPH_KEYS as keys } from '../../util/testing/setup.js'
import { describe, expect, test } from 'vitest'
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/crdx/src/user/test/user.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { asymmetric, signatures, symmetric } from '@localfirst/crypto'
import { describe, expect, it } from 'vitest'
import 'util/testing/expect/toLookLikeKeyset'
import '../../util/testing/expect/toLookLikeKeyset'
import { createUser, redactUser } from '../index.js'

describe('user', () => {
Expand Down
18 changes: 9 additions & 9 deletions packages/crdx/src/validator/test/validate.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { asymmetric } from '@localfirst/crypto'
import { buildGraph } from 'util/testing/graph.js'
import { TEST_GRAPH_KEYS as keys, setup } from 'util/testing/setup.js'
import { buildGraph } from '../../util/testing/graph.js'
import { TEST_GRAPH_KEYS as keys, setup } from '../../util/testing/setup.js'
import { describe, expect, test, vitest } from 'vitest'
import { hashEncryptedLink } from 'graph/hashLink.js'
import { append, createGraph, getHead, getLink, getRoot } from 'graph/index.js'
import { type Hash } from 'util/index.js'
import { validate } from 'validator/validate.js'
import 'util/testing/expect/toBeValid'
import { hashEncryptedLink } from '../../graph/hashLink.js'
import { append, createGraph, getHead, getLink, getRoot } from '../../graph/index.js'
import { type Hash } from '../../util/index.js'
import { validate } from '../validate.js'
import '../../util/testing/expect/toBeValid'

const { setSystemTime } = vitest.useFakeTimers()

Expand Down Expand Up @@ -35,8 +35,8 @@ describe('graphs', () => {
┌─ e ─ g ─┐
┌─ c ─ d ─┤ ├─ o ─┐
a ─ b ─┤ └─── f ───┤ ├─ n
├──── h ──── i ─────┘ │
└───── j ─── k ── l ──────┘
├──── h ──── i ─────┘ │
└───── j ─── k ── l ──────┘
`)
expect(validate(graph)).toBeValid()
return graph
Expand Down

0 comments on commit 608cb85

Please sign in to comment.