logseq-api.spec.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import { test } from './fixtures'
  2. import { expect } from '@playwright/test'
  3. import { callPageAPI } from './utils'
  4. import { Page } from 'playwright'
  5. async function createDBGraph(page: Page) {
  6. await page.locator(`#left-sidebar .cp__graphs-selector > a`).click()
  7. await page.click('text="Create db graph"')
  8. await page.waitForSelector('.new-graph')
  9. const name = `e2e-db-${Date.now()}`
  10. await page.waitForTimeout(100)
  11. await page.keyboard.type(name)
  12. await page.locator('.new-graph > .ui__button').click()
  13. return name
  14. }
  15. test.skip('test db graph', async ({ page }) => {
  16. const name = await createDBGraph(page)
  17. await page.waitForSelector(`a[title="logseq_db_${name}"]`)
  18. await page.pause()
  19. })
  20. test('(File graph): block related apis',
  21. async ({ page }) => {
  22. const callAPI = callPageAPI.bind(null, page)
  23. const bPageName = 'block-test-page'
  24. await callAPI('create_page', bPageName, null, { createFirstBlock: false })
  25. await callAPI('create_page', bPageName, null, { createFirstBlock: false })
  26. await page.waitForSelector(`body[data-page="${bPageName}"]`)
  27. let p = await callAPI('get_current_page')
  28. const bp = await callAPI('append_block_in_page', bPageName, 'tests')
  29. expect(p.name).toBe(bPageName)
  30. p = await callAPI('get_page', bPageName)
  31. expect(p.name).toBe(bPageName)
  32. await callAPI('edit_block', bp.uuid)
  33. const b = (await callAPI('get_current_block'))
  34. expect(Object.keys(b)).toContain('uuid')
  35. await page.waitForSelector('.block-editor > textarea')
  36. await page.locator('.block-editor > textarea').fill('')
  37. const content = 'test api'
  38. await page.type('.block-editor > textarea', content)
  39. const editingContent = await callAPI('get_editing_block_content')
  40. expect(editingContent).toBe(content)
  41. // create
  42. let b1 = await callAPI('insert_block', b.uuid, content)
  43. b1 = await callAPI('get_block', b1.uuid)
  44. expect(b1.parent.id).toBe(b.id)
  45. // update
  46. const content1 = content + '+ update!'
  47. await callAPI('update_block', b1.uuid, content1)
  48. b1 = await callAPI('get_block', b1.uuid)
  49. expect(b1.content).toBe(content1)
  50. // remove
  51. await callAPI('remove_block', b1.uuid)
  52. b1 = await callAPI('get_block', b1.uuid)
  53. expect(b1).toBeNull()
  54. // traverse
  55. b1 = await callAPI('insert_block', b.uuid, content1, { sibling: true })
  56. const nb = await callAPI('get_next_sibling_block', b.uuid)
  57. const pb = await callAPI('get_previous_sibling_block', b1.uuid)
  58. expect(nb.uuid).toBe(b1.uuid)
  59. expect(pb.uuid).toBe(b.uuid)
  60. // move
  61. await callAPI('move_block', b.uuid, b1.uuid)
  62. const mb = await callAPI('get_next_sibling_block', b1.uuid)
  63. expect(mb.uuid).toBe(b.uuid)
  64. // properties
  65. // FIXME: redundant api call
  66. await callAPI('upsert_block_property', b1.uuid, 'a')
  67. await callAPI('upsert_block_property', b1.uuid, 'a', 1)
  68. let prop1 = await callAPI('get_block_property', b1.uuid, 'a')
  69. expect(prop1).toBe(1)
  70. await callAPI('upsert_block_property', b1.uuid, 'a', 2)
  71. prop1 = await callAPI('get_block_property', b1.uuid, 'a')
  72. expect(prop1).toBe(2)
  73. await callAPI('remove_block_property', b1.uuid, 'a')
  74. prop1 = await callAPI('get_block_property', b1.uuid, 'a')
  75. expect(prop1).toBeNull()
  76. await callAPI('upsert_block_property', b1.uuid, 'a', 1)
  77. await callAPI('upsert_block_property', b1.uuid, 'b', 1)
  78. prop1 = await callAPI('get_block_properties', b1.uuid)
  79. expect(prop1).toEqual({ a: 1, b: 1 })
  80. // await page.pause()
  81. })
  82. test('(DB graph): block related apis',
  83. async ({ page }) => {
  84. const name = await createDBGraph(page)
  85. await page.waitForSelector(`a[title="logseq_db_${name}"]`)
  86. const callAPI = callPageAPI.bind(null, page)
  87. const bPageName = 'block-test-page'
  88. await callAPI('create_page', bPageName, null, { createFirstBlock: false })
  89. await callAPI('create_page', bPageName, null, { createFirstBlock: false })
  90. await page.waitForSelector(`body[data-page="${bPageName}"]`)
  91. let p = await callAPI('get_current_page')
  92. const bp = await callAPI('append_block_in_page', bPageName, 'tests')
  93. expect(p.name).toBe(bPageName)
  94. p = await callAPI('get_page', bPageName)
  95. expect(p.name).toBe(bPageName)
  96. await callAPI('edit_block', bp.uuid)
  97. const b = (await callAPI('get_current_block'))
  98. expect(Object.keys(b)).toContain('uuid')
  99. await page.waitForSelector('.block-editor > textarea')
  100. await page.locator('.block-editor > textarea').fill('')
  101. const content = 'test api'
  102. await page.type('.block-editor > textarea', content)
  103. const editingContent = await callAPI('get_editing_block_content')
  104. expect(editingContent).toBe(content)
  105. // create
  106. let b1 = await callAPI('insert_block', b.uuid, content)
  107. b1 = await callAPI('get_block', b1.uuid)
  108. expect(b1.parent.id).toBe(b.id)
  109. // update
  110. const content1 = content + '+ update!'
  111. await callAPI('update_block', b1.uuid, content1)
  112. b1 = await callAPI('get_block', b1.uuid)
  113. expect(b1.content).toBe(content1)
  114. // remove
  115. await callAPI('remove_block', b1.uuid)
  116. b1 = await callAPI('get_block', b1.uuid)
  117. expect(b1).toBeNull()
  118. // traverse
  119. b1 = await callAPI('insert_block', b.uuid, content1, { sibling: true })
  120. const nb = await callAPI('get_next_sibling_block', b.uuid)
  121. const pb = await callAPI('get_previous_sibling_block', b1.uuid)
  122. expect(nb.uuid).toBe(b1.uuid)
  123. expect(pb.uuid).toBe(b.uuid)
  124. // move
  125. await callAPI('move_block', b.uuid, b1.uuid)
  126. const mb = await callAPI('get_next_sibling_block', b1.uuid)
  127. expect(mb.uuid).toBe(b.uuid)
  128. // properties
  129. await callAPI('upsert_block_property', b1.uuid, 'a', 'a')
  130. let prop1 = await callAPI('get_block_property', b1.uuid, 'a')
  131. expect(prop1.title).toBe('a')
  132. await callAPI('upsert_block_property', b1.uuid, 'a', 'b')
  133. prop1 = await callAPI('get_block_property', b1.uuid, 'a')
  134. expect(prop1.title).toBe('b')
  135. await callAPI('remove_block_property', b1.uuid, 'a')
  136. prop1 = await callAPI('get_block_property', b1.uuid, 'a')
  137. expect(prop1).toBeNull()
  138. await callAPI('upsert_block_property', b1.uuid, 'a', 'a')
  139. await callAPI('upsert_block_property', b1.uuid, 'b', 'b')
  140. prop1 = await callAPI('get_block_properties', b1.uuid)
  141. expect(prop1).toEqual({ ':plugin.property/a': 'a', ':plugin.property/b': 'b' })
  142. // properties entity & schema
  143. await callAPI('upsert_property', 'p1')
  144. prop1 = await callAPI('get_property', 'p1')
  145. expect(prop1.title).toBe('p1')
  146. expect(prop1.ident).toBe(':plugin.property/p1')
  147. await callAPI('upsert_property', 'map1', { type: 'map' })
  148. await callAPI('upsert_block_property', b1.uuid, 'map1', { a: 1 })
  149. prop1 = await callAPI('get_property', 'map1')
  150. const b1p = await callAPI('get_block_property', b1.uuid, 'map1')
  151. expect(prop1.schema.type).toBe('map')
  152. expect(b1p).toEqual({a: 1})
  153. // await page.pause()
  154. })