platform-adapter-auth.contract.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Contract test for IPlatformAdapter.prepareRequest()
  3. *
  4. * This test validates the platform adapter authentication functionality
  5. * according to the contract specification. Tests MUST FAIL until implementation is complete.
  6. */
  7. import { describe, test, expect, beforeEach, jest } from '@jest/globals'
  8. // Import types that will be implemented
  9. import type { IPlatformAdapter, PlatformRequest, PreparedRequest } from '@/types/platformAdapter'
  10. describe('IPlatformAdapter.prepareRequest() Contract', () => {
  11. let adapter: IPlatformAdapter
  12. beforeEach(() => {
  13. // This will fail until platform adapters are implemented
  14. // eslint-disable-next-line @typescript-eslint/no-require-imports
  15. const { PacificaHttpAdapter } = require('@/adapters/pacifica/PacificaHttpAdapter')
  16. adapter = new PacificaHttpAdapter({
  17. platform: 'pacifica',
  18. baseUrl: 'https://api.pacifica.fi',
  19. authConfig: { type: 'signature', algorithm: 'Ed25519' }
  20. })
  21. })
  22. describe('Request Preparation', () => {
  23. test('should prepare authenticated request with signature', async () => {
  24. const request: PlatformRequest = {
  25. accountId: 'auth-account',
  26. method: 'POST',
  27. path: '/api/v1/orders',
  28. body: { symbol: 'BTC/USDT', side: 'buy', amount: '0.001' },
  29. options: { requiresAuth: true }
  30. }
  31. const prepared: PreparedRequest = await adapter.prepareRequest(request)
  32. expect(prepared).toBeDefined()
  33. expect(prepared.headers).toBeDefined()
  34. expect(prepared.headers['Authorization']).toBeDefined()
  35. expect(prepared.headers['X-Timestamp']).toBeDefined()
  36. expect(prepared.headers['X-Signature']).toBeDefined()
  37. expect(prepared.authenticated).toBe(true)
  38. expect(prepared.signatureAlgorithm).toBe('Ed25519')
  39. })
  40. test('should prepare public request without authentication', async () => {
  41. const request: PlatformRequest = {
  42. accountId: 'any-account',
  43. method: 'GET',
  44. path: '/api/v1/markets',
  45. options: { requiresAuth: false }
  46. }
  47. const prepared: PreparedRequest = await adapter.prepareRequest(request)
  48. expect(prepared).toBeDefined()
  49. expect(prepared.authenticated).toBe(false)
  50. expect(prepared.headers['Authorization']).toBeUndefined()
  51. expect(prepared.headers['X-Signature']).toBeUndefined()
  52. })
  53. test('should integrate with credential-manager for signing', async () => {
  54. const request: PlatformRequest = {
  55. accountId: 'credential-account',
  56. method: 'POST',
  57. path: '/api/v1/orders',
  58. body: { test: 'data' },
  59. options: { requiresAuth: true }
  60. }
  61. const prepared: PreparedRequest = await adapter.prepareRequest(request)
  62. expect(prepared).toBeDefined()
  63. expect(prepared.credentialManagerUsed).toBe(true)
  64. expect(prepared.signatureVerified).toBe(true)
  65. })
  66. })
  67. describe('Platform-Specific Authentication', () => {
  68. test('should handle Pacifica Ed25519 signature', async () => {
  69. const request: PlatformRequest = {
  70. accountId: 'pacifica-account',
  71. method: 'POST',
  72. path: '/api/v1/orders',
  73. body: { symbol: 'BTC/USDT' },
  74. options: { requiresAuth: true }
  75. }
  76. const prepared: PreparedRequest = await adapter.prepareRequest(request)
  77. expect(prepared.signatureAlgorithm).toBe('Ed25519')
  78. expect(prepared.headers['X-Signature']).toMatch(/^[a-fA-F0-9]{128}$/)
  79. })
  80. })
  81. describe('Error Handling', () => {
  82. test('should handle missing credentials gracefully', async () => {
  83. const request: PlatformRequest = {
  84. accountId: 'missing-credentials-account',
  85. method: 'POST',
  86. path: '/api/v1/orders',
  87. body: { test: 'data' },
  88. options: { requiresAuth: true }
  89. }
  90. await expect(adapter.prepareRequest(request)).rejects.toThrow(/credentials not found/i)
  91. })
  92. })
  93. })