agmission/Development/server/tests/integration/mock_data.js
2026-04-29 09:40:51 -04:00

226 lines
5.9 KiB
JavaScript

'use strict';
/**
* Shared mock data factories for integration tests.
* All ids are fresh ObjectIds so tests are fully isolated.
*/
const mongoose = require('mongoose');
const newId = () => new mongoose.Types.ObjectId();
// ---------------------------------------------------------------------------
// Applicator / Customer (kind = "1")
// ---------------------------------------------------------------------------
function mockApplicator(overrides = {}) {
return {
_id: newId(),
kind: '1',
username: `applicator_${Date.now()}@test.com`,
password: '$2a$10$hashedpassword',
name: 'Test Applicator Co.',
email: `applicator_${Date.now()}@test.com`,
phone: '+15550001000',
address: '123 Main St',
country: 'US',
active: true,
billable: true,
premium: 0,
selfSignup: false,
markedDelete: false,
...overrides,
};
}
// ---------------------------------------------------------------------------
// Client (kind = "3")
// ---------------------------------------------------------------------------
function mockClient(parentId, overrides = {}) {
return {
_id: newId(),
kind: '3',
username: `client_${Date.now()}@test.com`,
name: 'Test Client',
email: `client_${Date.now()}@test.com`,
phone: '+15550002000',
address: '456 Oak Ave',
country: 'US',
active: true,
parent: parentId,
markedDelete: false,
...overrides,
};
}
// ---------------------------------------------------------------------------
// Pilot (kind = "5")
// ---------------------------------------------------------------------------
function mockPilot(parentId, overrides = {}) {
return {
_id: newId(),
kind: '5',
username: `pilot_${Date.now()}@test.com`,
name: 'Test Pilot',
email: `pilot_${Date.now()}@test.com`,
active: true,
parent: parentId,
markedDelete: false,
...overrides,
};
}
// ---------------------------------------------------------------------------
// Vehicle / Device (kind = "9")
// ---------------------------------------------------------------------------
function mockVehicle(parentId, overrides = {}) {
return {
_id: newId(),
kind: '9',
name: `Aircraft-${Date.now()}`,
model: 'AgNav TestBird',
color: 'yellow',
vehicleType: 0,
active: true,
tracking: false,
pkgActive: false,
parent: parentId,
markedDelete: false,
...overrides,
};
}
// ---------------------------------------------------------------------------
// Product
// ---------------------------------------------------------------------------
function mockProduct(puid, overrides = {}) {
return {
name: `Product-${Date.now()}`,
type: 1, // ACTIVE
restricted: false,
epaReg: `EPA-${Date.now()}`,
desc: 'Integration test product',
rate: { value: 2.5, unit: 1 }, // GAL
byPuid: puid,
...overrides,
};
}
// ---------------------------------------------------------------------------
// Crop
// ---------------------------------------------------------------------------
function mockCrop(puid, overrides = {}) {
return {
name: `Crop-${Date.now()}`,
color: '#00AA00',
desc: 'Integration test crop',
byPuid: puid,
...overrides,
};
}
// ---------------------------------------------------------------------------
// Job
// ---------------------------------------------------------------------------
function mockJob(puid, overrides = {}) {
return {
name: `Job-${Date.now()}`,
orderNumber: `ORD-${Date.now()}`,
measureUnit: false, // false = acres
swathWidth: 60,
byPuid: puid,
status: 0, // PENDING
markedDelete: false,
...overrides,
};
}
// ---------------------------------------------------------------------------
// Costing Item
// ---------------------------------------------------------------------------
function mockCostingItem(puid, userId, overrides = {}) {
return {
name: `CostItem-${Date.now()}`,
type: 0,
unit: 1, // GAL
price: 10.5,
byPuid: puid,
createdBy: userId,
updatedBy: userId,
...overrides,
};
}
// ---------------------------------------------------------------------------
// Invoice Setting
// ---------------------------------------------------------------------------
function mockInvoiceSetting(puid, userId, overrides = {}) {
return {
byPuid: puid,
userId,
companyName: 'Test Applicator Co.',
address: '123 Main St, Testville, TX',
taxValue: 0,
discount: 0,
termOpts: [15, 30, 60],
paymentTerm: 30,
currency: 'USD',
note: 'Integration test invoice setting',
logo: '',
createdBy: userId,
updatedBy: userId,
...overrides,
};
}
// ---------------------------------------------------------------------------
// Request / response mocks used for controller unit-style tests
// ---------------------------------------------------------------------------
/**
* Build a minimal Express-like req object for an authenticated applicator.
*/
function mockReq({ uid, puid, ut = '1', body = {}, params = {}, query = {}, userInfo = null } = {}) {
const _uid = uid ? String(uid) : String(new mongoose.Types.ObjectId());
const _puid = puid ? String(puid) : _uid;
return {
uid: _uid,
ut,
userInfo: userInfo || { puid: _puid, kind: ut, premium: 0, membership: null, markedDelete: false },
body,
params,
query,
headers: {},
file: undefined,
};
}
/**
* Build a minimal Express-like res object with jest spy functions.
*/
function mockRes() {
const res = {
statusCode: 200,
_data: undefined,
};
res.json = jest.fn((data) => { res._data = data; return res; });
res.send = jest.fn((data) => { res._data = data; return res; });
res.status = jest.fn((code) => { res.statusCode = code; return res; });
res.end = jest.fn(() => res);
return res;
}
module.exports = {
newId,
mockApplicator,
mockClient,
mockPilot,
mockVehicle,
mockProduct,
mockCrop,
mockJob,
mockCostingItem,
mockInvoiceSetting,
mockReq,
mockRes,
};