diff --git a/.github/workflows/examples-tests.yml b/.github/workflows/examples-tests.yml new file mode 100644 index 0000000..3368961 --- /dev/null +++ b/.github/workflows/examples-tests.yml @@ -0,0 +1,47 @@ +name: E2E Examples + +on: + push: + branches: ["main"] + paths-ignore: + - "**/*.md" + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + test: + name: Tests + timeout-minutes: 40 + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + node-version: [18.x] + + steps: + - uses: actions/checkout@v4 + - name: Enable Corepack + run: corepack enable + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node-version }} + cache: "yarn" + - name: Install dependencies + run: yarn install --immutable + - name: Install ollama + run: curl -fsSL https://ollama.com/install.sh | sh + - name: Run ollama + run: | + ollama serve & + ollama pull llama3.1 + - name: Call ollama API + run: | + curl -d '{"model": "llama3.1:latest", "stream": false, "prompt":"Whatever I say, asnwer with Yes"}' http://localhost:11434/api/generate + - name: Example Tests + run: | + yarn test:examples diff --git a/package.json b/package.json index b4a29d5..52fae58 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,8 @@ "test:e2e:watch": "vitest watch tests", "test:all": "vitest run", "test:watch": "vitest watch", + "test:examples": "vitest run tests/e2e/examples", + "test:examples:watch": "vitest run tests/e2e/examples", "prepare": "husky", "copyright": "./scripts/copyright.sh", "release": "release-it", @@ -181,7 +183,7 @@ "temp-dir": "^3.0.0", "tsc-files": "^1.1.4", "tsup": "^8.3.0", - "tsx": "^4.19.0", + "tsx": "^4.19.1", "typescript": "^5.5.4", "typescript-eslint": "^8.2.0", "vite-tsconfig-paths": "^5.0.1", diff --git a/tests/e2e/examples.test.ts b/tests/e2e/examples.test.ts new file mode 100644 index 0000000..2092df9 --- /dev/null +++ b/tests/e2e/examples.test.ts @@ -0,0 +1,57 @@ +/** + * Copyright 2024 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { execSync } from "child_process"; +import * as fs from "fs"; +import * as path from "path"; + +const pattern = process.env.EXAMPLE_NAME; +const files: string[] = []; +function getTypeScriptFiles(directoryPath: string) { + const fileNames = fs.readdirSync(directoryPath); + + for (const fileName of fileNames) { + const filePath = path.join(directoryPath, fileName); + if (fs.statSync(filePath).isDirectory()) { + getTypeScriptFiles(filePath); + } else { + if (filePath.endsWith(".ts")) { + files.push(filePath); + } + } + } +} + +function runExample(example: string) { + describe("e2e test", () => { + it(`Runs ${example}`, async () => { + execSync(`yarn start -- ${example}`); + }); + }); +} + +const exampleDirectory = "./examples"; +getTypeScriptFiles(exampleDirectory); + +for (const example of files) { + if (pattern == null || pattern.trim() == "") { + runExample(example); + } else { + if (example.includes(pattern)) { + runExample(example); + } + } +}