functions.test.ts70 lines · main
1import { codeBlock } from 'common-tags'
2import OpenAI from 'openai'
3import { describe, expect, test } from 'vitest'
4
5import { formatSql } from '../../test/util'
6import { debugSql, titleSql } from './functions'
7
8const openAiKey = process.env.OPENAI_API_KEY
9const openai = new OpenAI({ apiKey: openAiKey })
10
11describe('debug', () => {
12 test.concurrent('fix order of operations', async ({ expect }) => {
13 const { sql } = await debugSql(
14 openai,
15 'relation "departments" does not exist',
16 codeBlock`
17 create table employees (
18 id bigint primary key generated always as identity,
19 name text,
20 email text,
21 department_id bigint references departments (id)
22 );
23
24 create table departments (
25 id bigint primary key generated always as identity,
26 name text
27 );
28 `
29 )
30
31 expect(formatSql(sql)).toMatchSnapshot()
32 })
33
34 test.concurrent('fix typos', async ({ expect }) => {
35 const { sql, solution } = await debugSql(
36 openai,
37 'syntax error at or near "fromm"',
38 codeBlock`
39 select * fromm employees;
40 `
41 )
42
43 expect(solution).toBeDefined()
44 expect(formatSql(sql)).toMatchSnapshot()
45 })
46})
47
48describe('title', () => {
49 test('title matches content', async () => {
50 const { title, description } = await titleSql(
51 openai,
52 codeBlock`
53 create table employees (
54 id bigint primary key generated always as identity,
55 name text,
56 email text,
57 department_id bigint references departments (id)
58 );
59
60 create table departments (
61 id bigint primary key generated always as identity,
62 name text
63 );
64 `
65 )
66
67 await expect(title).toMatchCriteria('relates to employees and departments')
68 await expect(description).toMatchCriteria('describes employees and departments')
69 })
70})