seed-initial.spec.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const {
  2. getInitialStylistFromEnv,
  3. seedInitialData,
  4. } = require('./seed-initial.cjs');
  5. describe('seedInitialData', () => {
  6. const env = {
  7. INITIAL_STYLIST_ID: '1',
  8. INITIAL_STYLIST_USERNAME: 'Ogawa',
  9. INITIAL_STYLIST_EMAIL: 'stylist@example.com',
  10. INITIAL_STYLIST_PASSWORD: 'Passw0rd!',
  11. };
  12. const createPrismaMock = () => ({
  13. hairstylistUser: {
  14. findUnique: jest.fn(),
  15. create: jest.fn(),
  16. update: jest.fn(),
  17. },
  18. systemConfig: {
  19. upsert: jest.fn(),
  20. },
  21. hairTemplate: {
  22. upsert: jest.fn(),
  23. },
  24. });
  25. it('seeds only the initial stylist account', async () => {
  26. const prisma = createPrismaMock();
  27. prisma.hairstylistUser.findUnique.mockResolvedValue(null);
  28. const hashPassword = jest.fn().mockResolvedValue('hashed-password');
  29. const result = await seedInitialData(prisma, { env, hashPassword });
  30. expect(hashPassword).toHaveBeenCalledWith('Passw0rd!', 10);
  31. expect(prisma.hairstylistUser.create).toHaveBeenCalledWith({
  32. data: {
  33. id: BigInt(1),
  34. username: 'Ogawa',
  35. email: 'stylist@example.com',
  36. password: 'hashed-password',
  37. isDeleted: 0,
  38. },
  39. });
  40. expect(prisma.systemConfig.upsert).not.toHaveBeenCalled();
  41. expect(prisma.hairTemplate.upsert).not.toHaveBeenCalled();
  42. expect(result).toEqual({
  43. stylist: 'created',
  44. stylistUsername: 'Ogawa',
  45. stylistEmail: 'stylist@example.com',
  46. });
  47. });
  48. it('does not reset or rewrite the initial stylist when it already exists', async () => {
  49. const prisma = createPrismaMock();
  50. prisma.hairstylistUser.findUnique.mockResolvedValue({ id: BigInt(1) });
  51. const hashPassword = jest.fn();
  52. const result = await seedInitialData(prisma, { env, hashPassword });
  53. expect(hashPassword).not.toHaveBeenCalled();
  54. expect(prisma.hairstylistUser.create).not.toHaveBeenCalled();
  55. expect(prisma.hairstylistUser.update).not.toHaveBeenCalled();
  56. expect(result.stylist).toBe('existing');
  57. });
  58. it('keeps the expected default account', () => {
  59. expect(getInitialStylistFromEnv(env)).toEqual({
  60. id: BigInt(1),
  61. username: 'Ogawa',
  62. email: 'stylist@example.com',
  63. password: 'Passw0rd!',
  64. });
  65. });
  66. });