Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
flakey5 committed Mar 28, 2024
1 parent 3716f9b commit b2dc46d
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 80 deletions.
23 changes: 3 additions & 20 deletions tests/e2e/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
import { before, after, beforeEach, describe, it } from 'node:test'
import assert from 'node:assert'
import { buildServer } from '@platformatic/service'
import stackable, { setDontRegisterPlatformaticService } from '../../index'
import { FastifyInstance } from 'fastify'
import { MOCK_CONTENT_RESPONSE, buildExpectedStreamBodyString, mockMistralApi, mockOpenAiApi } from '../utils/mocks'
import { AiWarpConfig } from '../../config'
import { buildAiWarpApp } from '../utils/stackable'

const expectedStreamBody = buildExpectedStreamBodyString()

Expand Down Expand Up @@ -42,23 +42,7 @@ for (const { name, config } of providers) {

let app: FastifyInstance
before(async () => {
app = await buildServer({
server: {
port,
forceCloseConnections: true,
healthCheck: {
enabled: false
},
logger: {
level: 'error'
}
}
})

app.platformatic.config.aiProvider = config

setDontRegisterPlatformaticService(true)
app.register(stackable)
app = await buildAiWarpApp(port, config)

// @ts-expect-error
await app.start()
Expand Down Expand Up @@ -120,5 +104,4 @@ for (const { name, config } of providers) {
assert.strictEqual(body, expectedStreamBody)
})
})
i++
}
22 changes: 22 additions & 0 deletions tests/types/fastify.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ReadableStream } from 'node:stream/web'
import fastify, { FastifyInstance, FastifyRequest } from 'fastify';
import { expectAssignable } from 'tsd';
import '../../index.d'

const app = fastify()

expectAssignable<FastifyInstance['ai']['warp']>(async (_: FastifyRequest, _2: string) => '')

expectAssignable<FastifyInstance['ai']['warpStream']>(async (_: FastifyRequest, _2: string) => {
return new ReadableStream()
})

expectAssignable<FastifyInstance['ai']['preResponseCallback']>((request: FastifyRequest, response: string) => '')

expectAssignable<FastifyInstance['ai']['preResponseCallback']>(async (request: FastifyRequest, response: string) => '')

expectAssignable<FastifyInstance['ai']['preResponseChunkCallback']>((request: FastifyRequest, response: string) => '')

expectAssignable<FastifyInstance['ai']['preResponseChunkCallback']>(async (request: FastifyRequest, response: string) => '')

expectAssignable<FastifyInstance['ai']['rateLimiting']['max']>((request: FastifyRequest, key: string) => 0)
2 changes: 1 addition & 1 deletion tests/unit/ai-providers.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
import { beforeEach, describe, it } from 'node:test'
import assert from 'node:assert'
import { MistralProvider } from '../../ai-providers/mistral'
Expand All @@ -15,7 +16,6 @@ const providers: AiProvider[] = [
for (const provider of providers) {
describe(provider.constructor.name, () => {
beforeEach(() => {
// TODO: why do we need to mock these for every single test?
mockOpenAiApi()
mockMistralApi()
})
Expand Down
114 changes: 71 additions & 43 deletions tests/unit/generator.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
import { describe, it, afterEach } from 'node:test'
import assert from 'node:assert'
import { existsSync } from 'node:fs'
Expand All @@ -6,10 +7,23 @@ import { join } from 'node:path'
import AiWarpGenerator from '../../lib/generator'
import { generateGlobalTypesFile } from '../../lib/templates/types'
import { generatePluginWithTypesSupport } from '@platformatic/generators/lib/create-plugin'
import { getTempDir } from '../utils/generators'

const tempDirBase = join(__dirname, 'tmp')

let counter = 0
export async function getTempDir (baseDir: string): Promise<string> {
if (baseDir === undefined) {
baseDir = __dirname
}
const dir = join(baseDir, `platformatic-generators-${process.pid}-${Date.now()}-${counter++}`)
try {
await mkdir(dir, { recursive: true })
} catch (err) {
// do nothing
}
return dir
}

describe('AiWarpGenerator', () => {
afterEach(async () => {
try {
Expand Down Expand Up @@ -92,46 +106,60 @@ describe('AiWarpGenerator', () => {
})
})

// TODO: uncomment when https://github.com/platformatic/ai-warp/pull/9 lands
// it('generates plugin correctly', async () => {
// const dir = await getTempDir(tempDirBase)
// const pluginsDirectory = join(dir, 'plugins')

// const generator = new AiWarpGenerator()
// generator.setConfig({
// targetDirectory: dir,
// aiWarpPackageJsonPath: join(__dirname, '..', '..', 'package.json'),
// aiProvider: 'openai',
// aiModel: 'gpt-3.5-turbo'
// })

// // Default config doesn't generate any plugins
// await generator.run()
// assert.strictEqual(existsSync(pluginsDirectory), false)

// // Javascript plugin is generated
// generator.setConfig({
// plugin: true
// })
// await generator.run()
// assert.strictEqual(existsSync(pluginsDirectory), true)

// const exampleJsPlugin = await readFile(join(pluginsDirectory, 'example.js'), 'utf8')
// assert.strictEqual(exampleJsPlugin, generatePluginWithTypesSupport(false))

// // Remove dir to make sure generator generates the plugins directory
// // properly for typescript plugins
// await rm(dir)

// // Typescript plugin is generated
// generator.setConfig({
// plugin: true,
// typescript: true
// })
// await generator.run()
// assert.strictEqual(existsSync(pluginsDirectory), true)

// const exampleTsPlugin = await readFile(join(pluginsDirectory, 'example.ts'), 'utf8')
// assert.strictEqual(exampleTsPlugin, generatePluginWithTypesSupport(true))
// })
it('doesn\'t generate a plugin when not wanted', async () => {
const dir = await getTempDir(tempDirBase)

const generator = new AiWarpGenerator()
generator.setConfig({
targetDirectory: dir,
aiWarpPackageJsonPath: join(__dirname, '..', '..', 'package.json'),
aiProvider: 'openai',
aiModel: 'gpt-3.5-turbo'
})
await generator.run()

const pluginsDirectory = join(dir, 'plugins')
assert.strictEqual(existsSync(pluginsDirectory), false)
})

it('generates expected js example plugin', async () => {
const dir = await getTempDir(tempDirBase)

const generator = new AiWarpGenerator()
generator.setConfig({
targetDirectory: dir,
aiWarpPackageJsonPath: join(__dirname, '..', '..', 'package.json'),
aiProvider: 'openai',
aiModel: 'gpt-3.5-turbo',
plugin: true
})
await generator.run()

const pluginsDirectory = join(dir, 'plugins')
assert.strictEqual(existsSync(pluginsDirectory), true)

const exampleJsPlugin = await readFile(join(pluginsDirectory, 'example.js'), 'utf8')
assert.strictEqual(exampleJsPlugin, generatePluginWithTypesSupport(false).contents)
})

it('generates expected ts example plugin', async () => {
const dir = await getTempDir(tempDirBase)

const generator = new AiWarpGenerator()
generator.setConfig({
targetDirectory: dir,
aiWarpPackageJsonPath: join(__dirname, '..', '..', 'package.json'),
aiProvider: 'openai',
aiModel: 'gpt-3.5-turbo',
plugin: true,
typescript: true
})
await generator.run()

const pluginsDirectory = join(dir, 'plugins')
assert.strictEqual(existsSync(pluginsDirectory), true)

const exampleTsPlugin = await readFile(join(pluginsDirectory, 'example.ts'), 'utf8')
assert.strictEqual(exampleTsPlugin, generatePluginWithTypesSupport(true).contents)
})
})
16 changes: 0 additions & 16 deletions tests/utils/generators.ts

This file was deleted.

3 changes: 3 additions & 0 deletions tests/utils/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export const MOCK_STREAMING_CONTENT_CHUNKS = [
'chunk3'
]

/**
* @returns The full body that should be returned from the stream endpoint
*/
export function buildExpectedStreamBodyString (): string {
let body = ''
for (const chunk of MOCK_STREAMING_CONTENT_CHUNKS) {
Expand Down
26 changes: 26 additions & 0 deletions tests/utils/stackable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { buildServer } from '@platformatic/service'
import { FastifyInstance } from 'fastify'
import stackable, { setDontRegisterPlatformaticService } from '../../index'
import { AiWarpConfig } from '../../config'

export async function buildAiWarpApp (port: number, config: AiWarpConfig['aiProvider']): Promise<FastifyInstance> {
const app = await buildServer({
server: {
port,
forceCloseConnections: true,
healthCheck: {
enabled: false
},
logger: {
level: 'error'
}
}
})

app.platformatic.config.aiProvider = config

setDontRegisterPlatformaticService(true)
await app.register(stackable)

return app
}

0 comments on commit b2dc46d

Please sign in to comment.