82 lines
3.0 KiB
TypeScript
82 lines
3.0 KiB
TypeScript
import { ProductStore } from '../src/catalog/product.store';
|
|
import { VariantStore } from '../src/catalog/variant.store';
|
|
import { AddressStore } from '../src/addresses/address.store';
|
|
import { StockStore } from '../src/inventory/stock.store';
|
|
import { AccessStore } from '../src/identity/access.store';
|
|
import { DatabaseService } from '../src/database/database.service';
|
|
import type { Principal } from '../src/identity/identity.types';
|
|
import { addressInput, variantInput } from './helpers/commerce';
|
|
|
|
describe('commerce use-case constraints', () => {
|
|
const actor: Principal = {
|
|
userId: 'user',
|
|
organizationId: 'org',
|
|
sessionId: 'session',
|
|
permissions: [],
|
|
};
|
|
const tx = {
|
|
product: { findFirst: jest.fn() },
|
|
catalogGroup: { count: jest.fn() },
|
|
productVariant: { count: jest.fn(), findFirst: jest.fn() },
|
|
address: { count: jest.fn(), findFirst: jest.fn() },
|
|
warehouse: { findFirst: jest.fn() },
|
|
};
|
|
const access = { mutate: jest.fn() };
|
|
beforeEach(() => {
|
|
jest.resetAllMocks();
|
|
access.mutate.mockImplementation((_actor, _permission, work) =>
|
|
work(tx, actor),
|
|
);
|
|
tx.product.findFirst.mockResolvedValue({ id: 'product', status: 'DRAFT' });
|
|
});
|
|
it('rejects group links outside the organization', async () => {
|
|
tx.catalogGroup.count.mockResolvedValue(0);
|
|
await expect(
|
|
new ProductStore(
|
|
{} as DatabaseService,
|
|
access as unknown as AccessStore,
|
|
).save(actor, {
|
|
name: 'Product',
|
|
slug: 'product',
|
|
description: '',
|
|
groupIds: ['foreign'],
|
|
}),
|
|
).rejects.toThrow('Catalogue group not found');
|
|
});
|
|
it('limits variants and hides unknown variant IDs', async () => {
|
|
const store = new VariantStore(access as unknown as AccessStore);
|
|
tx.productVariant.count.mockResolvedValue(100);
|
|
await expect(store.save(actor, 'product', variantInput)).rejects.toThrow(
|
|
'variant limit',
|
|
);
|
|
tx.productVariant.findFirst.mockResolvedValue(null);
|
|
await expect(
|
|
store.save(actor, 'product', variantInput, 'foreign'),
|
|
).rejects.toThrow('variant not found');
|
|
});
|
|
it('limits user address count', async () => {
|
|
tx.address.count.mockResolvedValue(20);
|
|
await expect(
|
|
new AddressStore(
|
|
{} as DatabaseService,
|
|
access as unknown as AccessStore,
|
|
).save(actor, addressInput),
|
|
).rejects.toThrow('Address limit');
|
|
});
|
|
it('rejects foreign warehouses and variants before creating stock', async () => {
|
|
const store = new StockStore(
|
|
{} as DatabaseService,
|
|
access as unknown as AccessStore,
|
|
);
|
|
tx.warehouse.findFirst.mockResolvedValue(null);
|
|
await expect(
|
|
store.create(actor, { warehouseId: 'bad', variantId: 'variant' }),
|
|
).rejects.toThrow('Warehouse not found');
|
|
tx.warehouse.findFirst.mockResolvedValue({ id: 'warehouse' });
|
|
tx.productVariant.findFirst.mockResolvedValue(null);
|
|
await expect(
|
|
store.create(actor, { warehouseId: 'warehouse', variantId: 'bad' }),
|
|
).rejects.toThrow('variant not found');
|
|
});
|
|
});
|