88 lines
2.5 KiB
JavaScript
88 lines
2.5 KiB
JavaScript
import { describe, expect, it, vi } from 'vitest';
|
|
|
|
const lookupItemByIdentifierMock = vi.fn();
|
|
|
|
vi.mock('../../../src/api/stock.js', () => ({
|
|
applyItemUpsert: vi.fn(),
|
|
previewItemUpsert: vi.fn(),
|
|
searchItemDefinitions: vi.fn(async () => []),
|
|
lookupItemByIdentifier: (...args) => lookupItemByIdentifierMock(...args),
|
|
}));
|
|
|
|
vi.mock('../../../src/api/labels.js', () => ({
|
|
previewLabel: vi.fn(async () => ({ objectUrl: 'blob:preview' })),
|
|
printItemLabel: vi.fn(async () => null),
|
|
formatPrintErrorMessage: (error) => error?.message || 'Printing failed.',
|
|
}));
|
|
|
|
vi.mock('../../../src/api/locations.js', () => ({
|
|
fetchLocations: vi.fn(async () => ({ flat: [], tree: [] })),
|
|
}));
|
|
|
|
const { labelCreatePageData } = await import('../../../src/features/labels/label-create-page.js');
|
|
|
|
describe('label identifier lookup feedback', () => {
|
|
it('shows retry hint for rate-limited lookup responses', () => {
|
|
const data = labelCreatePageData({
|
|
isConnected: false,
|
|
activeKitchen: { id: 1 },
|
|
addAlert: vi.fn(),
|
|
});
|
|
|
|
const message = data.lookupStatusMessageWithDetails(
|
|
{ status: 'rate_limited', retryAfterSeconds: 30 },
|
|
'3830012345678',
|
|
);
|
|
|
|
expect(message).toContain('rate-limited');
|
|
expect(message).toContain('Retry in 30s');
|
|
});
|
|
|
|
it('builds metadata-aware success message with source/cache/freshness context', () => {
|
|
const data = labelCreatePageData({
|
|
isConnected: false,
|
|
activeKitchen: { id: 1 },
|
|
addAlert: vi.fn(),
|
|
});
|
|
|
|
const message = data.lookupSuccessMessage({
|
|
source: 'openfoodfacts',
|
|
cacheHit: true,
|
|
staleCache: true,
|
|
payloadFetchedAt: '2026-04-11T09:00:00Z',
|
|
});
|
|
|
|
expect(message).toContain('OpenFoodFacts');
|
|
expect(message).toContain('cache hit');
|
|
expect(message).toContain('stale cache');
|
|
expect(message).toContain('fetched:');
|
|
});
|
|
|
|
it('applies non-ok lookup status as warning message with details', async () => {
|
|
lookupItemByIdentifierMock.mockResolvedValueOnce({
|
|
status: 'rate_limited',
|
|
retryAfterSeconds: 45,
|
|
source: 'openfoodfacts',
|
|
cacheHit: false,
|
|
staleCache: false,
|
|
item: null,
|
|
});
|
|
|
|
const addAlert = vi.fn();
|
|
const data = labelCreatePageData({
|
|
isConnected: false,
|
|
activeKitchen: { id: 1 },
|
|
addAlert,
|
|
});
|
|
data.form.identifierCode = '3830012345678';
|
|
|
|
await data.lookupIdentifierDetails();
|
|
|
|
expect(data.lookupState.error).toContain('Retry in 45s');
|
|
expect(addAlert).toHaveBeenCalledWith({
|
|
type: 'warning',
|
|
message: data.lookupState.error,
|
|
});
|
|
});
|
|
});
|