import { basename, resolve } from 'node:path';
import process from 'node:process';

import { loadEnv } from 'vite';
import { describe, expect, it } from 'vitest';

const APP_ROOT =
  basename(process.cwd()) === 'app'
    ? process.cwd()
    : resolve(process.cwd(), 'app');
const EXPECTED_DEPLOYMENT_CONTRACT = {
  VITE_BASE: '/flow/',
  VITE_GLOB_API_URL: '/flow',
  VITE_INJECT_APP_LOADING: 'true',
  VITE_ROUTER_HISTORY: 'hash',
};

describe.each(['development', 'production'])(
  '%s environment deployment contract',
  (mode) => {
    it('uses the same base path, API prefix and router mode', () => {
      const env = loadEnv(mode, APP_ROOT, 'VITE_');

      expect({
        VITE_BASE: env.VITE_BASE,
        VITE_GLOB_API_URL: env.VITE_GLOB_API_URL,
        VITE_INJECT_APP_LOADING: env.VITE_INJECT_APP_LOADING,
        VITE_ROUTER_HISTORY: env.VITE_ROUTER_HISTORY,
      }).toEqual(EXPECTED_DEPLOYMENT_CONTRACT);
    });
  },
);
