|
1 | 1 | import { describe, expect, it } from 'vitest'; |
2 | | -import { parseSpacesQueryResult } from '../../src/space/find-many-public.js'; |
| 2 | +import { buildFilterString, buildSpacesQuery, parseSpacesQueryResult } from '../../src/space/find-many-public.js'; |
3 | 3 |
|
4 | 4 | const buildQuerySpace = ({ |
5 | 5 | id = 'space-id', |
@@ -141,3 +141,109 @@ describe('parseSpacesQueryResult', () => { |
141 | 141 | ]); |
142 | 142 | }); |
143 | 143 | }); |
| 144 | + |
| 145 | +describe('buildFilterString', () => { |
| 146 | + it('returns undefined when no filter is provided', () => { |
| 147 | + expect(buildFilterString()).toBeUndefined(); |
| 148 | + expect(buildFilterString({})).toBeUndefined(); |
| 149 | + }); |
| 150 | + |
| 151 | + it('builds filter string with memberId', () => { |
| 152 | + const result = buildFilterString({ memberId: '1e5e39daa00d4fd8b53b98095337112f' }); |
| 153 | + expect(result).toBe('filter: {members: {some: {memberSpaceId: {is: "1e5e39daa00d4fd8b53b98095337112f"}}}}'); |
| 154 | + }); |
| 155 | + |
| 156 | + it('builds filter string with editorId', () => { |
| 157 | + const result = buildFilterString({ editorId: '1e5e39daa00d4fd8b53b98095337112f' }); |
| 158 | + expect(result).toBe('filter: {editors: {some: {memberSpaceId: {is: "1e5e39daa00d4fd8b53b98095337112f"}}}}'); |
| 159 | + }); |
| 160 | + |
| 161 | + it('builds filter string with spaceType PERSONAL', () => { |
| 162 | + const result = buildFilterString({ spaceType: 'PERSONAL' }); |
| 163 | + expect(result).toBe('filter: {type: {is: PERSONAL}}'); |
| 164 | + }); |
| 165 | + |
| 166 | + it('builds filter string with spaceType DAO', () => { |
| 167 | + const result = buildFilterString({ spaceType: 'DAO' }); |
| 168 | + expect(result).toBe('filter: {type: {is: DAO}}'); |
| 169 | + }); |
| 170 | + |
| 171 | + it('builds filter string with memberId and spaceType', () => { |
| 172 | + const result = buildFilterString({ memberId: '1e5e39daa00d4fd8b53b98095337112f', spaceType: 'PERSONAL' }); |
| 173 | + expect(result).toBe( |
| 174 | + 'filter: {members: {some: {memberSpaceId: {is: "1e5e39daa00d4fd8b53b98095337112f"}}}, type: {is: PERSONAL}}', |
| 175 | + ); |
| 176 | + }); |
| 177 | + |
| 178 | + it('builds filter string with editorId and spaceType', () => { |
| 179 | + const result = buildFilterString({ editorId: '1e5e39daa00d4fd8b53b98095337112f', spaceType: 'DAO' }); |
| 180 | + expect(result).toBe( |
| 181 | + 'filter: {editors: {some: {memberSpaceId: {is: "1e5e39daa00d4fd8b53b98095337112f"}}}, type: {is: DAO}}', |
| 182 | + ); |
| 183 | + }); |
| 184 | + |
| 185 | + it('normalizes UUID with dashes to dashless format', () => { |
| 186 | + const result = buildFilterString({ memberId: '1e5e39da-a00d-4fd8-b53b-98095337112f' }); |
| 187 | + expect(result).toBe('filter: {members: {some: {memberSpaceId: {is: "1e5e39daa00d4fd8b53b98095337112f"}}}}'); |
| 188 | + }); |
| 189 | + |
| 190 | + it('throws error for invalid memberId', () => { |
| 191 | + expect(() => buildFilterString({ memberId: 'invalid-id' })).toThrow('Invalid Geo ID'); |
| 192 | + }); |
| 193 | + |
| 194 | + it('throws error for invalid editorId', () => { |
| 195 | + expect(() => buildFilterString({ editorId: 'invalid"; DROP TABLE spaces; --' })).toThrow('Invalid Geo ID'); |
| 196 | + }); |
| 197 | + |
| 198 | + it('throws error for invalid spaceType', () => { |
| 199 | + // @ts-expect-error - testing runtime validation with invalid value |
| 200 | + expect(() => buildFilterString({ spaceType: 'INVALID' })).toThrow( |
| 201 | + "Invalid spaceType: INVALID. Must be 'PERSONAL' or 'DAO'.", |
| 202 | + ); |
| 203 | + }); |
| 204 | +}); |
| 205 | + |
| 206 | +describe('buildSpacesQuery', () => { |
| 207 | + it('builds query without filter', () => { |
| 208 | + const query = buildSpacesQuery(); |
| 209 | + expect(query).toContain('query spaces {'); |
| 210 | + // Check that the top-level spaces query doesn't have a filter (spaces { not spaces(filter:) |
| 211 | + expect(query).toMatch(/spaces\s*\{/); |
| 212 | + expect(query).not.toMatch(/spaces\s*\(filter:/); |
| 213 | + }); |
| 214 | + |
| 215 | + it('builds query with memberId filter', () => { |
| 216 | + const query = buildSpacesQuery({ memberId: '1e5e39daa00d4fd8b53b98095337112f' }); |
| 217 | + expect(query).toContain( |
| 218 | + 'spaces(filter: {members: {some: {memberSpaceId: {is: "1e5e39daa00d4fd8b53b98095337112f"}}}})', |
| 219 | + ); |
| 220 | + }); |
| 221 | + |
| 222 | + it('builds query with editorId filter', () => { |
| 223 | + const query = buildSpacesQuery({ editorId: '1e5e39daa00d4fd8b53b98095337112f' }); |
| 224 | + expect(query).toContain( |
| 225 | + 'spaces(filter: {editors: {some: {memberSpaceId: {is: "1e5e39daa00d4fd8b53b98095337112f"}}}})', |
| 226 | + ); |
| 227 | + }); |
| 228 | + |
| 229 | + it('builds query with spaceType filter only', () => { |
| 230 | + const query = buildSpacesQuery({ spaceType: 'DAO' }); |
| 231 | + expect(query).toContain('spaces(filter: {type: {is: DAO}})'); |
| 232 | + }); |
| 233 | + |
| 234 | + it('builds query with combined filters', () => { |
| 235 | + const query = buildSpacesQuery({ memberId: '1e5e39daa00d4fd8b53b98095337112f', spaceType: 'PERSONAL' }); |
| 236 | + expect(query).toContain( |
| 237 | + 'spaces(filter: {members: {some: {memberSpaceId: {is: "1e5e39daa00d4fd8b53b98095337112f"}}}, type: {is: PERSONAL}})', |
| 238 | + ); |
| 239 | + }); |
| 240 | + |
| 241 | + it('includes required space fields in query', () => { |
| 242 | + const query = buildSpacesQuery(); |
| 243 | + expect(query).toContain('id'); |
| 244 | + expect(query).toContain('type'); |
| 245 | + expect(query).toContain('page {'); |
| 246 | + expect(query).toContain('editorsList {'); |
| 247 | + expect(query).toContain('membersList {'); |
| 248 | + }); |
| 249 | +}); |
0 commit comments