text_builder_spec.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { expect } from "chai";
  2. import helper from "helper";
  3. let text_builder, grid;
  4. describe("Text Builder", () => {
  5. beforeEach((done) => {
  6. global.mock_DOM_template = [
  7. " ",
  8. " ",
  9. " ",
  10. " ",
  11. " ",
  12. " !!! ",
  13. " !!! ",
  14. ];
  15. // We can't simulate anything that uses groups of spaces, as TextBuilder collapses all spaces
  16. // to a single space in order to sync with how the DOM renders monospaced text.
  17. //
  18. // TODO: That being said, I can surely imagine that multiple spaces within a single DOM rect
  19. // would not be collapsed by the DOM, so maybe that's something to take into account for
  20. // the TextBuilder code?
  21. global.mock_DOM_text = [
  22. "Testing nodes. ",
  23. "Max 15 chars ",
  24. "wide. ",
  25. "Diff kinds of ",
  26. "Whitespace. ",
  27. "Also we need to ",
  28. "test subframes.",
  29. ];
  30. global.tty = {
  31. width: 5,
  32. height: 3,
  33. x_scroll: 0,
  34. y_scroll: 0,
  35. };
  36. global.frame_type = "small";
  37. helper.runTextBuilder((returned_text_builder) => {
  38. text_builder = returned_text_builder;
  39. grid = text_builder.tty_grid.cells;
  40. done();
  41. });
  42. });
  43. it("should convert text nodes to a grid of cell objects", () => {
  44. expect(grid.length).to.equal(37);
  45. expect(grid[0]).to.deep.equal({
  46. index: 0,
  47. rune: "T",
  48. fg_colour: [6, 0, 0],
  49. bg_colour: [0, 0, 6],
  50. parent_element: {
  51. style: {
  52. textAlign: "left",
  53. },
  54. },
  55. tty_coords: {
  56. x: 0,
  57. y: 0,
  58. },
  59. dom_coords: {
  60. x: 0,
  61. y: 0,
  62. },
  63. });
  64. expect(grid[5]).to.equal(undefined);
  65. expect(grid[16]).to.deep.equal({
  66. index: 16,
  67. rune: "M",
  68. fg_colour: [16, 0, 0],
  69. bg_colour: [0, 0, 16],
  70. parent_element: {
  71. style: {
  72. textAlign: "left",
  73. },
  74. },
  75. tty_coords: {
  76. x: 0,
  77. y: 1,
  78. },
  79. dom_coords: {
  80. x: 0,
  81. y: 2,
  82. },
  83. });
  84. expect(grid[36]).to.deep.equal({
  85. index: 36,
  86. rune: ".",
  87. fg_colour: [30, 0, 0],
  88. bg_colour: [0, 0, 30],
  89. parent_element: {
  90. style: {
  91. textAlign: "left",
  92. },
  93. },
  94. tty_coords: {
  95. x: 4,
  96. y: 2,
  97. },
  98. dom_coords: {
  99. x: 4,
  100. y: 4,
  101. },
  102. });
  103. expect(grid[37]).to.equal(undefined);
  104. });
  105. it("should not detect the colour of whitespace characters", () => {
  106. expect(grid[19].rune).to.equal(" ");
  107. expect(grid[19].fg_colour).to.deep.equal([0, 19, 0]);
  108. });
  109. it("should serialise a frame", () => {
  110. text_builder._serialiseFrame();
  111. expect(text_builder.frame.meta).to.deep.equal({
  112. sub_left: 0,
  113. sub_top: 0,
  114. sub_width: 5,
  115. sub_height: 6,
  116. total_width: 16,
  117. total_height: 14,
  118. id: 1,
  119. });
  120. expect(text_builder.frame.text).to.deep.equal([
  121. "T",
  122. "e",
  123. "s",
  124. "t",
  125. "i",
  126. "M",
  127. "a",
  128. "x",
  129. " ",
  130. "1",
  131. "w",
  132. "i",
  133. "d",
  134. "e",
  135. ".",
  136. ]);
  137. expect(text_builder.frame.colours).to.deep.equal([
  138. 6, 0, 0, 7, 0, 0, 8, 0, 0, 9, 0, 0, 10, 0, 0, 16, 0, 0, 17, 0, 0, 18, 0,
  139. 0, 0, 19, 0, 20, 0, 0, 26, 0, 0, 27, 0, 0, 28, 0, 0, 29, 0, 0, 30, 0, 0,
  140. ]);
  141. });
  142. });