public-goods-random.integration.test.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import { describe, it, expect, beforeEach } from 'vitest';
  2. import { testClient } from 'hono/testing';
  3. import { IntegrationTestDatabase, setupIntegrationDatabaseHooksWithEntities } from '@d8d/shared-test-util';
  4. import { JWTUtil } from '@d8d/shared-utils';
  5. import { UserEntityMt, RoleMt } from '@d8d/user-module-mt';
  6. import { FileMt } from '@d8d/file-module-mt';
  7. import { SupplierMt } from '@d8d/supplier-module-mt';
  8. import { MerchantMt } from '@d8d/merchant-module-mt';
  9. import { publicGoodsRandomRoutesMt } from '../../src/routes/public-goods-random.mt';
  10. import { GoodsMt, GoodsCategoryMt } from '../../src/entities/index.mt';
  11. import { GoodsTestFactory } from '../factories/goods-test-factory';
  12. // 设置集成测试钩子
  13. setupIntegrationDatabaseHooksWithEntities([
  14. UserEntityMt, RoleMt, GoodsMt, GoodsCategoryMt, FileMt, SupplierMt, MerchantMt
  15. ])
  16. describe('公开随机商品API集成测试', () => {
  17. let client: ReturnType<typeof testClient<typeof publicGoodsRandomRoutesMt>>;
  18. let testUser: UserEntityMt;
  19. let testSupplier: SupplierMt;
  20. let testMerchant: MerchantMt;
  21. let testCategory1: GoodsCategoryMt;
  22. let testCategory2: GoodsCategoryMt;
  23. let testFactory: GoodsTestFactory;
  24. beforeEach(async () => {
  25. // 创建测试客户端
  26. client = testClient(publicGoodsRandomRoutesMt);
  27. // 获取数据源并创建测试工厂
  28. const dataSource = await IntegrationTestDatabase.getDataSource();
  29. testFactory = new GoodsTestFactory(dataSource);
  30. // 使用测试工厂创建测试数据
  31. testUser = await testFactory.createTestUser();
  32. testSupplier = await testFactory.createTestSupplier(testUser.id);
  33. testMerchant = await testFactory.createTestMerchant(testUser.id);
  34. testCategory1 = await testFactory.createTestCategory(testUser.id, { name: '测试分类1' });
  35. testCategory2 = await testFactory.createTestCategory(testUser.id, { name: '测试分类2' });
  36. // 创建测试商品
  37. // 创建可用状态的商品
  38. const activeGoods1 = await testFactory.createTestGoods(testUser.id, {
  39. name: '可用商品1',
  40. price: 100.00,
  41. costPrice: 80.00,
  42. categoryId1: testCategory1.id,
  43. categoryId2: testCategory1.id,
  44. categoryId3: testCategory1.id,
  45. supplierId: testSupplier.id,
  46. merchantId: testMerchant.id,
  47. state: 1,
  48. stock: 100
  49. });
  50. const activeGoods2 = await testFactory.createTestGoods(testUser.id, {
  51. name: '可用商品2',
  52. price: 200.00,
  53. costPrice: 160.00,
  54. categoryId1: testCategory2.id,
  55. categoryId2: testCategory2.id,
  56. categoryId3: testCategory2.id,
  57. supplierId: testSupplier.id,
  58. merchantId: testMerchant.id,
  59. state: 1,
  60. stock: 50
  61. });
  62. const activeGoods3 = await testFactory.createTestGoods(testUser.id, {
  63. name: '可用商品3',
  64. price: 300.00,
  65. costPrice: 240.00,
  66. categoryId1: testCategory1.id,
  67. categoryId2: testCategory1.id,
  68. categoryId3: testCategory1.id,
  69. goodsType: 2,
  70. supplierId: testSupplier.id,
  71. merchantId: testMerchant.id,
  72. state: 1,
  73. stock: 30
  74. });
  75. // 创建不可用状态的商品(不应该被随机查询返回)
  76. const inactiveGoods = await testFactory.createTestGoods(testUser.id, {
  77. name: '不可用商品',
  78. price: 400.00,
  79. costPrice: 320.00,
  80. categoryId1: testCategory1.id,
  81. categoryId2: testCategory1.id,
  82. categoryId3: testCategory1.id,
  83. supplierId: testSupplier.id,
  84. merchantId: testMerchant.id,
  85. state: 2,
  86. stock: 10
  87. });
  88. });
  89. describe('GET /goods/random', () => {
  90. it('应该返回随机商品列表', async () => {
  91. const response = await client.index.$get({
  92. query: {}
  93. });
  94. console.debug('随机商品列表响应状态:', response.status);
  95. expect(response.status).toBe(200);
  96. if (response.status === 200) {
  97. const data = await response.json();
  98. expect(data).toHaveProperty('data');
  99. expect(Array.isArray(data.data)).toBe(true);
  100. // 验证只返回可用状态的商品
  101. data.data.forEach((goods: any) => {
  102. expect(goods.state).toBe(1);
  103. });
  104. }
  105. });
  106. it('应该支持按分类过滤', async () => {
  107. const response = await client.index.$get({
  108. query: { categoryId: testCategory1.id }
  109. });
  110. expect(response.status).toBe(200);
  111. if (response.status === 200) {
  112. const data = await response.json();
  113. expect(Array.isArray(data.data)).toBe(true);
  114. // 验证只返回指定分类的商品
  115. data.data.forEach((goods: any) => {
  116. expect(goods.categoryId1).toBe(testCategory1.id);
  117. expect(goods.state).toBe(1);
  118. });
  119. }
  120. });
  121. it('应该支持限制返回数量', async () => {
  122. const response = await client.index.$get({
  123. query: { limit: 2 }
  124. });
  125. expect(response.status).toBe(200);
  126. if (response.status === 200) {
  127. const data = await response.json();
  128. expect(Array.isArray(data.data)).toBe(true);
  129. expect(data.data.length).toBeLessThanOrEqual(2);
  130. // 验证只返回可用状态的商品
  131. data.data.forEach((goods: any) => {
  132. expect(goods.state).toBe(1);
  133. });
  134. }
  135. });
  136. it('应该支持包含图片选项', async () => {
  137. const response = await client.index.$get({
  138. query: { includeImages: true }
  139. });
  140. expect(response.status).toBe(200);
  141. if (response.status === 200) {
  142. const data = await response.json();
  143. expect(Array.isArray(data.data)).toBe(true);
  144. // 验证返回的商品数据包含图片关联信息
  145. data.data.forEach((goods: any) => {
  146. expect(goods.state).toBe(1);
  147. // 注意:实际图片数据需要先创建文件实体才能测试
  148. });
  149. }
  150. });
  151. it('应该正确处理不存在的分类', async () => {
  152. const response = await client.index.$get({
  153. query: { categoryId: 999999 }
  154. });
  155. expect(response.status).toBe(200);
  156. if (response.status === 200) {
  157. const data = await response.json();
  158. expect(Array.isArray(data.data)).toBe(true);
  159. // 不存在的分类应该返回空数组
  160. expect(data.data.length).toBe(0);
  161. }
  162. });
  163. });
  164. describe('随机商品关联关系测试', () => {
  165. it('应该正确加载商品关联关系', async () => {
  166. const response = await client.index.$get({
  167. query: {}
  168. });
  169. expect(response.status).toBe(200);
  170. if (response.status === 200) {
  171. const data = await response.json();
  172. expect(Array.isArray(data.data)).toBe(true);
  173. // 验证至少返回一个商品
  174. if (data.data.length > 0) {
  175. const goods = data.data[0];
  176. // 验证基础字段
  177. expect(goods).toHaveProperty('id');
  178. expect(goods).toHaveProperty('name');
  179. expect(goods).toHaveProperty('price');
  180. expect(goods).toHaveProperty('state', 1);
  181. // 验证关联关系字段存在
  182. expect(goods).toHaveProperty('category1');
  183. expect(goods).toHaveProperty('supplier');
  184. expect(goods).toHaveProperty('merchant');
  185. }
  186. }
  187. });
  188. });
  189. describe('随机商品排序测试', () => {
  190. it('应该返回随机排序的商品', async () => {
  191. // 多次请求验证排序的随机性
  192. const responses = await Promise.all([
  193. client.index.$get({ query: {} }),
  194. client.index.$get({ query: {} }),
  195. client.index.$get({ query: {} })
  196. ]);
  197. // 验证所有请求都成功
  198. responses.forEach((response: any) => {
  199. expect(response.status).toBe(200);
  200. });
  201. // 提取所有商品ID
  202. const allGoodsIds: number[] = [];
  203. for (const response of responses) {
  204. if (response.status === 200) {
  205. const data = await response.json();
  206. data.data.forEach((goods: any) => {
  207. allGoodsIds.push(goods.id);
  208. });
  209. }
  210. }
  211. // 验证返回了商品(可能有重复,因为是随机)
  212. expect(allGoodsIds.length).toBeGreaterThan(0);
  213. });
  214. });
  215. });