Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/workflows/playground_unit_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code using yarn build:app run unit tests using yarn test:unit.

name: Playground Unit tests
permissions:
contents: read

on:
pull_request:
branches:
- development
paths:
- "packages/playground/**"

jobs:
unit-tests:
runs-on: ubuntu-latest
env:
NODE_OPTIONS: "--max-old-space-size=8192"

strategy:
matrix:
node-version: [22.16]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v4

- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: "yarn"
cache-dependency-path: "**/yarn.lock"

- name: Install dependencies
run: |
yarn install
- name: Build
run: |
lerna run build --no-private
- name: Run unit tests
run: yarn workspace @threefold/playground test:unit
10 changes: 10 additions & 0 deletions packages/playground/tests/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Vitest setup file - runs before all tests
// Set up mock window.env that matches the development environment
// @ts-ignore - Add window object for test environment
global.window = global.window || {};
// @ts-ignore - Add env object to window
global.window.env = {
// set the needed env variables for the tests
};

console.log("Test environment initialized with mock window.env");
42 changes: 42 additions & 0 deletions packages/playground/tests/utils/extractDomainIP.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { describe, expect, it } from "vitest";

import { extractDomainIP } from "../../src/utils/gateway";

describe("extractDomainIP", () => {
it("should extract the domain from a URL with a protocol", () => {
const domain = extractDomainIP("https://example.com:8080");
expect(domain).toBe("example.com");
});

it("should extract the IPv6 address from a URL", () => {
const ipv6 = extractDomainIP("https://[::1]:8080");
expect(ipv6).toBe("::1");
});

it("should throw an error when there is no domain or IP address", () => {
expect(() => extractDomainIP("http://:8080")).toThrow(
'Invalid input "http://:8080": No domain or IP address found.',
);
});

it("should throw an error for invalid IPv6 format", () => {
expect(() => extractDomainIP("https://[]:8080")).toThrow(
'Invalid input "https://[]:8080": Invalid IPv6 address format.',
);
});

it("should extract the domain from a URL without a port", () => {
const domain = extractDomainIP("https://example.com");
expect(domain).toBe("example.com");
});

it("should extract the IPv4 address from a URL", () => {
const ipv4 = extractDomainIP("http://192.168.0.1:3000");
expect(ipv4).toBe("192.168.0.1");
});

it("should handle plain domain strings without protocols", () => {
const domain = extractDomainIP("example.com");
expect(domain).toBe("example.com");
});
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to add test cases for urls with query params?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not supported by the extractDomainIP function, Please open a separate issue for that if needed, as these tests were added only to verify the functionality of Vitest configuration

1 change: 1 addition & 0 deletions packages/playground/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default mergeConfig(
environment: "jsdom",
exclude: [...configDefaults.exclude, "e2e/*"],
root: fileURLToPath(new URL("./", import.meta.url)),
setupFiles: ["./tests/setup.ts"],
},
}),
);
Loading