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
48 changes: 24 additions & 24 deletions src/components/admin/connections/SecretField.utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConnectionType } from "@/hooks/intelligence";
import type { ConnectionType } from '@/hooks/intelligence';

/**
* Mapeia o `connectionId` (curto, usado nas abas) para a `ConnectionType`
Expand All @@ -7,23 +7,23 @@ import type { ConnectionType } from "@/hooks/intelligence";
*/
export function mapConnectionToTester(
connectionId: string | undefined,
): { type: ConnectionType; envKey?: "promobrind" | "crm" } | null {
): { type: ConnectionType; envKey?: 'promobrind' | 'crm' } | null {
if (!connectionId) return null;
if (connectionId === "n8n") return { type: "n8n" };
if (connectionId === "bitrix24") return { type: "bitrix24" };
if (connectionId === "mcp") return { type: "mcp" };
if (connectionId === "promobrind" || connectionId === "crm") {
return { type: "supabase", envKey: connectionId };
if (connectionId === 'n8n') return { type: 'n8n' };
if (connectionId === 'bitrix24') return { type: 'bitrix24' };
if (connectionId === 'mcp') return { type: 'mcp' };
if (connectionId === 'promobrind' || connectionId === 'crm') {
return { type: 'supabase', envKey: connectionId };
}
return null;
}

export function formatRelative(iso: string): string {
const then = new Date(iso).getTime();
const diffMs = Date.now() - then;
if (Number.isNaN(then)) return "";
if (Number.isNaN(then)) return '';
const sec = Math.floor(diffMs / 1000);
if (sec < 60) return "agora";
if (sec < 60) return 'agora';
const min = Math.floor(sec / 60);
if (min < 60) return `há ${min}m`;
const hr = Math.floor(min / 60);
Expand All @@ -38,14 +38,14 @@ export function formatRelative(iso: string): string {
export function formatFullPtBr(iso: string): string {
const date = new Date(iso);
if (Number.isNaN(date.getTime())) return iso;
const fmt = new Intl.DateTimeFormat("pt-BR", {
day: "2-digit",
month: "2-digit",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
timeZoneName: "short",
const fmt = new Intl.DateTimeFormat('pt-BR', {
day: '2-digit',
month: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZoneName: 'short',
});
return fmt.format(date);
}
Expand All @@ -56,19 +56,19 @@ export function formatFullPtBr(iso: string): string {
export function buildUpdatedTooltip(
updatedAt: string | null | undefined,
updatedByEmail: string | null | undefined,
updatedById?: string | null | undefined,
updatedBy?: string | null | undefined,
): string | undefined {
if (!updatedAt) return undefined;

const dateStr = formatFullPtBr(updatedAt);
const relative = formatRelative(updatedAt);
let author = "sistema (sem autor registrado)";

let author = 'sistema (sem autor registrado)';
if (updatedByEmail) {
author = updatedByEmail;
} else if (updatedById) {
author = `equipe (#${updatedById.substring(0, 8)})`;
} else if (updatedBy) {
author = `equipe (#${updatedBy.substring(0, 8)})`;
}

return `Atualizado ${relative}\n${dateStr}\npor ${author}`;
}
33 changes: 23 additions & 10 deletions tests/components/admin/SecretField.a11y.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,29 @@ import { axe } from "../../a11y/axe-helper";
const setSecretMock = vi.fn();
const rotateSecretMock = vi.fn();
const getRotationHistoryMock = vi.fn().mockResolvedValue([]);
vi.mock("@/hooks/useSecretsManager", () => ({
useSecretsManager: () => ({
setSecret: setSecretMock,
rotateSecret: rotateSecretMock,
getRotationHistory: getRotationHistoryMock,
}),
}));
vi.mock("@/hooks/useConnectionTestDetails", () => ({
useConnectionTestDetails: () => ({ details: null, loading: false, error: null, refresh: vi.fn() }),
}));
vi.mock("@/hooks/admin", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/hooks/admin")>();
return {
...actual,
useSecretsManager: () => ({
setSecret: setSecretMock,
rotateSecret: rotateSecretMock,
getRotationHistory: getRotationHistoryMock,
isLoading: false,
secrets: [],
listError: null,
list: vi.fn(),
refreshCache: vi.fn(),
}),
};
});
vi.mock("@/hooks/intelligence", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/hooks/intelligence")>();
return {
...actual,
useConnectionTestDetails: () => ({ details: null, loading: false, error: null, refetch: vi.fn() }),
};
});
vi.mock("@/components/admin/connections/CredentialsSourceFilterContext", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/components/admin/connections/CredentialsSourceFilterContext")>();
return {
Expand Down
36 changes: 25 additions & 11 deletions tests/components/admin/SecretField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,31 @@ import { render, screen, fireEvent } from "@testing-library/react";
const setSecretMock = vi.fn();
const rotateSecretMock = vi.fn();
const getRotationHistoryMock = vi.fn().mockResolvedValue([]);
vi.mock("@/hooks/useSecretsManager", () => ({
useSecretsManager: () => ({
setSecret: setSecretMock,
rotateSecret: rotateSecretMock,
getRotationHistory: getRotationHistoryMock,
}),
}));
vi.mock("@/hooks/admin", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/hooks/admin")>();
return {
...actual,
useSecretsManager: () => ({
setSecret: setSecretMock,
rotateSecret: rotateSecretMock,
getRotationHistory: getRotationHistoryMock,
isLoading: false,
secrets: [],
listError: null,
list: vi.fn(),
refreshCache: vi.fn(),
}),
};
});

// O hook de detalhes de teste de conexão dispara queries — neutralizamos.
vi.mock("@/hooks/useConnectionTestDetails", () => ({
useConnectionTestDetails: () => ({ details: null, loading: false, error: null, refresh: vi.fn() }),
}));
vi.mock("@/hooks/intelligence", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/hooks/intelligence")>();
return {
...actual,
useConnectionTestDetails: () => ({ details: null, loading: false, error: null, refetch: vi.fn() }),
};
});

// Filtro de fonte de credencial usa contexto — sobrescreve só o hook,
// preservando exports auxiliares (`resolveSource`, etc) usados pelo
Expand Down Expand Up @@ -208,14 +221,15 @@ describe("SecretField — handler de salvar nunca chama API com sufixo <4 chars"
it("modo rotate: status com has_value → botão Rotacionar abre input, e Salvar com 3 chars NÃO chama rotateSecret", () => {
renderField({
status: {
name: "MCP_SHARED_SECRET",
has_value: true,
length: 32,
masked_suffix: "abcd",
source: "db",
env_fallback_active: false,
updated_at: new Date().toISOString(),
updated_by_email: null,
} as any,
},
});

// Entra em modo rotate via botão "Rotacionar".
Expand Down
29 changes: 23 additions & 6 deletions tests/components/admin/pluralization.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,29 @@ import { render, screen, fireEvent } from "@testing-library/react";
const setSecretMock = vi.fn();
const rotateSecretMock = vi.fn();
const getRotationHistoryMock = vi.fn().mockResolvedValue([]);
vi.mock("@/hooks/useSecretsManager", () => ({
useSecretsManager: () => ({ setSecret: setSecretMock, rotateSecret: rotateSecretMock, getRotationHistory: getRotationHistoryMock }),
}));
vi.mock("@/hooks/useConnectionTestDetails", () => ({
useConnectionTestDetails: () => ({ details: null, loading: false, error: null, refresh: vi.fn() }),
}));
vi.mock("@/hooks/admin", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/hooks/admin")>();
return {
...actual,
useSecretsManager: () => ({
setSecret: setSecretMock,
rotateSecret: rotateSecretMock,
getRotationHistory: getRotationHistoryMock,
isLoading: false,
secrets: [],
listError: null,
list: vi.fn(),
refreshCache: vi.fn(),
}),
};
});
vi.mock("@/hooks/intelligence", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/hooks/intelligence")>();
return {
...actual,
useConnectionTestDetails: () => ({ details: null, loading: false, error: null, refetch: vi.fn() }),
};
});
vi.mock("@/components/admin/connections/CredentialsSourceFilterContext", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/components/admin/connections/CredentialsSourceFilterContext")>();
return {
Expand Down
Loading