basic.test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @author: oldj
  3. * @homepage: https://oldj.net
  4. */
  5. import { beforeEach, describe, expect, it } from 'vitest'
  6. import { clearData } from '../_base'
  7. import {
  8. getBasicData,
  9. getHostsContent,
  10. getList,
  11. setHostsContent,
  12. setList,
  13. } from '../../src/main/actions'
  14. import { swhdb } from '../../src/main/data'
  15. describe('basic test', () => {
  16. beforeEach(async () => {
  17. await clearData()
  18. })
  19. it('add hosts', async () => {
  20. const basic_data = await getBasicData()
  21. expect(basic_data.list).toHaveLength(0)
  22. expect(basic_data.trashcan).toHaveLength(0)
  23. expect(basic_data.version).toHaveLength(4)
  24. await swhdb.collection.hosts.insert({ id: '1' })
  25. let items = await swhdb.collection.hosts.all()
  26. expect(items).toHaveLength(1)
  27. await setHostsContent('1', '# 111')
  28. expect(await getHostsContent('1')).toBe('# 111')
  29. let list = await getList()
  30. expect(list).toHaveLength(0)
  31. await setList([ { id: '1' } ])
  32. list = await getList()
  33. expect(list).toHaveLength(1)
  34. expect(list[0].id).toBe('1')
  35. })
  36. it('normalizes CRLF when reading and writing hosts content', async () => {
  37. await swhdb.collection.hosts.insert({
  38. id: 'crlf-item',
  39. content: '127.0.0.1 localhost\r\n# comment\r\n',
  40. })
  41. expect(await getHostsContent('crlf-item')).toBe('127.0.0.1 localhost\n# comment\n')
  42. await setHostsContent('crlf-item', '127.0.0.1 localhost\r\n# next\r\n')
  43. const raw = await swhdb.collection.hosts.find<{ content: string }>((i) => i.id === 'crlf-item')
  44. expect(raw?.content).toBe('127.0.0.1 localhost\n# next\n')
  45. expect(await getHostsContent('crlf-item')).toBe('127.0.0.1 localhost\n# next\n')
  46. })
  47. it('group hosts', async () => {
  48. await setList([
  49. { id: '1' },
  50. { id: '2' },
  51. { id: '3', type: 'group', include: [ '1', '2' ] },
  52. ])
  53. const c1 = '# 425748244153'
  54. const c2 = '# 642156457548'
  55. await setHostsContent('1', c1)
  56. await setHostsContent('2', c2)
  57. expect(await getHostsContent('1')).toBe(c1)
  58. expect(await getHostsContent('2')).toBe(c2)
  59. const c3 = await getHostsContent('3')
  60. expect(c3).toContain(c1)
  61. expect(c3.indexOf(c2)).toBeGreaterThan(c3.indexOf(c1))
  62. await setList([
  63. { id: '1' },
  64. { id: '2' },
  65. {
  66. id: '4', type: 'folder', children: [
  67. { id: '5', type: 'group', include: [ '1', '2' ] },
  68. ],
  69. },
  70. ])
  71. const c5 = await getHostsContent('5')
  72. expect(c3).toBe(c5)
  73. })
  74. })