Giter Club home page Giter Club logo

pythagora's Introduction

This repo is deprecated - we're working on GPT Pilot

Pythagora Logo

Pythagora is on a mission to make automated tests
πŸ€– fully autonomous πŸ€–

Just run one command and watch the tests being created with GPT-4


The following details are for generating unit tests. To view the docs on how to generate integration tests, click here.


Visual Studio Code Logo Visual Studio Code Extension

If you want to try out Pythagora using Visual Studio Code extension you can download it here.

πŸƒπŸ’¨οΈ Quickstart

To install Pythagora run:

npm i pythagora --save-dev

Then, add your API key and you're ready to get tests generated. After that, just run the following command from the root directory of your repo:

npx pythagora --unit-tests --func <FUNCTION_NAME>

Where <FUNCTION_NAME> is the name of the function you want to generate unit tests for. Just make sure that your function is exported from a file. You can see other options like generating tests for multiple files or folders below in the Options section.



If you wish to expand your current test suite with more tests to get better code coverage you can run:

npx pythagora --expand-unit-tests --path <PATH_TO_YOUR_TEST_SUITE>

for more details on expanding existing tests see below in the Expanding existing tests section.



NOTE: on Windows make sure to run all commands using Git Bash and not Power Shell or anything similiar


🎞 Demo

Here are some demo videos that can help you get started.

Pythagora Alpha Demo

Pythagora Unit Tests Demo (2 min)


πŸ”Ž Examples

Here are examples of open sourced repositories that we forked and created tests with Pythagora so you can easily see it in action.

  • Lodash

    • πŸ“ 1604 tests generated
    • 🐞 11 bugs found (1 edge case and 10 bugs)
    • ⏳️ 4 hour run time

    lodash pythagora tests results

  • node-fs-extra

    • πŸ“ 98 tests generated
    • 🐞 2 bugs found
    • ⏳️ 30 minutes run time

    node-fs-extra pythagora tests results


πŸ”¬ How does it work?

When Pythagora generates unit tests, it uses the following approach:

  1. Find the function you want to test
  2. Find all the functions that are called from within that function
  • This is done with AST (Abstract Syntax Tree) parsing
  1. Send the function you want to test and all the related functions to the Pythagora server which then generates the unit tests with GPT-4
  • the Pythagora server is open sourced as well here
  • You can find the prompts in this folder on the Pythagora server

πŸ“ˆ Expand existing tests

If you already have generated tests for your codebase but you just want to increase your code coverage or cover more edge cases, simply run:

npx pythagora --expand-unit-tests --path <PATH_TO_YOUR_TEST_SUITE>

When running command PATH_TO_YOUR_TEST_SUITE can be path to a single test file or to a folder and all test files inside of that folder will be processed and expanded.

That's all, enjoy your new code coverage!

πŸ“– Options

  • To generate unit tests for one single function, run:

    npx pythagora --unit-tests --func <FUNCTION_NAME>
  • To generate unit tests for one single function in a specific file, run:

    npx pythagora --unit-tests --func <FUNCTION_NAME> --path ./path/to/file.js
  • To generate unit tests for all functions in a file, run:

    npx pythagora --unit-tests --path ./path/to/file.js
  • To generate unit tests for all functions in all files in a folder, run:

    npx pythagora --unit-tests --path ./path/to/folder/

βš™οΈ Config

Pythagora uses GPT-4 to generate tests so you either need to have OpenAI API Key or Pythagora API Key. You can get your Pythagora API Key here or OpenAI API Key here. Once you have it, add it to Pythagora with:

npx pythagora --config --pythagora-api-key <API_KEY>

or

npx pythagora --config --openai-api-key <API_KEY>

▢️ How to run unit tests

To run the generated tests, you can simply run

npx jest ./pythagora_tests/

or to run tests from a specific file or a folder, run npx jest <PATH_TO_FILE_OR_FOLDER>. Currently, Pythagora supports only generating Jest tests but if you would like it to generate tests in other frameworks, let us know at [email protected].


πŸ“ŒοΈ Notes

  • The best unit tests that Pythagora generates are the ones that are standalone functions (eg. helpers). Basically, the parts of the code that actually can be unit tested. For example, take a look at this Pythagora file - it contains helper functions that are a perfect candidate for unit tests. When we ran npx pythagora --unit-tests --path ./src/utils/common.js - it generated 145 tests from which only 17 failed. What is amazing is that only 6 tests failed because they were incorrectly written and the other 11 tests caught bugs in the code itself. You can view these tests here.

  • We don't store any of your code on our servers. However, the code is being sent to GPT and hence OpenAI. Here is their privacy policy.

  • a function you want to generate tests for needs to be exported from the file. For example, if you have a file like this:

    function mongoObjToJson(originalObj) {
        ...
    }
    
    module.exports = {
        mongoObjToJson
    };

    Then, to generate unit tests for the mongoObjToJson function, you can run:

    npx pythagora --unit-tests --func mongoObjToJson

πŸ€”οΈ FAQ

  • How accurate are these tests?

    • The best unit tests that Pythagora generates are the ones that are standalone functions. Basically, the parts of the code that actually can be unit tested. For example, take a look at this Pythagora file - it contains helper functions that are a perfect candidate for unit tests. When we ran npx pythagora --unit-tests --path ./src/utils/common.js - it generated 145 tests from which only 17 failed. What is amazing is that only 6 tests failed because they were incorrectly written and the other 11 tests caught bugs in the code itself. You can view these tests here.
    • Here are a couple of observations we've made while testing Pythagora:
      1. It does a great job at testing edge cases. For many repos we created tests for, the tests found bugs right away by testing edge cases.
      2. It works best for testing standalone helper functions. For example, we tried generating tests for the Lodash repo and it create 1000 tests from which only 40 needed additional review. For other, non standalone functions, we're planning to combine recordings from integration tests to generate proper mocks so that should expand Pythagora's test palette.
      3. It's definitely not perfect but the tests it created I wanted to keep and commit them. So, I encourage you to try it out and see how it works for you. If you do that, please let us know via email or Discord. We're super excited to hear how it went for you.

  • Should I review generated tests?

    • Absolutely. As mentioned above, some tests might be incorrectly written so it's best for you to review all tests before committing them. Nevertheless, I think this will save you a lot of time and will help you think about your code in a different way.

  • Tests help me think about my code - I don't want to generate them automatically

    • That's the best thing about Pythagora - it actually does help you think about the code. Just, you don't need to spend time writing tests. This happened to us, who created Pythagora - we coded it as fast as possible but when we added unit test generation, we realized that it cannot create tests for some functions. So, we refactored the code and made it more modular so that unit tests can be generated for it.

  • Is Pythagora limited to a specific programming language or framework?

    • Pythagora primarily generates unit tests for JavaScript code. However, it's designed to work with code written in JavaScript, TypeScript, and similar languages. If you'd like to see support for other languages or frameworks, please let us know at [email protected].

  • Can Pythagora generate integration tests as well?

    • Pythagora is currently focused on generating unit tests. For generating integration tests, you might need to combine the recordings from integration tests to generate proper mocks. We are actively exploring options to expand its capabilities in the future.

  • Is Pythagora compatible with all JavaScript testing frameworks?

    • Currently, Pythagora generates tests using the Jest testing framework. While we are open to expanding compatibility to other testing frameworks, Jest is the primary framework supported at the moment. If you have a specific framework in mind, feel free to share your suggestions with us.

  • How does Pythagora handle sensitive or proprietary code?

    • Pythagora doesn't store your code on its servers, but it sends code to GPT and OpenAI for test generation. It's essential to review the generated tests, especially if your code contains sensitive or proprietary information, before committing them to your repository. Be cautious when using Pythagora with sensitive code.

  • Is Pythagora suitable for all types of projects?

    • Pythagora works best for projects with well-structured code and standalone functions (such as helper functions). It excels at generating tests for these types of code. For more complex or non-standalone functions, manual review and modifications may be necessary.

🏁 Alpha version

This is an alpha version of Pythagora. To get an update about the beta release or to give a suggestion on tech (framework / database) you want Pythagora to support you can πŸ‘‰ add your email / comment here πŸ‘ˆ .

πŸ”— Connect with us

πŸ’¬ Join the discussion on our Discord server.

πŸ“¨ Get updates on new features and beta release by adding your email here.

🌟 As an open source tool, it would mean the world to us if you starred the Pythagora repo 🌟





pythagora's People

Contributors

adarsh-jha-dev avatar igor-pythagora avatar leonostrez avatar pavel-pythagora avatar up1 avatar zvone187 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pythagora's Issues

Use with Inversify?

Hello,

is it possible to use pythagora with Inversify?

With Inversify there is no manual initialization of express like let app = express();
but there is an instance created of InversifyExpressServer and configuration is then
set by InversifyExpressServer.setConfig(app => {...
Then, the built in router in InversifyExpressServer is used to register the row controller on the root url.

thanks!

VSCode execution environments options

I'd like it if the vscode environment for pythagora allowed for customized execution environments. In the same way that VSCode allows for me to execute my code on a remote system or a docker or whatever, pythagora should allow me to use separate system for the node commands and such. This allows for me to keep a cleaner environment and segment out requirements for your plugin and others.

Error: Cannot find module unit.js

Got this error:

$ npx pythagora --unit-tests --path index.js 
Need to install the following packages:
  [email protected]
Ok to proceed? (y) y
Generating unit tests...
node:internal/modules/cjs/loader:1078
  throw err;
  ^

Error: Cannot find module '/home/kuba/projects/jcubic/wayne/repo/node_modules/pythagora/src/scripts/unit.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
    at Module._load (node:internal/modules/cjs/loader:920:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Node.js v18.16.0
Bash script exited with code 0

when running on my Wayne library

Fastify support

Hi,

(sorry for the initial issue name, i made a mistake and i resolved it by myself)

Is there any fastify support planned for pythagora ?

To say this is free is misleading

you have a subscription based service.

This is not the same as being free.

I don't mind that there is a subscription fee, but it seems VERY misleading to say that it is free.

Configure with local LLM?

Is it possible to configure with local llm - i.e.: Ollama or LlamaCpp servers?

Some code cannot be sent to open apis.

Thanks

Errors are not being shown in terminal or error log

When running into errors, the terminal just shows "[object Object]pythagora_end:"
If I check the error logs, it just contains a JSON array of all the functions it tried, but doesn't show the error message.

I'm running the pythagora api server locally, and if I add a console.log to print out the stringified response in resFromOpenAI.on('data',... i can see the actual error object. (In this case, I'm rate limited)

image

Add support for Next.js

Hi, we're using Next.js and we'd be interested in using this software.
Is there an ETA for Next.js support?

Support required for Microservices.

When attempting to run multiple microservices on the same machine, the following error message is displayed:

Error: listen EADDRINUSE: address already in use.

Steps to Reproduce:
Run multiple microservices on the same machine, each listening on a different port.
Observe that the error message "Port is already in use" is displayed for some of the microservices, preventing them from starting.

Expected Behavior:
Each microservice should start successfully and listen on the specified port without any errors.

Actual Behavior:
The error message "Port is already in use" is displayed for all of the microservices, preventing them from starting.

Suggested Solution:
change node options from --inspect to --inspect=0 in run.bash file.

Firestore support

Hi, interesting tool you're building here!

Are there any plans to support Firestore?

How do you capture network traffic?

How do you capture network traffic?

Is there any change required in actual service code? If not, then how externally traffic can be monitored?

Thanks!

EXPORTED_TESTS_DIR Error: ENOTDIR: not a directory, mkdir '~/.nvm/versions/node/v18.18.0/bin/pythagora/pythagora_tests'

The test dir is trying to create a directory where file exists:

npm i pythagora
npx pythagora \
  --unit-tests \
  --path src/controllers/list.js \
  --pythagora-api-server http://localhost:3000

Starting generation of unit tests...
node:internal/fs/utils:350
    throw err;
    ^

Error: ENOTDIR: not a directory, mkdir '/Users/gardner/.nvm/versions/node/v18.18.0/bin/pythagora/pythagora_tests'
    at Object.mkdirSync (node:fs:1398:3)
    at /Users/gardner/.nvm/versions/node/v18.18.0/lib/node_modules/pythagora/src/helpers/starting.js:105:47
    at Array.forEach (<anonymous>)
    at setUpPythagoraDirs (/Users/gardner/.nvm/versions/node/v18.18.0/lib/node_modules/pythagora/src/helpers/starting.js:103:17)
    at Object.<anonymous> (/Users/gardner/.nvm/versions/node/v18.18.0/lib/node_modules/pythagora/src/scripts/unit.js:10:1)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:86:12) {
  errno: -20,
  syscall: 'mkdir',
  code: 'ENOTDIR',
  path: '/Users/gardner/.nvm/versions/node/v18.18.0/bin/pythagora/pythagora_tests'
}

Node.js v18.18.0
Bash script exited with code 0

When we inspect the path:

ls -lah /Users/gardner/.nvm/versions/node/v18.18.0/bin/pythagora
lrwxr-xr-x  1 gardner  staff    44B  3 Oct 12:13 /Users/gardner/.nvm/versions/node/v18.18.0/bin/pythagora -> ../lib/node_modules/pythagora/src/bin/run.js

These appear to be consts defined in https://github.com/Pythagora-io/js-code-processing

How can we pass a value in for test directory?

cmd/ps issue: expecting a bash terminal

Running (via npx) in PowerShell or cmd fails, e.g.

PS C:\Users\Me\Projects\test\pythagora-poc> npx pythagora --unit-tests --func simple
/bin/bash: C:UsersMeProjectstestpythagora-pocnode_modulespythagorasrcbinrun.bash: No such file or directory
Bash script exited with code 127

Running from git bash works fine.

Not a huge deal, I was still able to try it out. I think digging around the issue is it detects bash on my system, but that is for WSL, and the Windows path to the bash script (run.bash) won't resolve in WSL.

navigating away from page

I don't know if this is your extension or the interaction between your's and someone elses.
I click on open gpt pilot, and the page opens asking me to sign up. Before I can finish signing up (about 5 seconds), the page navigates away from the signup page.

Can't actually use it.

Compiled TS files produces "No tests generated"

Is there any way to transpile typescript so that pythagora can generate tests for it?
I tried transpiling a simple addNumbers function

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addNumbers = void 0;
function addNumbers(a, b) {
    return a + b;
}
exports.addNumbers = addNumbers;

[Bug] Installed packages not found

Pilot couldn't find npm installed, so I installed it. However, it's still not finding it. Runs fine from the console for me. Is there a custom PATH, or is the subprocess not loading the shell settings? I tried rebooting.

Thoughts or suggestions?

File extension is not supported

I'm currently running pythagora on a React codebase written in TypeScript and running into issues running the following command.
I've tried App.tsx as well; I'm reporting this here and my goal is to go over my codebase.

mnyon@Maximal:~/src/friends/queerglobal/qg-frontend-v2$ npx pythagora --unit-tests --path ./src/App.ts
Generating unit tests...
Error: File extension is not supported
    at traverseDirectory (/home/mnyon/src/friends/queerglobal/qg-frontend-v2/node_modules/pythagora/src/helpers/unitTests.js:213:73)
    at async generateTestsForDirectory (/home/mnyon/src/friends/queerglobal/qg-frontend-v2/node_modules/pythagora/src/helpers/unitTests.js:271:5)
Bash script exited with code 0

Doesn't like Windows 10

When I try to start without npx

Line |
  24 |      & "/bin/bash$exe"  "$basedir/node_modules/pythagora/src/bin/run.b …
     |        ~~~~~~~~~~~~~~~
     | The term '/bin/bash.exe' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

bug: error on log dumping due to invalid path - directory not found

I mistakenly configured an invalid api key. So the actual error looks like Error: Response status code: 401. Error message: Access denied. Invalid API key. but I wasn't able to see this message due to an error occurred during log dumping as below.

Error: ENOENT: no such file or directory, open '<path>/pythagora_tests/unit/errorLogs.log'
    at Object.openSync (node:fs:601:3)
    at Object.writeFileSync (node:fs:2249:35)
    at generateTestsForDirectory (<path>/node_modules/pythagora/src/helpers/unitTests.js:382:12) {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: '<path>/pythagora_tests/unit/errorLogs.log'
}

Fix: create the unit folder under pythagora_tests

MongoDB failure on app that doesn't use Mongo

Hey Pythagora. I love what you're aiming for with the project. I have a simple Express app that runs on a postgresql backend, and I created Pythagora tests on a few simple paths. When I went to run them, I encountered a Mongo error I wasn't expecting:

Running tests in 3 seconds...
Listening on port 5001
Starting tests on endpoints:
/
/v1/status

(node:9065) [MONGOOSE] DeprecationWarning: Mongoose: the `strictQuery` option will be switched back to `false` by default in Mongoose 7. Use `mongoose.set('strictQuery', false);` if you want to prepare for this change. Or use `mongoose.set('strictQuery', true);` to suppress this warning.
(Use `node --trace-deprecation ...` to show where the warning was created)
The app has crashed!
This is likely not related to Pythagora, but the app itself.
MongoRuntimeError: Unable to parse undefined:undefined with URL
    at new HostAddress (/Users/chris/Workspace/my-project/node_modules/mongodb/lib/utils.js:888:34)
    at fromString (/Users/chris/Workspace/my-project/node_modules/mongodb/lib/utils.js:930:16)
    ... 4 lines matching cause stack trace ...
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (/Users/chris/Workspace/my-project/node_modules/mongoose/lib/connection.js:799:19)
    at /Users/chris/Workspace/my-project/node_modules/mongoose/lib/index.js:409:10
    at /Users/chris/Workspace/my-project/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5 {
  cause: TypeError [ERR_INVALID_URL]: Invalid URL
      at new NodeError (node:internal/errors:372:5)
      at URL.onParseError (node:internal/url:553:9)
      at new URL (node:internal/url:629:5)
      at new HostAddress (/Users/chris/Workspace/my-project/node_modules/mongodb/lib/utils.js:885:19)
      at fromString (/Users/chris/Workspace/my-project/node_modules/mongodb/lib/utils.js:930:16)
      at Array.map (<anonymous>)
      at parseOptions (/Users/chris/Workspace/my-project/node_modules/mongodb/lib/connection_string.js:210:45)
      at new MongoClient (/Users/chris/Workspace/my-project/node_modules/mongodb/lib/mongo_client.js:46:63)
      at /Users/chris/Workspace/my-project/node_modules/mongoose/lib/connection.js:802:16
      at new Promise (<anonymous>) {
    input: 'iLoveJS://undefined:undefined',
    code: 'ERR_INVALID_URL'
  },
  [Symbol(errorLabels)]: Set(0) {}
}

Express won't start.

Normally when I start my Express API Server, I get:

Connected Database: db1
Application running on port: 5001

But when I run npx pythagora --initScript ./app/server.js --mode capture it doesn't seem to hit the callback in server.listen and I get this instead:

Connected Database: db1

I'm running Express as an API server for a React frontend with a Postgres DB.
I'm trying to use this on my Express API Server.

Can you please help?
I'm not sure how to get this to work.
Thanks.

Support more languages stack

Is it possible to make pythagora support more languages, eg: java, python, golang...
I know pythagora patched the http/https module of node.js, I think it would be grate if it can be used on my java project.

Gerneated Tests are empty when run on a fresh project.

When i delete the pythagora_tests folder first and then generate the tests, it always generates me an almost empty unit-test file with only the lines


` ` ` ` javascript

in it.
After i leave the existing folder and just delete the unit-test-file, it will run fine again.

Migration to Typescript (Question)

Hi @LeonOstrez ,
Is there any plan to migrate the project to Typescript by any chance. As it would for helpful for development and maintenance.

Just as a suggestion as it will be easier if done right on start of the project rather than, processing it in a much more complicated stage. Kindly let me know if you plan for a migration.

World be happy to help πŸ™Œ

Add --impatient option

Would be cool, if it only generated one test, so you can quickly see, if your project setup is correct and if it looks at the right thing and has a proper general understanding.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.