AI TDD
v0.0.3
提示使用測試套件提示GPT使其編寫代碼令人印象深刻準確
AITDD在BUN上運行,請確保您首先安裝最新的BUN版本。
作為CLI,全球安裝AITDD:
curl -sSL https://raw.githubusercontent.com/di-sukharev/AI-TDD/master/install.sh | bash
從Openai獲取API鍵。確保添加付款詳細信息,因此API可以使用。
將密鑰設置為AITDD配置:
aitdd config set OPENAI_API_KEY < your_api_key >
您的API密鑰在本地存儲在~/.aitdd/config
文件中,並且不會以任何其他方式存儲任何地方。
設置命令運行測試:
aitdd config set RUN_TESTS " npm run test "
您的API密鑰在本地存儲在~/.aitdd/config
文件中,並且不會以任何其他方式存儲任何地方。
這是用Jest + Testing庫編寫的前端測試套件。是的,AITDD輕鬆通過前端測試:
import React from "react" ;
import { rest } from "msw" ;
import { setupServer } from "msw/node" ;
import { render , fireEvent , waitFor , screen } from "@testing-library/react" ;
import "@testing-library/jest-dom" ;
import Fetch from "../fetch" ;
const server = setupServer (
rest . get ( "/greeting" , ( req , res , ctx ) => {
return res ( ctx . json ( { greeting : "hello there" } ) ) ;
} )
) ;
beforeAll ( ( ) => server . listen ( ) ) ;
afterEach ( ( ) => server . resetHandlers ( ) ) ;
afterAll ( ( ) => server . close ( ) ) ;
test ( "loads and displays greeting" , async ( ) => {
render ( < Fetch url = "/greeting" / > ) ;
fireEvent . click ( screen . getByText ( "Load Greeting" ) ) ;
await waitFor ( ( ) => screen . getByRole ( "heading" ) ) ;
expect ( screen . getByRole ( "heading" ) ) . toHaveTextContent ( "hello there" ) ;
expect ( screen . getByRole ( "button" ) ) . toBeDisabled ( ) ;
} ) ;
test ( "handles server error" , async ( ) => {
server . use (
rest . get ( "/greeting" , ( req , res , ctx ) => {
return res ( ctx . status ( 500 ) ) ;
} )
) ;
render ( < Fetch url = "/greeting" / > ) ;
fireEvent . click ( screen . getByText ( "Load Greeting" ) ) ;
await waitFor ( ( ) => screen . getByRole ( "alert" ) ) ;
expect ( screen . getByRole ( "alert" ) ) . toHaveTextContent ( "Oops, failed to fetch!" ) ;
expect ( screen . getByRole ( "button" ) ) . not . toBeDisabled ( ) ;
} ) ;
準備好?這是AITDD(GPT-4)生成的代碼通過測試:
import React , { useState } from "react" ;
function Fetch ( { url } ) {
const [ data , setData ] = useState ( null ) ;
const [ error , setError ] = useState ( null ) ;
const [ loading , setLoading ] = useState ( false ) ;
async function fetchData ( ) {
setLoading ( true ) ;
try {
const response = await fetch ( url ) ;
if ( ! response . ok ) throw new Error ( "Failed to fetch" ) ;
const data = await response . json ( ) ;
setData ( data . greeting ) ;
} catch ( error ) {
setError ( error . message ) ;
} finally {
setLoading ( false ) ;
}
}
return (
< div >
{ data && < h1 role = "heading" > { data } < / h1 > }
{ error && < div role = "alert" > { error } < / div > }
< button onClick = { fetchData } disabled = { loading } >
Load Greeting
< / button >
< / div >
) ;
}
export default Fetch ;
您可以這樣致電AITDD:
aitdd run
您向OpenAI API支付了自己的要求。 AITDD默認使用最新的GPT模型,請檢查其價格。最大響應令牌設置為2000,您可以通過ait config set maxTokens=<number>
對其進行調整。
我無法管理ChatGpt模型來解決問題。我試圖用一個響應示例拍攝它,它不了解我想要的。如果您想嘗試通過chatgpt進行管理 - 對其進行測試並打開PR