diff --git a/.github/workflows/playground_unit_test.yaml b/.github/workflows/playground_unit_test.yaml new file mode 100644 index 0000000000..ef6e4fb584 --- /dev/null +++ b/.github/workflows/playground_unit_test.yaml @@ -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 diff --git a/packages/playground/tests/setup.ts b/packages/playground/tests/setup.ts new file mode 100644 index 0000000000..5757962e31 --- /dev/null +++ b/packages/playground/tests/setup.ts @@ -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"); diff --git a/packages/playground/tests/utils/extractDomainIP.test.ts b/packages/playground/tests/utils/extractDomainIP.test.ts new file mode 100644 index 0000000000..51a3915d13 --- /dev/null +++ b/packages/playground/tests/utils/extractDomainIP.test.ts @@ -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"); + }); +}); diff --git a/packages/playground/vitest.config.ts b/packages/playground/vitest.config.ts index 089e6b8064..16751bcd76 100644 --- a/packages/playground/vitest.config.ts +++ b/packages/playground/vitest.config.ts @@ -10,6 +10,7 @@ export default mergeConfig( environment: "jsdom", exclude: [...configDefaults.exclude, "e2e/*"], root: fileURLToPath(new URL("./", import.meta.url)), + setupFiles: ["./tests/setup.ts"], }, }), );