remoteConnectionLogs.test.ts 5.0 KB

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