File size: 4,054 Bytes
25059bd
 
 
 
 
 
 
13def79
25059bd
 
13def79
25059bd
 
 
 
 
1f0f638
25059bd
 
 
7f0d072
25059bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { describe, it, expect, vi, afterAll, afterEach } from 'vitest'
import fs from 'fs'
import path from 'path'
import { mkdtempSync } from 'fs'
import { tmpdir } from 'os'
import { EventEmitter } from 'events'
import { Buffer } from 'buffer'
import { save, restore, configMockFactory, createClearCjs } from '../helpers/coverage'

const tmpDir = mkdtempSync(path.join(tmpdir(), 'cov-seed-'))
const clearCjs = createClearCjs(import.meta.url)

afterAll(() => {
  try { fs.rmSync(tmpDir, { recursive: true, force: true }) } catch {}
})

vi.mock('../../src/config/index.js', () => configMockFactory())

describe('seed.js', () => {
  afterEach(() => {
    clearCjs('../../src/db/seed.js', '../../src/db/migrate.js', '../../src/db/index.js')
  })

  it('imports and migrate runs, seed returns early when DB has data', async () => {
    const seedDbPath = path.join(tmpDir, 'seed.db')
    const { DatabaseSync } = await import('node:sqlite')
    const db = new DatabaseSync(seedDbPath)
    db.exec('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)')
    db.prepare('INSERT INTO users (id, name) VALUES (1, ?)').run('Test')
    db.close()
    const s = save('DB_PATH')
    process.env.DB_PATH = seedDbPath
    vi.resetModules()
    const { seed } = await import('../../src/db/seed.js')
    await expect(seed()).resolves.toBeUndefined()
    restore(s)
  })

  it('covers seed fetch parse error via promise rejection', async () => {
    const db = {
      exec: vi.fn(),
      prepare: () => ({
        get: () => ({ rowCount: 0 }),
        run: vi.fn(),
      }),
    }
    const fetchFn = () => Promise.reject(new SyntaxError('Unexpected end of JSON input'))
    const { seed } = await import('../../src/db/seed.js')
    await expect(seed({ database: db, fetch: fetchFn, runMigrate: false })).rejects.toThrow()
  })

  it('covers seed fetch parse error via event stream', async () => {
    const db = {
      exec: vi.fn(),
      prepare: () => ({
        get: () => ({ rowCount: 0 }),
        run: vi.fn(),
      }),
    }
    const { seed } = await import('../../src/db/seed.js')
    const fetch = async (url) => {
      const response = new EventEmitter()
      queueMicrotask(() => {
        response.emit('data', Buffer.from('{bad'))
        response.emit('end')
      })
      return response
    }
    await expect(seed({ database: db, fetch, runMigrate: false })).rejects.toThrow()
  })

  it('covers seed https error', async () => {
    const db = {
      exec: vi.fn(),
      prepare: () => ({
        get: () => ({ rowCount: 0 }),
        run: vi.fn(),
      }),
    }
    const fetchFn = () => Promise.reject(new Error('boom'))
    const { seed } = await import('../../src/db/seed.js')
    await expect(seed({ database: db, fetch: fetchFn, runMigrate: false })).rejects.toThrow('boom')
  })

  it('covers full seed insert path', async () => {
    const runs = []
    const db = {
      exec: vi.fn(),
      prepare: () => ({
        get: () => ({ rowCount: 0 }),
        run: (...args) => runs.push(args),
      }),
    }
    const payload = {
      users: [{ id: 1, name: 'A', username: 'a', email: 'a@example.com', phone: '1', website: 'w', address: { city: 'Hanoi' }, company: { name: 'ACME' } }],
      posts: [{ id: 1, userId: 1, title: 'P', body: 'Body' }],
      comments: [{ id: 1, postId: 1, name: 'C', email: 'c@example.com', body: 'Comment' }],
      albums: [{ id: 1, userId: 1, title: 'Album' }],
      photos: [{ id: 1, albumId: 1, title: 'Photo', url: 'u', thumbnailUrl: 't' }],
      todos: [{ id: 1, userId: 1, title: 'Todo', completed: true }, { id: 2, userId: 1, title: 'Todo2', completed: false }],
    }
    const { seed } = await import('../../src/db/seed.js')
    const fetch = async (url) => {
      const name = new URL(url).pathname.slice(1)
      return payload[name]
    }
    await seed({ database: db, fetch, runMigrate: false })
    expect(runs).toHaveLength(7)
    expect(runs[0][6]).toBe('{"city":"Hanoi"}')
    expect(runs[0][7]).toBe('{"name":"ACME"}')
    expect(runs[6][3]).toBe(0)
  })
})