|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { readAnnouncement } from '../announcement' |
| 3 | + |
| 4 | +const ANNOUNCEMENT = JSON.stringify( |
| 5 | + { url: 'http://127.0.0.1:51234/#token=abc', port: 51234 }, |
| 6 | + null, |
| 7 | + 2, |
| 8 | +) |
| 9 | + |
| 10 | +describe('readAnnouncement', () => { |
| 11 | + it('reads the JSON of devhub ui --json', () => { |
| 12 | + expect(readAnnouncement(`${ANNOUNCEMENT}\n`)).toEqual({ |
| 13 | + url: 'http://127.0.0.1:51234/#token=abc', |
| 14 | + port: 51234, |
| 15 | + }) |
| 16 | + }) |
| 17 | + |
| 18 | + it('waits while the object is not complete', () => { |
| 19 | + const half = ANNOUNCEMENT.slice(0, ANNOUNCEMENT.indexOf('"port"')) |
| 20 | + expect(readAnnouncement(half)).toBeUndefined() |
| 21 | + expect(readAnnouncement('')).toBeUndefined() |
| 22 | + expect(readAnnouncement('DevHub by Root Native runs at')).toBeUndefined() |
| 23 | + }) |
| 24 | + |
| 25 | + it('reads the object when text comes before and after it', () => { |
| 26 | + expect(readAnnouncement(`noise\n${ANNOUNCEMENT}\nmore\n`)).toEqual({ |
| 27 | + url: 'http://127.0.0.1:51234/#token=abc', |
| 28 | + port: 51234, |
| 29 | + }) |
| 30 | + }) |
| 31 | + |
| 32 | + it('refuses a URL that is not the loopback server on the port', () => { |
| 33 | + const wrong = (url: string, port = 51234) => |
| 34 | + readAnnouncement(JSON.stringify({ url, port })) |
| 35 | + expect(wrong('http://localhost:51234/#token=abc')).toBeUndefined() |
| 36 | + expect(wrong('https://127.0.0.1:51234/')).toBeUndefined() |
| 37 | + expect(wrong('http://127.0.0.1:51235/#token=abc')).toBeUndefined() |
| 38 | + expect(wrong('http://127.0.0.1:51234/', 0)).toBeUndefined() |
| 39 | + expect(wrong('not a url')).toBeUndefined() |
| 40 | + }) |
| 41 | + |
| 42 | + it('refuses an object with another shape', () => { |
| 43 | + expect(readAnnouncement('{"error":"nope"}')).toBeUndefined() |
| 44 | + expect(readAnnouncement('{"url":1,"port":"5"}')).toBeUndefined() |
| 45 | + expect(readAnnouncement('[]')).toBeUndefined() |
| 46 | + }) |
| 47 | +}) |
0 commit comments