2026-05-01 23:51:05 +02:00
|
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
2026-04-10 22:08:01 +02:00
|
|
|
|
2026-05-01 23:51:05 +02:00
|
|
|
const apiRequestMock = vi.fn();
|
|
|
|
|
|
|
|
|
|
vi.mock('../../src/api/client.js', () => ({
|
|
|
|
|
getPath(key) {
|
|
|
|
|
const paths = {
|
|
|
|
|
items: 'kitchen/items',
|
|
|
|
|
};
|
|
|
|
|
return paths[key];
|
|
|
|
|
},
|
|
|
|
|
apiRequest: (...args) => apiRequestMock(...args),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
formatPrintErrorMessage,
|
|
|
|
|
getItemLabel,
|
|
|
|
|
previewLabel,
|
|
|
|
|
} = await import('../../src/api/labels.js');
|
|
|
|
|
|
|
|
|
|
describe('api/labels', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
apiRequestMock.mockReset();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('previewLabel uses boolean label/preview query flags', async () => {
|
|
|
|
|
apiRequestMock.mockResolvedValueOnce({
|
|
|
|
|
label: 'YWJj',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const response = await previewLabel({ config: { database: 'db' } }, { name: 'Rice' });
|
|
|
|
|
|
|
|
|
|
expect(apiRequestMock).toHaveBeenCalledWith(
|
|
|
|
|
{ config: { database: 'db' } },
|
|
|
|
|
'kitchen/items',
|
|
|
|
|
{
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: { name: 'Rice' },
|
|
|
|
|
accept: 'image/svg+xml, image/png, application/json',
|
|
|
|
|
query: { label: true, preview: true },
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
expect(response).toEqual({
|
|
|
|
|
objectUrl: 'data:image/png;base64,YWJj',
|
|
|
|
|
contentType: 'image/png',
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('getItemLabel fetches PNG from /label endpoint', async () => {
|
|
|
|
|
apiRequestMock.mockResolvedValueOnce({
|
|
|
|
|
label: 'YWJj',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const response = await getItemLabel({ config: { database: 'db' } }, 'item-1');
|
|
|
|
|
|
|
|
|
|
expect(apiRequestMock).toHaveBeenCalledWith(
|
|
|
|
|
{ config: { database: 'db' } },
|
|
|
|
|
'kitchen/items/item-1/label',
|
|
|
|
|
{
|
|
|
|
|
method: 'GET',
|
|
|
|
|
accept: 'image/png, application/json',
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
expect(response).toEqual({
|
|
|
|
|
objectUrl: 'data:image/png;base64,YWJj',
|
|
|
|
|
contentType: 'image/png',
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-04-10 22:08:01 +02:00
|
|
|
|
|
|
|
|
describe('api/labels formatPrintErrorMessage', () => {
|
|
|
|
|
it('maps printer_unavailable payload to user-friendly message', () => {
|
|
|
|
|
const message = formatPrintErrorMessage({
|
|
|
|
|
status: 503,
|
|
|
|
|
payload: {
|
|
|
|
|
code: 'printer_unavailable',
|
|
|
|
|
message: 'Backend says unavailable',
|
|
|
|
|
details: { printer: 'Office Zebra' },
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(message).toBe('Printer is unavailable. (printer: Office Zebra)');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('falls back to generic message when payload is missing', () => {
|
|
|
|
|
const message = formatPrintErrorMessage(new Error('Something failed'));
|
|
|
|
|
expect(message).toBe('Something failed');
|
|
|
|
|
});
|
|
|
|
|
});
|