|
| 1 | +import{describe,expect,it}from"vitest"; |
| 2 | + |
| 3 | +import{ |
| 4 | +formatBody, |
| 5 | +formatContentLength, |
| 6 | +formatHeaders, |
| 7 | +formatMethod, |
| 8 | +formatTime, |
| 9 | +formatUri, |
| 10 | +}from"@/logging/formatters"; |
| 11 | + |
| 12 | +describe("Logging formatters",()=>{ |
| 13 | +it("formats time in appropriate units",()=>{ |
| 14 | +expect(formatTime(500)).toBe("500ms"); |
| 15 | +expect(formatTime(1000)).toBe("1.00s"); |
| 16 | +expect(formatTime(5500)).toBe("5.50s"); |
| 17 | +expect(formatTime(60000)).toBe("1.00m"); |
| 18 | +expect(formatTime(150000)).toBe("2.50m"); |
| 19 | +expect(formatTime(3600000)).toBe("1.00h"); |
| 20 | +expect(formatTime(7255000)).toBe("2.02h"); |
| 21 | +}); |
| 22 | + |
| 23 | +describe("formatMethod",()=>{ |
| 24 | +it("normalizes HTTP methods to uppercase",()=>{ |
| 25 | +expect(formatMethod("get")).toBe("GET"); |
| 26 | +expect(formatMethod("post")).toBe("POST"); |
| 27 | +expect(formatMethod("PUT")).toBe("PUT"); |
| 28 | +expect(formatMethod("delete")).toBe("DELETE"); |
| 29 | +}); |
| 30 | + |
| 31 | +it("defaults to GET for falsy values",()=>{ |
| 32 | +expect(formatMethod(undefined)).toBe("GET"); |
| 33 | +expect(formatMethod("")).toBe("GET"); |
| 34 | +}); |
| 35 | +}); |
| 36 | + |
| 37 | +describe("formatContentLength",()=>{ |
| 38 | +it("uses content-length header when available",()=>{ |
| 39 | +constresult=formatContentLength({"content-length":"1024"},null); |
| 40 | +expect(result).toContain("1.02 kB"); |
| 41 | +}); |
| 42 | + |
| 43 | +it("handles invalid content-length header",()=>{ |
| 44 | +constresult=formatContentLength({"content-length":"invalid"},null); |
| 45 | +expect(result).toContain("?"); |
| 46 | +}); |
| 47 | + |
| 48 | +it.each([ |
| 49 | +["hello",5], |
| 50 | +[Buffer.from("test"),4], |
| 51 | +[123,8], |
| 52 | +[false,8], |
| 53 | +[BigInt(1234),4], |
| 54 | +[null,0], |
| 55 | +[undefined,0], |
| 56 | +])("calculates size for %s",(data:unknown,bytes:number)=>{ |
| 57 | +constresult=formatContentLength({},data); |
| 58 | +expect(result).toContain(`${bytes} B`); |
| 59 | +}); |
| 60 | + |
| 61 | +it("estimates size for objects",()=>{ |
| 62 | +constresult=formatContentLength({},{foo:"bar"}); |
| 63 | +expect(result).toMatch(/~\d+/); |
| 64 | +}); |
| 65 | + |
| 66 | +it("handles circular references safely",()=>{ |
| 67 | +constcircular:Record<string,unknown>={a:1}; |
| 68 | +circular.self=circular; |
| 69 | +constresult=formatContentLength({},circular); |
| 70 | +expect(result).toMatch(/~\d+/); |
| 71 | +}); |
| 72 | +}); |
| 73 | + |
| 74 | +describe("formatUri",()=>{ |
| 75 | +it("returns URL when present",()=>{ |
| 76 | +expect(formatUri({url:"https://example.com/api"})).toBe( |
| 77 | +"https://example.com/api", |
| 78 | +); |
| 79 | +expect(formatUri({url:"/relative/path"})).toBe("/relative/path"); |
| 80 | +}); |
| 81 | + |
| 82 | +it("returns placeholder for missing URL",()=>{ |
| 83 | +expect(formatUri(undefined)).toContain("no url"); |
| 84 | +expect(formatUri({})).toContain("no url"); |
| 85 | +expect(formatUri({url:""})).toContain("no url"); |
| 86 | +}); |
| 87 | +}); |
| 88 | + |
| 89 | +describe("formatHeaders",()=>{ |
| 90 | +it("formats headers as key-value pairs",()=>{ |
| 91 | +constheaders={ |
| 92 | +"content-type":"application/json", |
| 93 | +accept:"text/html", |
| 94 | +}; |
| 95 | +constresult=formatHeaders(headers); |
| 96 | +expect(result).toContain("content-type: application/json"); |
| 97 | +expect(result).toContain("accept: text/html"); |
| 98 | +}); |
| 99 | + |
| 100 | +it("redacts sensitive headers",()=>{ |
| 101 | +constsensitiveHeaders=["Coder-Session-Token","Proxy-Authorization"]; |
| 102 | + |
| 103 | +sensitiveHeaders.forEach((header)=>{ |
| 104 | +constresult=formatHeaders({[header]:"secret-value"}); |
| 105 | +expect(result).toContain(`${header}: <redacted>`); |
| 106 | +expect(result).not.toContain("secret-value"); |
| 107 | +}); |
| 108 | +}); |
| 109 | + |
| 110 | +it("returns placeholder for empty headers",()=>{ |
| 111 | +expect(formatHeaders({})).toBe("<no headers>"); |
| 112 | +}); |
| 113 | +}); |
| 114 | + |
| 115 | +describe("formatBody",()=>{ |
| 116 | +it("formats various body types",()=>{ |
| 117 | +expect(formatBody({key:"value"})).toContain("key: 'value'"); |
| 118 | +expect(formatBody("plain text")).toContain("plain text"); |
| 119 | +expect(formatBody([1,2,3])).toContain("1"); |
| 120 | +expect(formatBody(123)).toContain("123"); |
| 121 | +expect(formatBody(true)).toContain("true"); |
| 122 | +}); |
| 123 | + |
| 124 | +it("handles circular references gracefully",()=>{ |
| 125 | +constcircular:Record<string,unknown>={a:1}; |
| 126 | +circular.self=circular; |
| 127 | +constresult=formatBody(circular); |
| 128 | +expect(result).toBeTruthy(); |
| 129 | +expect(result).not.toContain("invalid body"); |
| 130 | +expect(result).toContain("a: 1"); |
| 131 | +}); |
| 132 | + |
| 133 | +it("handles deep nesting",()=>{ |
| 134 | +constdeep={ |
| 135 | +level1:{level2:{level3:{level4:{value:"deep"}}}}, |
| 136 | +}; |
| 137 | +constresult=formatBody(deep); |
| 138 | +expect(result).toContain("level4: { value: 'deep' }"); |
| 139 | +}); |
| 140 | + |
| 141 | +it("returns placeholder for empty values",()=>{ |
| 142 | +constemptyValues=[null,undefined,"",0,false]; |
| 143 | +emptyValues.forEach((value)=>{ |
| 144 | +expect(formatBody(value)).toContain("no body"); |
| 145 | +}); |
| 146 | +}); |
| 147 | +}); |
| 148 | +}); |