wooClient.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { test } from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { applyTestConfig } from './helpers/testConfig.js';
  4. import { WooClient } from '../src/clients/wooClient.js';
  5. import { RelayError } from '../src/errors.js';
  6. /**
  7. * Exercises the order-number fallback path against a stubbed fetch, because the
  8. * fuzzy behaviour of Woo's `search` is exactly what used to make a bogus order
  9. * number look like somebody else's order.
  10. */
  11. function stubFetch(handler: (url: string) => { status: number; body: unknown }) {
  12. const original = globalThis.fetch;
  13. globalThis.fetch = (async (input: string | URL | Request) => {
  14. const url = typeof input === 'string' ? input : input.toString();
  15. const { status, body } = handler(url);
  16. return new Response(JSON.stringify(body), {
  17. status,
  18. headers: { 'Content-Type': 'application/json' },
  19. });
  20. }) as typeof globalThis.fetch;
  21. return () => {
  22. globalThis.fetch = original;
  23. };
  24. }
  25. test('a direct order fetch is returned as-is', async () => {
  26. applyTestConfig();
  27. const restore = stubFetch(() => ({ status: 200, body: { id: 555, number: '555' } }));
  28. try {
  29. const order = await new WooClient().getOrder({ orderNumber: '555' });
  30. assert.equal(order.id, 555);
  31. } finally {
  32. restore();
  33. }
  34. });
  35. test('the search fallback accepts only an exact order-number match', async () => {
  36. applyTestConfig();
  37. const restore = stubFetch((url) => {
  38. if (url.includes('/orders/12345')) return { status: 404, body: { message: 'nope' } };
  39. // Woo's fuzzy search returns an unrelated order.
  40. return { status: 200, body: [{ id: 987, number: '987' }] };
  41. });
  42. try {
  43. await new WooClient().getOrder({ orderNumber: '12345' });
  44. assert.fail('expected ORDER_NOT_FOUND');
  45. } catch (err) {
  46. assert.ok(err instanceof RelayError);
  47. assert.equal(err.code, 'ORDER_NOT_FOUND');
  48. } finally {
  49. restore();
  50. }
  51. });
  52. test('the search fallback returns a genuine custom-order-number match', async () => {
  53. applyTestConfig();
  54. const restore = stubFetch((url) => {
  55. if (url.includes('/orders/EK-2026-77')) return { status: 404, body: {} };
  56. return { status: 200, body: [{ id: 42, number: 'EK-2026-77' }, { id: 43, number: 'EK-2026-78' }] };
  57. });
  58. try {
  59. const order = await new WooClient().getOrder({ orderNumber: 'EK-2026-77' });
  60. assert.equal(order.id, 42);
  61. } finally {
  62. restore();
  63. }
  64. });
  65. test('a hidden product is filtered out of search results', async () => {
  66. applyTestConfig();
  67. const restore = stubFetch(() => ({
  68. status: 200,
  69. body: [
  70. { id: 1, name: 'Widoczny', catalog_visibility: 'visible' },
  71. { id: 2, name: 'Ukryty', catalog_visibility: 'hidden' },
  72. ],
  73. }));
  74. try {
  75. const products = await new WooClient().searchProducts('gaz', 5, 'PLN', 'pl');
  76. assert.equal(products.length, 1);
  77. assert.equal(products[0]?.id, 1);
  78. } finally {
  79. restore();
  80. }
  81. });
  82. test('an unpublished product is not returned by id', async () => {
  83. applyTestConfig();
  84. const restore = stubFetch(() => ({ status: 200, body: { id: 9, status: 'draft' } }));
  85. try {
  86. await new WooClient().getProduct({ productId: 9 });
  87. assert.fail('expected PRODUCT_NOT_FOUND');
  88. } catch (err) {
  89. assert.ok(err instanceof RelayError);
  90. assert.equal(err.code, 'PRODUCT_NOT_FOUND');
  91. } finally {
  92. restore();
  93. }
  94. });