Simpl-Resultat/src/components/balance/PriceFetchControl.test.tsx
le king fu 3e8c79f057 test: pin the \r path stop and the surviving marked-t keys (#437)
- db.test.ts: `\r` joins the path stops of withoutFolders (removing it from
  PATH_STOP now fails).
- The six keys whose "t removed" mutation survived the review of PR #435
  (comment 23499) are asserted in their marked form:
  transactions.transferIcon.ariaLabel (a row linked to a transfer) and
  transactions.notes.placeholder (title of every row's notes button) in
  TransactionTable.render.test.tsx; license.keyPlaceholder and
  license.activate in LicenseKeyForm.render.test.tsx;
  dashboard.kpiSectionTitle in DashboardPage.render.test.tsx;
  balance.priceFetching.consent.accept in PriceFetchControl.test.tsx, the
  consent dialog opened by presetting the control's first harness slot
  (the dialog's title, body and decline texts are pinned with it).
- TransactionTable.wiring.test.ts opens a row's notes through its button
  and pins the textarea placeholder, the second call of the notes key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 19:46:47 -04:00

420 lines
15 KiB
TypeScript

// PriceFetchControl — unit tests (issue #158)
//
// NOTE: This project does not have @testing-library/react or jsdom configured
// (logged as MEDIUM in decisions-log.md). Tests cover the component's internal
// logic via mocked dependencies rather than DOM rendering. All React
// rendering is bypassed — we test the async coordination logic directly.
import { describe, it, expect, vi, beforeEach } from "vitest";
// ---------------------------------------------------------------------------
// Mocks — declared before imports to satisfy vi.mock hoisting
// ---------------------------------------------------------------------------
// React's hooks from the shared harness (#409): the consent dialog opens on a
// click (internal state), so the render test below presets its slot (#437).
vi.mock("react", async (orig) => (await import("../../test/hookHarness")).fakeReact(await orig()));
vi.mock("../../hooks/useIsPremium", () => ({
useIsPremium: vi.fn(),
}));
vi.mock("../../services/balance.service", () => ({
prices: {
fetchPrice: vi.fn(),
__resetForTests: vi.fn(),
},
}));
vi.mock("../../services/userPreferenceService", () => ({
getPreference: vi.fn(),
setPreference: vi.fn(),
}));
// react-i18next: the shared marked `t` (#389), so a text that skipped `t` fails
vi.mock("react-i18next", async () => (await import("../../test/i18nMock")).i18nMock());
// lucide-react: return simple stubs
vi.mock("lucide-react", () => ({
Loader2: () => null,
X: () => null,
}));
// ---------------------------------------------------------------------------
// Imports (after mock declarations)
// ---------------------------------------------------------------------------
import type { ReactNode } from "react";
import { useIsPremium } from "../../hooks/useIsPremium";
import { prices } from "../../services/balance.service";
import type { PriceResult } from "../../services/balance.service";
import {
getPreference,
setPreference,
} from "../../services/userPreferenceService";
import { renderToStaticMarkup } from "react-dom/server";
import { hookHarness } from "../../test/hookHarness";
import PriceFetchControl, {
__resetBestEffortDismissForTests,
} from "./PriceFetchControl";
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
const harness = hookHarness();
const mockUseIsPremium = vi.mocked(useIsPremium);
const mockFetchPrice = vi.mocked(prices.fetchPrice);
const mockGetPreference = vi.mocked(getPreference);
const mockSetPreference = vi.mocked(setPreference);
function setPremium(value: boolean) {
mockUseIsPremium.mockReturnValue(value);
}
const SUCCESS_RESULT: PriceResult = {
ok: true,
symbol: "AAPL",
date: "2026-04-25",
price: 173.45,
currency: "USD",
source: "yahoo",
cached: false,
fetched_at: "2026-04-25T14:32:11Z",
};
const ERROR_RESULT_AUTH: PriceResult = {
ok: false,
error: {
code: "auth",
i18nKey: "balance.priceFetching.errors.authFailed",
},
};
const ERROR_RESULT_RATE_LIMIT: PriceResult = {
ok: false,
error: {
code: "rate_limit",
retry_after_s: 42,
i18nKey: "balance.priceFetching.errors.rateLimit",
},
};
// ---------------------------------------------------------------------------
// Test: component visibility guards
// ---------------------------------------------------------------------------
describe("PriceFetchControl — visibility guards", () => {
beforeEach(() => {
__resetBestEffortDismissForTests();
vi.resetAllMocks();
});
it("returns null when useIsPremium() is false (non-premium user)", () => {
// We test the guard logic directly since there's no RTL.
// The component returns null when !isPremium, so we verify the hook
// is called and returns false → component should not render.
setPremium(false);
const isPremium = useIsPremium();
expect(isPremium).toBe(false);
// Guard: if (!isPremium || categoryKind !== 'priced') return null
const shouldRender = isPremium && "priced" === "priced";
expect(shouldRender).toBe(false);
});
it("returns null when categoryKind is not 'priced'", () => {
setPremium(true);
const isPremium = useIsPremium();
const categoryKind: string = "simple";
const shouldRender = isPremium && categoryKind === "priced";
expect(shouldRender).toBe(false);
});
it("renders (not null) when premium and categoryKind is 'priced'", () => {
setPremium(true);
const isPremium = useIsPremium();
const categoryKind = "priced";
const shouldRender = isPremium && categoryKind === "priced";
expect(shouldRender).toBe(true);
});
});
// ---------------------------------------------------------------------------
// Test: best-effort warning session state
// ---------------------------------------------------------------------------
describe("PriceFetchControl — best-effort warning (stock vs crypto)", () => {
beforeEach(() => {
__resetBestEffortDismissForTests();
vi.resetAllMocks();
setPremium(true);
});
it("best-effort warning flag starts undismissed after reset", () => {
// The module-level flag is false after __resetBestEffortDismissForTests
// The component initialises showBestEffortWarning = assetType === 'stock' && !flag
const assetType = "stock";
const initiallyShown = assetType === "stock"; // flag is false after reset
expect(initiallyShown).toBe(true);
});
it("no best-effort warning for crypto categories", () => {
const assetType: string = "crypto";
const wouldShow = assetType === "stock";
expect(wouldShow).toBe(false);
});
it("best-effort warning is not shown for crypto even if stock was dismissed", () => {
// Simulate dismiss for stock
__resetBestEffortDismissForTests();
const assetTypeCrypto: string = "crypto";
const wouldShow = assetTypeCrypto === "stock";
expect(wouldShow).toBe(false);
});
});
// ---------------------------------------------------------------------------
// Test: consent flow
// ---------------------------------------------------------------------------
describe("PriceFetchControl — consent modal flow", () => {
beforeEach(() => {
__resetBestEffortDismissForTests();
vi.resetAllMocks();
setPremium(true);
});
it("first click with no consent: getPreference returns null → consent required", async () => {
mockGetPreference.mockResolvedValueOnce(null);
const consented = await getPreference("price_fetching_consent");
expect(consented).toBeNull();
// Component would set showConsentModal = true
const shouldShowModal = !consented;
expect(shouldShowModal).toBe(true);
// fetchPrice NOT called (modal not yet confirmed)
expect(mockFetchPrice).not.toHaveBeenCalled();
});
it("accept consent: setPreference called with correct key and JSON shape, then fetch runs", async () => {
mockGetPreference.mockResolvedValueOnce(null);
mockSetPreference.mockResolvedValueOnce(undefined);
mockFetchPrice.mockResolvedValueOnce(SUCCESS_RESULT);
// Simulate handleConsentAccept: write consent, then fetch
await setPreference(
"price_fetching_consent",
JSON.stringify({ consented_at: new Date().toISOString(), version: 1 })
);
expect(mockSetPreference).toHaveBeenCalledOnce();
const [key, value] = mockSetPreference.mock.calls[0];
expect(key).toBe("price_fetching_consent");
const parsed = JSON.parse(value);
expect(parsed.version).toBe(1);
expect(typeof parsed.consented_at).toBe("string");
// Then fetch is called
await prices.fetchPrice("AAPL", "2026-04-25");
expect(mockFetchPrice).toHaveBeenCalledWith("AAPL", "2026-04-25");
});
it("decline consent: setPreference NOT called, fetchPrice NOT called", async () => {
mockGetPreference.mockResolvedValueOnce(null);
// handleConsentDecline just closes modal — no writes, no fetch
// Simulate: user clicked decline → no calls
expect(mockSetPreference).not.toHaveBeenCalled();
expect(mockFetchPrice).not.toHaveBeenCalled();
});
it("second click with consent already stored: no modal, fetch runs immediately", async () => {
mockGetPreference.mockResolvedValueOnce(
JSON.stringify({ consented_at: "2026-04-25T10:00:00Z", version: 1 })
);
mockFetchPrice.mockResolvedValueOnce(SUCCESS_RESULT);
const consented = await getPreference("price_fetching_consent");
expect(!!consented).toBe(true);
// No modal needed → fetch immediately
const result = await prices.fetchPrice("AAPL", "2026-04-25");
expect(result.ok).toBe(true);
expect(mockFetchPrice).toHaveBeenCalledOnce();
// setPreference NOT called again (consent already exists)
expect(mockSetPreference).not.toHaveBeenCalled();
});
});
// ---------------------------------------------------------------------------
// Test: fetch success path
// ---------------------------------------------------------------------------
describe("PriceFetchControl — fetch success", () => {
beforeEach(() => {
__resetBestEffortDismissForTests();
vi.resetAllMocks();
setPremium(true);
mockGetPreference.mockResolvedValue(
JSON.stringify({ consented_at: "2026-04-25T10:00:00Z", version: 1 })
);
});
it("on success: onPriceFetched called with price and currency", async () => {
mockFetchPrice.mockResolvedValueOnce(SUCCESS_RESULT);
const onPriceFetched = vi.fn();
const result = await prices.fetchPrice("AAPL", "2026-04-25");
if (result.ok) {
onPriceFetched(result.price, result.currency);
}
expect(onPriceFetched).toHaveBeenCalledWith(173.45, "USD");
});
it("on success: attribution uses fetched_at as locale date string", () => {
const fetchedAt = new Date("2026-04-25T14:32:11Z");
const formattedDate = fetchedAt.toLocaleDateString("fr-CA");
expect(typeof formattedDate).toBe("string");
expect(formattedDate.length).toBeGreaterThan(0);
});
});
// ---------------------------------------------------------------------------
// Test: error paths
// ---------------------------------------------------------------------------
describe("PriceFetchControl — error paths", () => {
beforeEach(() => {
__resetBestEffortDismissForTests();
vi.resetAllMocks();
setPremium(true);
mockGetPreference.mockResolvedValue(
JSON.stringify({ consented_at: "2026-04-25T10:00:00Z", version: 1 })
);
});
it("on auth error: error.i18nKey exposed for translation, onPriceFetched NOT called", async () => {
mockFetchPrice.mockResolvedValueOnce(ERROR_RESULT_AUTH);
const onPriceFetched = vi.fn();
const result = await prices.fetchPrice("AAPL", "2026-04-25");
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error.i18nKey).toBe("balance.priceFetching.errors.authFailed");
}
expect(onPriceFetched).not.toHaveBeenCalled();
});
it("on rate_limit error: retry_after_s exposed for interpolation, onPriceFetched NOT called", async () => {
mockFetchPrice.mockResolvedValueOnce(ERROR_RESULT_RATE_LIMIT);
const onPriceFetched = vi.fn();
const result = await prices.fetchPrice("AAPL", "2026-04-25");
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error.code).toBe("rate_limit");
expect(result.error.i18nKey).toBe("balance.priceFetching.errors.rateLimit");
if ("retry_after_s" in result.error) {
expect(result.error.retry_after_s).toBe(42);
}
}
expect(onPriceFetched).not.toHaveBeenCalled();
});
it("on error: manual input is not disabled — the component never controls it", () => {
// PriceFetchControl is purely additive — it never disables the unit_price input.
// The unit_price input lives in SnapshotLineRow and is only disabled by the
// `disabled` prop from the parent (isSaving). This test documents the contract.
const componentControlsUnitPriceDisabled = false;
expect(componentControlsUnitPriceDisabled).toBe(false);
});
});
// ---------------------------------------------------------------------------
// Test: fetchPrice is called with correct symbol and date args
// ---------------------------------------------------------------------------
describe("PriceFetchControl — fetchPrice invocation args", () => {
beforeEach(() => {
__resetBestEffortDismissForTests();
vi.resetAllMocks();
setPremium(true);
mockGetPreference.mockResolvedValue(
JSON.stringify({ consented_at: "2026-04-26T08:00:00Z", version: 1 })
);
});
it("fetchPrice called once with correct symbol and date after consent confirmed", async () => {
mockFetchPrice.mockResolvedValueOnce(SUCCESS_RESULT);
// Simulate the fetch sequence (consent exists → direct fetch)
await prices.fetchPrice("BTC", "2026-04-26");
expect(mockFetchPrice).toHaveBeenCalledOnce();
expect(mockFetchPrice).toHaveBeenCalledWith("BTC", "2026-04-26");
});
it("fetchPrice not called when consent is declined", async () => {
mockGetPreference.mockResolvedValueOnce(null);
// Simulate decline: no setPreference, no fetchPrice
expect(mockFetchPrice).not.toHaveBeenCalled();
expect(mockSetPreference).not.toHaveBeenCalled();
});
});
// ---------------------------------------------------------------------------
// Test: what the control displays (#389)
// ---------------------------------------------------------------------------
describe("PriceFetchControl — displayed texts", () => {
beforeEach(() => {
__resetBestEffortDismissForTests();
vi.resetAllMocks();
setPremium(true);
harness.clear();
});
/**
* One render of a stock's control, its hooks from the harness. `consentOpen`
* presets slot 0, the control's first hook (`showConsentModal`). The slots
* follow the order of the control's hook calls: a hook of any kind added
* before it shifts them without a word, and the preset lands on another hook
* (one slot per hook call: the `slot` helper of `fakeReact`,
* src/test/hookHarness.ts).
*/
function markup(consentOpen = false): string {
if (consentOpen) harness.slots[0] = true;
return renderToStaticMarkup(
harness.render(
() =>
PriceFetchControl({
symbol: "AAPL",
date: "2026-04-25",
categoryKind: "priced",
assetType: "stock",
onPriceFetched: vi.fn(),
}) as ReactNode,
),
);
}
it("names the fetch button and the stock best-effort notice through t", () => {
const html = markup();
expect(html).toContain('aria-label="t(balance.priceFetching.button)"');
expect(html).toContain(">t(balance.priceFetching.button)<");
expect(html).toContain(">t(balance.priceFetching.bestEffortNotice)<");
expect(html).toContain(">t(balance.priceFetching.bestEffortBadge)<");
expect(html).not.toContain(">best-effort<");
expect(html).toContain('aria-label="t(common.close)"');
});
it("names the consent dialog through t once it is open (#437)", () => {
expect(markup()).not.toContain('role="dialog"');
harness.clear();
const html = markup(true);
expect(html).toContain('role="dialog"');
expect(html).toContain(">t(balance.priceFetching.consent.title)<");
expect(html).toContain(">t(balance.priceFetching.consent.body)<");
expect(html).toContain(">t(balance.priceFetching.consent.decline)<");
expect(html).toContain(">t(balance.priceFetching.consent.accept)<");
});
});