describe('AddTeamPermissionSettings1785524400000', () => {
  it('backfills the new min-role settings for existing teams idempotently', async () => {
    const migrationModule = require('./1785524400000-AddTeamPermissionSettings');
    const migration =
      new migrationModule.AddTeamPermissionSettings1785524400000();
    const queryRunner = { query: jest.fn() } as any;

    await migration.up(queryRunner);

    const calls: string[] = queryRunner.query.mock.calls.map((c: any) => c[0]);
    expect(calls).toHaveLength(5);
    for (const key of [
      'invite_min_role',
      'member_manage_min_role',
      'penalty_manage_min_role',
      'public_access_manage_min_role',
    ]) {
      expect(
        calls.some(
          (sql) => sql.includes(`'${key}'`) && sql.includes('NOT EXISTS'),
        ),
      ).toBe(true);
    }
    expect(
      calls.some(
        (sql) =>
          sql.includes("'transaction_reverse_min_role'") &&
          sql.includes("'transaction_create_min_role'"),
      ),
    ).toBe(true);
  });

  it('removes the new min-role settings again on down', async () => {
    const migrationModule = require('./1785524400000-AddTeamPermissionSettings');
    const migration =
      new migrationModule.AddTeamPermissionSettings1785524400000();
    const queryRunner = { query: jest.fn() } as any;

    await migration.down(queryRunner);

    expect(queryRunner.query).toHaveBeenCalledTimes(1);
    const sql = queryRunner.query.mock.calls[0][0];
    expect(sql).toContain('DELETE FROM "team_setting"');
    expect(sql).toContain('transaction_reverse_min_role');
    expect(sql).toContain('invite_min_role');
    expect(sql).toContain('member_manage_min_role');
    expect(sql).toContain('penalty_manage_min_role');
    expect(sql).toContain('public_access_manage_min_role');
  });
});
