manicanldes-backend/test/cart-recovery.spec.ts

240 lines
7.2 KiB
TypeScript

import { identityApp, type IdentityApp } from './helpers/identity-app';
import { checkoutFixture } from './helpers/checkout';
import { secondActor } from './helpers/commerce';
describe('private idle cart recovery', () => {
let ctx: IdentityApp;
beforeAll(async () => {
ctx = await identityApp();
});
afterAll(async () => {
await ctx.close();
});
beforeEach(async () => {
await ctx.clearLimits();
});
const idle = async (userId: string) => {
await ctx.db.cart.updateMany({
where: { userId },
data: { lastActivityAt: new Date(Date.now() - 25 * 3600000) },
});
};
it('discovers retained carts privately without refreshing activity on reads', async () => {
const f = await checkoutFixture(ctx);
const read = () =>
ctx
.api()
.get('/api/v1/cart/recovery')
.auth(f.actor.token, { type: 'bearer' });
expect((await read().expect(200)).body.recoverable).toBe(false);
await idle(f.actor.userId);
await ctx.db.productVariant.update({
where: { id: f.variant.id },
data: { price: '549.25' },
});
const result = await read().expect(200);
expect(result.body).toMatchObject({
recoverable: true,
version: 1,
lines: [
{
variantId: f.variant.id,
quantity: 2,
available: true,
variant: { price: '549.25', currency: 'INR' },
},
],
});
await ctx
.api()
.get('/api/v1/cart')
.auth(f.actor.token, { type: 'bearer' })
.expect(200);
expect((await read().expect(200)).body.lastActivityAt).toBe(
result.body.lastActivityAt,
);
for (const sameOrganization of [true, false]) {
const other = await secondActor(ctx, sameOrganization);
expect(
(
await ctx
.api()
.get('/api/v1/cart/recovery')
.auth(other.token, { type: 'bearer' })
.expect(200)
).body,
).toEqual({
recoverable: false,
version: 0,
lastActivityAt: null,
lines: [],
});
}
await ctx.api().get('/api/v1/cart/recovery').expect(401);
await ctx
.api()
.post('/api/v1/cart/recovery')
.send({ version: 1 })
.expect(401);
});
it('resumes once at the reviewed version and records an audit', async () => {
const f = await checkoutFixture(ctx);
const resume = (body: object) =>
ctx
.api()
.post('/api/v1/cart/recovery')
.auth(f.actor.token, { type: 'bearer' })
.send(body);
expect((await resume({ version: 1 }).expect(409)).body.code).toBe(
'CART_NOT_RECOVERABLE',
);
await idle(f.actor.userId);
expect((await resume({ version: 0 }).expect(409)).body.code).toBe(
'CART_CHANGED',
);
await resume({ version: 1, userId: ctx.owner.userId }).expect(400);
expect((await resume({ version: 1 }).expect(201)).body).toEqual({
version: 2,
removedVariantIds: [],
});
expect((await resume({ version: 1 }).expect(409)).body.code).toBe(
'CART_CHANGED',
);
const cart = await ctx
.api()
.get('/api/v1/cart')
.auth(f.actor.token, { type: 'bearer' })
.expect(200);
expect(cart.body).toMatchObject({ version: 2, lines: [{ quantity: 2 }] });
expect(
await ctx.db.auditEvent.count({
where: { actorId: f.actor.userId, action: 'cart.recovered' },
}),
).toBe(1);
});
it('redacts unavailable merchandise and removes it only on explicit resume', async () => {
const f = await checkoutFixture(ctx);
await idle(f.actor.userId);
await ctx.db.productVariant.update({
where: { id: f.variant.id },
data: { active: false },
});
const result = await ctx
.api()
.get('/api/v1/cart/recovery')
.auth(f.actor.token, { type: 'bearer' })
.expect(200);
expect(result.body.lines).toEqual([
{ variantId: f.variant.id, quantity: 2, available: false, variant: null },
]);
const resumed = await ctx
.api()
.post('/api/v1/cart/recovery')
.auth(f.actor.token, { type: 'bearer' })
.send({ version: 1 })
.expect(201);
expect(resumed.body).toEqual({
version: 2,
removedVariantIds: [f.variant.id],
});
expect(
(
await ctx
.api()
.get('/api/v1/cart/recovery')
.auth(f.actor.token, { type: 'bearer' })
.expect(200)
).body.recoverable,
).toBe(false);
});
it('editing or checking out ends recovery eligibility', async () => {
const f = await checkoutFixture(ctx);
await idle(f.actor.userId);
await ctx
.api()
.put('/api/v1/cart/lines/' + f.variant.id)
.auth(f.actor.token, { type: 'bearer' })
.send({ version: 1, quantity: 1 })
.expect(200);
expect(
(
await ctx
.api()
.get('/api/v1/cart/recovery')
.auth(f.actor.token, { type: 'bearer' })
.expect(200)
).body.recoverable,
).toBe(false);
await idle(f.actor.userId);
await ctx
.api()
.post('/api/v1/checkout')
.auth(f.actor.token, { type: 'bearer' })
.send({ ...f.input, cartVersion: 2 })
.expect(201);
const result = await ctx
.api()
.get('/api/v1/cart/recovery')
.auth(f.actor.token, { type: 'bearer' })
.expect(200);
expect(result.body).toMatchObject({
recoverable: false,
version: 3,
lines: [],
});
expect(Date.now() - Date.parse(result.body.lastActivityAt)).toBeLessThan(
60000,
);
});
it('serializes competing recovery commands and leaves undated carts alone', async () => {
const f = await checkoutFixture(ctx);
await ctx.db.cart.updateMany({
where: { userId: f.actor.userId },
data: { lastActivityAt: null },
});
const resume = () =>
ctx
.api()
.post('/api/v1/cart/recovery')
.auth(f.actor.token, { type: 'bearer' })
.send({ version: 1 });
expect((await resume().expect(409)).body.code).toBe('CART_NOT_RECOVERABLE');
const other = await secondActor(ctx);
await ctx
.api()
.post('/api/v1/cart/recovery')
.auth(other.token, { type: 'bearer' })
.send({ version: 1 })
.expect(409);
await idle(f.actor.userId);
const results = await Promise.all([resume(), resume()]);
expect(results.map((r) => r.status).sort()).toEqual([201, 409]);
expect(
await ctx.db.auditEvent.count({
where: { actorId: f.actor.userId, action: 'cart.recovered' },
}),
).toBe(1);
});
it('rechecks product publication at resume even after a successful preview', async () => {
const f = await checkoutFixture(ctx);
await idle(f.actor.userId);
const preview = await ctx
.api()
.get('/api/v1/cart/recovery')
.auth(f.actor.token, { type: 'bearer' })
.expect(200);
expect(preview.body.lines[0].available).toBe(true);
await ctx.db.product.update({
where: { id: f.product.id },
data: { status: 'DRAFT' },
});
const resumed = await ctx
.api()
.post('/api/v1/cart/recovery')
.auth(f.actor.token, { type: 'bearer' })
.send({ version: 1 })
.expect(201);
expect(resumed.body.removedVariantIds).toEqual([f.variant.id]);
});
});