File size: 3,630 Bytes
68465df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import {fetchMinimumClientVersion, minimumClientVersionUrl} from "./cloudVersion"

function jsonResponse(body: unknown, ok = true, status = 200): Response {
  return {ok, status, json: async () => body} as Response
}

describe("minimum client version policy", () => {
  beforeEach(() => {
    jest.restoreAllMocks()
  })

  it("fetches the policy from Runtime", async () => {
    const fetchMock = jest
      .spyOn(globalThis, "fetch")
      .mockResolvedValue(jsonResponse({success: true, data: {required: "3.1.0", recommended: "3.2.0"}}))

    const result = await fetchMinimumClientVersion("https://runtime.example/", 1, 0)

    expect(result.is_ok()).toBe(true)
    if (result.is_error()) throw result.error
    expect(result.value).toEqual({required: "3.1.0", recommended: "3.2.0"})
    expect(fetchMock).toHaveBeenCalledWith("https://runtime.example/api/client/min-version")
  })

  it("keeps a Runtime base path and rejects query-bearing base URLs", () => {
    expect(minimumClientVersionUrl("https://runtime.example/mentra/")).toBe(
      "https://runtime.example/mentra/api/client/min-version",
    )
    expect(() => minimumClientVersionUrl("https://runtime.example/?tenant=acme")).toThrow(/query/)
    expect(() => minimumClientVersionUrl("not a url")).toThrow(/invalid/)
  })

  it("falls back to the required version when recommended is missing", async () => {
    jest.spyOn(globalThis, "fetch").mockResolvedValue(jsonResponse({data: {required: "3.1.0"}}))

    const result = await fetchMinimumClientVersion("https://runtime.example", 1, 0)

    if (result.is_error()) throw result.error
    expect(result.value).toEqual({required: "3.1.0", recommended: "3.1.0"})
  })

  it("rejects malformed policy values instead of returning them", async () => {
    jest.spyOn(globalThis, "fetch").mockResolvedValue(jsonResponse({data: {required: "latest", recommended: "3.2.0"}}))

    const result = await fetchMinimumClientVersion("https://runtime.example", 1, 0)

    expect(result.is_error()).toBe(true)
    if (result.is_ok()) throw new Error("expected malformed policy to fail")
    expect(result.error.message).toMatch(/malformed required/)
  })

  it("rejects non-canonical SemVer such as a v prefix", async () => {
    jest.spyOn(globalThis, "fetch").mockResolvedValue(jsonResponse({data: {required: "3.1.0", recommended: "v3.2.0"}}))

    const result = await fetchMinimumClientVersion("https://runtime.example", 1, 0)

    expect(result.is_error()).toBe(true)
  })

  it("retries HTTP failures and returns the last error", async () => {
    const fetchMock = jest
      .spyOn(globalThis, "fetch")
      .mockResolvedValueOnce(jsonResponse({}, false, 503))
      .mockRejectedValueOnce(new Error("dns"))
      .mockResolvedValueOnce(jsonResponse({}, false, 500))

    const result = await fetchMinimumClientVersion("https://runtime.example", 3, 0)

    expect(fetchMock).toHaveBeenCalledTimes(3)
    expect(result.is_error()).toBe(true)
    if (result.is_ok()) throw new Error("expected HTTP failure")
    expect(result.error.message).toBe("min-version HTTP 500")
  })

  it("succeeds on a later attempt after a transient failure", async () => {
    const fetchMock = jest
      .spyOn(globalThis, "fetch")
      .mockRejectedValueOnce(new Error("dns"))
      .mockResolvedValueOnce(jsonResponse({data: {required: "3.1.0", recommended: "3.2.0"}}))

    const result = await fetchMinimumClientVersion("https://runtime.example", 3, 0)

    expect(fetchMock).toHaveBeenCalledTimes(2)
    expect(result.is_ok()).toBe(true)
  })
})