chatgpt-api/src/chatgpt-api.test.ts

71 wiersze
1.7 KiB
TypeScript
Czysty Zwykły widok Historia

import test from 'ava'
import dotenv from 'dotenv-safe'
import { ChatGPTAPI } from './chatgpt-api'
dotenv.config()
const isCI = !!process.env.CI
test('ChatGPTAPI invalid session token', async (t) => {
2022-12-06 05:05:24 +00:00
t.timeout(30 * 1000) // 30 seconds
t.throws(() => new ChatGPTAPI({ sessionToken: null }), {
message: 'ChatGPT invalid session token'
})
await t.throwsAsync(
async () => {
const chatgpt = new ChatGPTAPI({ sessionToken: 'invalid' })
await chatgpt.ensureAuth()
},
{
message: 'ChatGPT failed to refresh auth token. Error: Unauthorized'
}
)
})
test('ChatGPTAPI valid session token', async (t) => {
if (!isCI) {
t.timeout(2 * 60 * 1000) // 2 minutes
}
t.notThrows(
() => new ChatGPTAPI({ sessionToken: 'fake valid session token' })
)
await t.notThrowsAsync(
(async () => {
const api = new ChatGPTAPI({ sessionToken: process.env.SESSION_TOKEN })
// Don't make any real API calls using our session token if we're running on CI
if (!isCI) {
await api.ensureAuth()
const response = await api.sendMessage('test')
console.log('chatgpt response', response)
t.truthy(response)
t.is(typeof response, 'string')
}
})()
)
})
2022-12-05 23:15:42 +00:00
if (!isCI) {
test('ChatGPTAPI expired session token', async (t) => {
2022-12-06 05:05:24 +00:00
t.timeout(30 * 1000) // 30 seconds
2022-12-05 23:15:42 +00:00
const expiredSessionToken = process.env.TEST_EXPIRED_SESSION_TOKEN
await t.throwsAsync(
async () => {
const chatgpt = new ChatGPTAPI({ sessionToken: expiredSessionToken })
await chatgpt.ensureAuth()
},
{
message:
'ChatGPT failed to refresh auth token. Error: session token may have expired'
2022-12-05 23:15:42 +00:00
}
)
})
}