remoteConnectionLogs.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. appendRemoteConnectionRuntimeLog,
  4. normalizeRemoteConnectionLogMessage,
  5. shouldAcceptRemoteConnectionRuntimeLog,
  6. type RemoteConnectionLogEntry,
  7. } from "@/hooks/useRemoteConnectionLogs.js";
  8. function createLogEntry(overrides: Partial<RemoteConnectionLogEntry>): RemoteConnectionLogEntry {
  9. return {
  10. id: "log-1",
  11. level: "info",
  12. message: "connecting...",
  13. timestamp: "10:00:00",
  14. ...overrides,
  15. };
  16. }
  17. describe("remote connection log merge", () => {
  18. it("preserves Source Server phases while removing generic process prefixes", () => {
  19. expect(
  20. normalizeRemoteConnectionLogMessage(
  21. "[2026-08-19 12:00:00.000] [pid:123] [source-server:upload] upload progress (archive): 50.0%",
  22. ),
  23. ).toBe("[source-server:upload] upload progress (archive): 50.0%");
  24. });
  25. it("coalesces Source Server upload progress by phase and artifact", () => {
  26. const current = [
  27. createLogEntry({
  28. id: "source-upload",
  29. message: "[source-server:upload] upload progress (archive): 10.0%",
  30. }),
  31. ];
  32. const next = appendRemoteConnectionRuntimeLog(current, {
  33. id: "source-upload-next",
  34. level: "info",
  35. message: "[source-server:upload] upload progress (archive): 80.0%",
  36. timestamp: "10:00:09",
  37. });
  38. expect(next).toHaveLength(1);
  39. expect(next[0]?.message).toContain("80.0%");
  40. });
  41. it("coalesces consecutive download progress lines into one row", () => {
  42. const currentLogs = [
  43. createLogEntry({
  44. id: "progress-1",
  45. message: "download progress: 15.0% (15.0/100.0 MB, 2.50 MB/s)",
  46. }),
  47. ];
  48. const nextLogs = appendRemoteConnectionRuntimeLog(currentLogs, {
  49. id: "progress-2",
  50. level: "info",
  51. message: "download progress: 20.0% (20.0/100.0 MB, 2.20 MB/s)",
  52. timestamp: "10:00:01",
  53. });
  54. expect(nextLogs).toHaveLength(1);
  55. expect(nextLogs[0]?.id).toBe("progress-1");
  56. expect(nextLogs[0]?.message).toContain("20.0%");
  57. expect(nextLogs[0]?.timestamp).toBe("10:00:01");
  58. });
  59. it("appends a new row when message is not a progress update", () => {
  60. const currentLogs = [
  61. createLogEntry({
  62. id: "progress-1",
  63. message: "download progress: 100.0% (100.0/100.0 MB, 3.10 MB/s)",
  64. }),
  65. ];
  66. const nextLogs = appendRemoteConnectionRuntimeLog(currentLogs, {
  67. id: "checksum-1",
  68. level: "info",
  69. message: "checksum verified",
  70. timestamp: "10:00:02",
  71. });
  72. expect(nextLogs).toHaveLength(2);
  73. expect(nextLogs[1]?.id).toBe("checksum-1");
  74. expect(nextLogs[1]?.message).toBe("checksum verified");
  75. });
  76. it("coalesces consecutive upload progress lines for the same file", () => {
  77. const currentLogs = [
  78. createLogEntry({
  79. id: "upload-1",
  80. message: "upload progress [exec] (node.new): 35.0% (3.5/10.0 MB, 2.40 MB/s)",
  81. }),
  82. ];
  83. const nextLogs = appendRemoteConnectionRuntimeLog(currentLogs, {
  84. id: "upload-2",
  85. level: "info",
  86. message: "upload progress [exec] (node.new): 55.0% (5.5/10.0 MB, 2.10 MB/s)",
  87. timestamp: "10:00:03",
  88. });
  89. expect(nextLogs).toHaveLength(1);
  90. expect(nextLogs[0]?.id).toBe("upload-1");
  91. expect(nextLogs[0]?.message).toContain("55.0%");
  92. expect(nextLogs[0]?.timestamp).toBe("10:00:03");
  93. });
  94. it("coalesces interleaved upload progress lines for the same file", () => {
  95. const currentLogs = [
  96. createLogEntry({
  97. id: "node-upload",
  98. message: "upload progress [sftp] (node.new): 20.0% (2.0/10.0 MB, 2.00 MB/s)",
  99. }),
  100. createLogEntry({
  101. id: "server-upload",
  102. message: "upload progress [sftp] (zcode-server.cjs.new): 45.0% (4.5/10.0 MB, 1.80 MB/s)",
  103. }),
  104. ];
  105. const nextLogs = appendRemoteConnectionRuntimeLog(currentLogs, {
  106. id: "node-upload-next",
  107. level: "info",
  108. message: "upload progress [sftp] (node.new): 25.0% (2.5/10.0 MB, 2.10 MB/s)",
  109. timestamp: "10:00:04",
  110. });
  111. expect(nextLogs).toHaveLength(2);
  112. expect(nextLogs[0]?.id).toBe("server-upload");
  113. expect(nextLogs[1]?.id).toBe("node-upload");
  114. expect(nextLogs[1]?.message).toContain("25.0%");
  115. expect(nextLogs[1]?.timestamp).toBe("10:00:04");
  116. });
  117. it("keeps separate rows for upload progress from different files", () => {
  118. const currentLogs = [
  119. createLogEntry({
  120. id: "upload-1",
  121. message: "upload progress [exec] (node.new): 100.0% (10.0/10.0 MB, 2.00 MB/s)",
  122. }),
  123. ];
  124. const nextLogs = appendRemoteConnectionRuntimeLog(currentLogs, {
  125. id: "upload-2",
  126. level: "info",
  127. message: "upload progress [exec] (zcode-server.cjs.new): 5.0% (0.5/10.0 MB, 1.50 MB/s)",
  128. timestamp: "10:00:04",
  129. });
  130. expect(nextLogs).toHaveLength(2);
  131. expect(nextLogs[1]?.id).toBe("upload-2");
  132. expect(nextLogs[1]?.message).toContain("zcode-server.cjs.new");
  133. });
  134. it("keeps separate rows for different upload transports of the same file", () => {
  135. const currentLogs = [
  136. createLogEntry({
  137. id: "upload-1",
  138. message: "upload progress [sftp] (node.new): 0.1% (0.1/10.0 MB, 0.37 MB/s)",
  139. }),
  140. ];
  141. const nextLogs = appendRemoteConnectionRuntimeLog(currentLogs, {
  142. id: "upload-2",
  143. level: "info",
  144. message: "upload progress [exec] (node.new): 5.0% (0.5/10.0 MB, 1.20 MB/s)",
  145. timestamp: "10:00:05",
  146. });
  147. expect(nextLogs).toHaveLength(2);
  148. expect(nextLogs[1]?.message).toContain("[exec]");
  149. });
  150. it("filters remote connection logs by active request id", () => {
  151. expect(shouldAcceptRemoteConnectionRuntimeLog({ requestId: "request-1" }, "request-1")).toBe(
  152. true,
  153. );
  154. expect(
  155. shouldAcceptRemoteConnectionRuntimeLog({ requestId: "background-request" }, "request-1"),
  156. ).toBe(false);
  157. expect(
  158. shouldAcceptRemoteConnectionRuntimeLog(
  159. { requestId: "background-request" },
  160. null,
  161. ),
  162. ).toBe(true);
  163. });
  164. });
  165. it("coalesces source archive progress while byte counts and speed change", () => {
  166. const first = createLogEntry({
  167. message: "[source-server:upload] upload progress: 10.0% (1.0/10.0 MB, 2.00 MB/s)",
  168. });
  169. const next = appendRemoteConnectionRuntimeLog([first], {
  170. ...first,
  171. id: "next",
  172. message: "[source-server:upload] upload progress: 90.0% (9.0/10.0 MB, 3.00 MB/s)",
  173. });
  174. expect(next).toHaveLength(1);
  175. expect(next[0]?.message).toContain("90.0%");
  176. });