Skip to content

Commit

Permalink
test (api, common, common-web): add further tests (bluesky-social#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j committed May 12, 2023
1 parent 69bb1ba commit 36f95b1
Show file tree
Hide file tree
Showing 6 changed files with 576 additions and 0 deletions.
63 changes: 63 additions & 0 deletions packages/api/tests/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,67 @@ describe('agent', () => {
expect(sessions.length).toEqual(1)
expect(sessions[0]?.accessJwt).toEqual(origAccessJwt)
})

describe('setPersistSessionHandler', () => {
it('sets persist session handler', async () => {
let originalHandlerCallCount = 0
let newHandlerCallCount = 0

const persistSession = () => {
originalHandlerCallCount++
}
const newPersistSession = () => {
newHandlerCallCount++
}

const agent = new AtpAgent({ service: server.url, persistSession })

await agent.createAccount({
handle: 'user7.test',
email: 'user7@test.com',
password: 'password',
})

expect(originalHandlerCallCount).toEqual(1)

agent.setPersistSessionHandler(newPersistSession)

await agent.createAccount({
handle: 'user8.test',
email: 'user8@test.com',
password: 'password',
})

expect(originalHandlerCallCount).toEqual(1)
expect(newHandlerCallCount).toEqual(1)
})
})

describe('createAccount', () => {
it('persists an empty session on failure', async () => {
const events: string[] = []
const sessions: (AtpSessionData | undefined)[] = []
const persistSession = (evt: AtpSessionEvent, sess?: AtpSessionData) => {
events.push(evt)
sessions.push(sess)
}

const agent = new AtpAgent({ service: server.url, persistSession })

await expect(
agent.createAccount({
handle: '',
email: '',
password: 'password',
}),
).rejects.toThrow()

expect(agent.hasSession).toEqual(false)
expect(agent.session).toEqual(undefined)
expect(events.length).toEqual(1)
expect(events[0]).toEqual('create-failed')
expect(sessions.length).toEqual(1)
expect(sessions[0]).toEqual(undefined)
})
})
})
63 changes: 63 additions & 0 deletions packages/api/tests/bsky-agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,67 @@ describe('agent', () => {
})
await expect(p).rejects.toThrow('Record/displayName must be a string')
})

describe('app', () => {
it('should retrieve the api app', () => {
const agent = new BskyAgent({ service: server.url })
expect(agent.app).toBe(agent.api.app)
})
})

describe('post', () => {
it('should throw if no session', async () => {
const agent = new BskyAgent({ service: server.url })
await expect(agent.post({ text: 'foo' })).rejects.toThrow('Not logged in')
})
})

describe('deletePost', () => {
it('should throw if no session', async () => {
const agent = new BskyAgent({ service: server.url })
await expect(agent.deletePost('foo')).rejects.toThrow('Not logged in')
})
})

describe('like', () => {
it('should throw if no session', async () => {
const agent = new BskyAgent({ service: server.url })
await expect(agent.like('foo', 'bar')).rejects.toThrow('Not logged in')
})
})

describe('deleteLike', () => {
it('should throw if no session', async () => {
const agent = new BskyAgent({ service: server.url })
await expect(agent.deleteLike('foo')).rejects.toThrow('Not logged in')
})
})

describe('repost', () => {
it('should throw if no session', async () => {
const agent = new BskyAgent({ service: server.url })
await expect(agent.repost('foo', 'bar')).rejects.toThrow('Not logged in')
})
})

describe('deleteRepost', () => {
it('should throw if no session', async () => {
const agent = new BskyAgent({ service: server.url })
await expect(agent.deleteRepost('foo')).rejects.toThrow('Not logged in')
})
})

describe('follow', () => {
it('should throw if no session', async () => {
const agent = new BskyAgent({ service: server.url })
await expect(agent.follow('foo')).rejects.toThrow('Not logged in')
})
})

describe('deleteFollow', () => {
it('should throw if no session', async () => {
const agent = new BskyAgent({ service: server.url })
await expect(agent.deleteFollow('foo')).rejects.toThrow('Not logged in')
})
})
})
89 changes: 89 additions & 0 deletions packages/common-web/tests/check.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { check } from '../src/index'
import { ZodError } from 'zod'

describe('check', () => {
describe('is', () => {
it('checks object against definition', () => {
const checkable: check.Checkable<boolean> = {
parse(obj) {
return Boolean(obj)
},
safeParse(obj) {
return {
success: true,
data: Boolean(obj),
}
},
}

expect(check.is(true, checkable)).toBe(true)
})

it('handles failed checks', () => {
const checkable: check.Checkable<boolean> = {
parse(obj) {
return Boolean(obj)
},
safeParse() {
return {
success: false,
error: new ZodError([]),
}
},
}

expect(check.is(true, checkable)).toBe(false)
})
})

describe('assure', () => {
it('returns value on success', () => {
const checkable: check.Checkable<boolean> = {
parse(obj) {
return Boolean(obj)
},
safeParse(obj) {
return {
success: true,
data: Boolean(obj),
}
},
}

expect(check.assure(checkable, true)).toEqual(true)
})

it('throws on failure', () => {
const err = new Error('foo')
const checkable: check.Checkable<boolean> = {
parse() {
throw err
},
safeParse() {
throw err
},
}

expect(() => check.assure(checkable, true)).toThrow(err)
})
})

describe('isObject', () => {
const falseTestValues: unknown[] = [null, undefined, 'foo', 123, true]

for (const obj of falseTestValues) {
it(`returns false for ${obj}`, () => {
expect(check.isObject(obj)).toBe(false)
})
}

it('returns true for objects', () => {
expect(check.isObject({})).toBe(true)
})

it('returns true for instances of classes', () => {
const obj = new (class {})()
expect(check.isObject(obj)).toBe(true)
})
})
})
118 changes: 118 additions & 0 deletions packages/common-web/tests/tid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,122 @@ describe('TIDs', () => {
expect(parsed.timestamp()).toEqual(tid.timestamp())
expect(parsed.clockid()).toEqual(tid.clockid())
})

it('throws if invalid tid passed', () => {
expect(() => new TID('')).toThrow('Poorly formatted TID: 0 length')
})

describe('nextStr', () => {
it('returns next tid as a string', () => {
const str = TID.nextStr()
expect(typeof str).toEqual('string')
expect(str.length).toEqual(13)
})
})

describe('newestFirst', () => {
it('sorts tids newest first', () => {
const oldest = TID.next()
const newest = TID.next()

const tids = [oldest, newest]

tids.sort(TID.newestFirst)

expect(tids).toEqual([newest, oldest])
})
})

describe('oldestFirst', () => {
it('sorts tids oldest first', () => {
const oldest = TID.next()
const newest = TID.next()

const tids = [newest, oldest]

tids.sort(TID.oldestFirst)

expect(tids).toEqual([oldest, newest])
})
})

describe('is', () => {
it('true for valid tids', () => {
const tid = TID.next()
const asStr = tid.toString()

expect(TID.is(asStr)).toBe(true)
})

it('false for invalid tids', () => {
expect(TID.is('')).toBe(false)
})
})

describe('equals', () => {
it('true when same tid', () => {
const tid = TID.next()
expect(tid.equals(tid)).toBe(true)
})

it('true when different instance, same tid', () => {
const tid0 = TID.next()
const tid1 = new TID(tid0.toString())

expect(tid0.equals(tid1)).toBe(true)
})

it('false when different tid', () => {
const tid0 = TID.next()
const tid1 = TID.next()

expect(tid0.equals(tid1)).toBe(false)
})
})

describe('newerThan', () => {
it('true for newer tid', () => {
const tid0 = TID.next()
const tid1 = TID.next()

expect(tid1.newerThan(tid0)).toBe(true)
})

it('false for older tid', () => {
const tid0 = TID.next()
const tid1 = TID.next()

expect(tid0.newerThan(tid1)).toBe(false)
})

it('false for identical tids', () => {
const tid0 = TID.next()
const tid1 = new TID(tid0.toString())

expect(tid0.newerThan(tid1)).toBe(false)
})
})

describe('olderThan', () => {
it('true for older tid', () => {
const tid0 = TID.next()
const tid1 = TID.next()

expect(tid0.olderThan(tid1)).toBe(true)
})

it('false for newer tid', () => {
const tid0 = TID.next()
const tid1 = TID.next()

expect(tid1.olderThan(tid0)).toBe(false)
})

it('false for identical tids', () => {
const tid0 = TID.next()
const tid1 = new TID(tid0.toString())

expect(tid0.olderThan(tid1)).toBe(false)
})
})
})
Loading

0 comments on commit 36f95b1

Please sign in to comment.