normalize.test.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * @author: oldj
  3. * @homepage: https://oldj.net
  4. */
  5. import { promises as fs } from 'fs'
  6. import * as path from 'path'
  7. import { fileURLToPath } from 'url'
  8. import { describe, expect, it } from 'vitest'
  9. import normalize, { parseLine } from '../../src/common/normalize'
  10. const dirname = path.dirname(fileURLToPath(import.meta.url))
  11. const mock_dir = path.join(dirname, 'mock')
  12. describe('normalize test', () => {
  13. const loadData = async (fn: string) => {
  14. return fs.readFile(path.join(mock_dir, fn), 'utf-8')
  15. }
  16. it('basic test', () => {
  17. expect(normalize('aaa')).toBe('aaa')
  18. })
  19. it('paresLine test', () => {
  20. let d = parseLine('1.2.3.4 abc.com')
  21. expect(d.ip).toBe('1.2.3.4')
  22. expect(d.domains).toEqual([ 'abc.com' ])
  23. expect(d.comment).toBe('')
  24. d = parseLine('1.2.3.4 \t abc.com abc2.com abc3.com\ttest.com ')
  25. expect(d.ip).toBe('1.2.3.4')
  26. expect(d.domains).toEqual([ 'abc.com', 'abc2.com', 'abc3.com', 'test.com' ])
  27. expect(d.comment).toBe('')
  28. d = parseLine('1.2.3.4 \t abc.com abc2.com abc3.com\ttest.com # this is comment ')
  29. expect(d.ip).toBe('1.2.3.4')
  30. expect(d.domains).toEqual([ 'abc.com', 'abc2.com', 'abc3.com', 'test.com' ])
  31. expect(d.comment).toBe('this is comment')
  32. d = parseLine('1.2.3.4 \t # this is comment ')
  33. expect(d.ip).toBe('1.2.3.4')
  34. expect(d.domains).toEqual([])
  35. expect(d.comment).toBe('this is comment')
  36. d = parseLine(' \t # this is comment ')
  37. expect(d.ip).toBe('')
  38. expect(d.domains).toEqual([])
  39. expect(d.comment).toBe('this is comment')
  40. d = parseLine('# this is comment ')
  41. expect(d.ip).toBe('')
  42. expect(d.domains).toEqual([])
  43. expect(d.comment).toBe('this is comment')
  44. })
  45. it('duplicate test', async () => {
  46. const eq = async (number: string) => {
  47. const input = await loadData(`normalize.${number}.input.hosts`)
  48. const output = await loadData(`normalize.${number}.output.hosts`)
  49. expect(normalize(input, { remove_duplicate_records: true })).toBe(output)
  50. }
  51. await eq('001')
  52. })
  53. })