import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';

const packageRoot = join(import.meta.dirname, '..', '..');
const gitToolsDir = join(packageRoot, 'framework', 'tools', 'git');

function readGitTool(scriptName: string): string {
  return readFileSync(join(gitToolsDir, scriptName), 'utf-8');
}

describe('Gitea git wrapper API calls', () => {
  it.each(['ci-queue-wait.sh', 'pr-ci-wait.sh'])(
    '%s follows Gitea API redirects before parsing JSON',
    (scriptName) => {
      const script = readGitTool(scriptName);

      expect(script).not.toMatch(/curl -fsS\s+(?:-H "[^"]+"\s+)*-H "Authorization: token/);
      expect(script).toMatch(/curl -fsSL\s+(?:-H "[^"]+"\s+)*-H "Authorization: token/);
    },
  );
});
