Skip to content

Commit 07cb542

Browse files
committed
tests: make sure the storage aliases are covered
1 parent 3bcbf7e commit 07cb542

File tree

4 files changed

+306
-0
lines changed

4 files changed

+306
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { useStorage } from '#imports';
2+
import { defineEventHandler } from 'h3';
3+
4+
export default defineEventHandler(async _event => {
5+
const storage = useStorage('test-storage');
6+
7+
// Test all alias methods (get, set, del, remove)
8+
const results: Record<string, unknown> = {};
9+
10+
// Test set (alias for setItem)
11+
await storage.set('alias:user', { name: 'Jane Doe', role: 'admin' });
12+
results.set = 'success';
13+
14+
// Test get (alias for getItem)
15+
const user = await storage.get('alias:user');
16+
results.get = user;
17+
18+
// Test has (alias for hasItem)
19+
const hasUser = await storage.has('alias:user');
20+
results.has = hasUser;
21+
22+
// Setup for delete tests
23+
await storage.set('alias:temp1', 'temp1');
24+
await storage.set('alias:temp2', 'temp2');
25+
26+
// Test del (alias for removeItem)
27+
await storage.del('alias:temp1');
28+
results.del = 'success';
29+
30+
// Test remove (alias for removeItem)
31+
await storage.remove('alias:temp2');
32+
results.remove = 'success';
33+
34+
// Verify deletions worked
35+
const hasTemp1 = await storage.has('alias:temp1');
36+
const hasTemp2 = await storage.has('alias:temp2');
37+
results.verifyDeletions = !hasTemp1 && !hasTemp2;
38+
39+
// Clean up
40+
await storage.clear();
41+
42+
return {
43+
success: true,
44+
results,
45+
};
46+
});
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { expect, test } from '@playwright/test';
2+
import { waitForTransaction } from '@sentry-internal/test-utils';
3+
import { SEMANTIC_ATTRIBUTE_CACHE_HIT, SEMANTIC_ATTRIBUTE_CACHE_KEY } from '@sentry/core';
4+
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/nuxt';
5+
6+
test.describe('Storage Instrumentation - Aliases', () => {
7+
const prefixKey = (key: string) => `test-storage:${key}`;
8+
9+
test('instruments storage alias methods (get, set, has, del, remove) and creates spans', async ({ request }) => {
10+
const transactionPromise = waitForTransaction('nuxt-3', transactionEvent => {
11+
return transactionEvent.transaction?.includes('GET /api/storage-aliases-test') ?? false;
12+
});
13+
14+
const response = await request.get('/api/storage-aliases-test');
15+
expect(response.status()).toBe(200);
16+
17+
const transaction = await transactionPromise;
18+
19+
// Helper to find spans by operation
20+
const findSpansByOp = (op: string) => {
21+
return transaction.spans?.filter(span => span.data?.[SEMANTIC_ATTRIBUTE_SENTRY_OP] === op) || [];
22+
};
23+
24+
// Test set (alias for setItem)
25+
const setSpans = findSpansByOp('cache.set_item');
26+
expect(setSpans.length).toBeGreaterThanOrEqual(1);
27+
const setSpan = setSpans.find(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('alias:user'));
28+
expect(setSpan).toBeDefined();
29+
expect(setSpan?.data).toMatchObject({
30+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.set_item',
31+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt',
32+
[SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('alias:user'),
33+
'nuxt.storage.op': 'setItem',
34+
'nuxt.storage.mount': 'test-storage:',
35+
'nuxt.storage.driver': 'memory',
36+
});
37+
expect(setSpan?.description).toBe(prefixKey('alias:user'));
38+
39+
// Test get (alias for getItem)
40+
const getSpans = findSpansByOp('cache.get_item');
41+
expect(getSpans.length).toBeGreaterThanOrEqual(1);
42+
const getSpan = getSpans.find(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('alias:user'));
43+
expect(getSpan).toBeDefined();
44+
expect(getSpan?.data).toMatchObject({
45+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.get_item',
46+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt',
47+
[SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('alias:user'),
48+
[SEMANTIC_ATTRIBUTE_CACHE_HIT]: true,
49+
'nuxt.storage.op': 'getItem',
50+
'nuxt.storage.mount': 'test-storage:',
51+
'nuxt.storage.driver': 'memory',
52+
});
53+
expect(getSpan?.description).toBe(prefixKey('alias:user'));
54+
55+
// Test has (alias for hasItem)
56+
const hasSpans = findSpansByOp('cache.has_item');
57+
expect(hasSpans.length).toBeGreaterThanOrEqual(1);
58+
const hasSpan = hasSpans.find(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('alias:user'));
59+
expect(hasSpan).toBeDefined();
60+
expect(hasSpan?.data).toMatchObject({
61+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.has_item',
62+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt',
63+
[SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('alias:user'),
64+
[SEMANTIC_ATTRIBUTE_CACHE_HIT]: true,
65+
'nuxt.storage.op': 'hasItem',
66+
'nuxt.storage.mount': 'test-storage:',
67+
'nuxt.storage.driver': 'memory',
68+
});
69+
70+
// Test del and remove (both aliases for removeItem)
71+
const removeSpans = findSpansByOp('cache.remove_item');
72+
expect(removeSpans.length).toBeGreaterThanOrEqual(2); // Should have both del and remove calls
73+
74+
const delSpan = removeSpans.find(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('alias:temp1'));
75+
expect(delSpan).toBeDefined();
76+
expect(delSpan?.data).toMatchObject({
77+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.remove_item',
78+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt',
79+
[SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('alias:temp1'),
80+
'nuxt.storage.op': 'removeItem',
81+
'nuxt.storage.mount': 'test-storage:',
82+
'nuxt.storage.driver': 'memory',
83+
});
84+
expect(delSpan?.description).toBe(prefixKey('alias:temp1'));
85+
86+
const removeSpan = removeSpans.find(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('alias:temp2'));
87+
expect(removeSpan).toBeDefined();
88+
expect(removeSpan?.data).toMatchObject({
89+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.remove_item',
90+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt',
91+
[SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('alias:temp2'),
92+
'nuxt.storage.op': 'removeItem',
93+
'nuxt.storage.mount': 'test-storage:',
94+
'nuxt.storage.driver': 'memory',
95+
});
96+
expect(removeSpan?.description).toBe(prefixKey('alias:temp2'));
97+
98+
// Verify all spans have OK status
99+
const allStorageSpans = transaction.spans?.filter(
100+
span => span.data?.[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] === 'auto.cache.nuxt',
101+
);
102+
expect(allStorageSpans?.length).toBeGreaterThan(0);
103+
allStorageSpans?.forEach(span => {
104+
expect(span.status).toBe('ok');
105+
});
106+
});
107+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { useStorage } from '#imports';
2+
import { defineEventHandler } from 'h3';
3+
4+
export default defineEventHandler(async _event => {
5+
const storage = useStorage('test-storage');
6+
7+
// Test all alias methods (get, set, del, remove)
8+
const results: Record<string, unknown> = {};
9+
10+
// Test set (alias for setItem)
11+
await storage.set('alias:user', { name: 'Jane Doe', role: 'admin' });
12+
results.set = 'success';
13+
14+
// Test get (alias for getItem)
15+
const user = await storage.get('alias:user');
16+
results.get = user;
17+
18+
// Test has (alias for hasItem)
19+
const hasUser = await storage.has('alias:user');
20+
results.has = hasUser;
21+
22+
// Setup for delete tests
23+
await storage.set('alias:temp1', 'temp1');
24+
await storage.set('alias:temp2', 'temp2');
25+
26+
// Test del (alias for removeItem)
27+
await storage.del('alias:temp1');
28+
results.del = 'success';
29+
30+
// Test remove (alias for removeItem)
31+
await storage.remove('alias:temp2');
32+
results.remove = 'success';
33+
34+
// Verify deletions worked
35+
const hasTemp1 = await storage.has('alias:temp1');
36+
const hasTemp2 = await storage.has('alias:temp2');
37+
results.verifyDeletions = !hasTemp1 && !hasTemp2;
38+
39+
// Clean up
40+
await storage.clear();
41+
42+
return {
43+
success: true,
44+
results,
45+
};
46+
});
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { expect, test } from '@playwright/test';
2+
import { waitForTransaction } from '@sentry-internal/test-utils';
3+
import { SEMANTIC_ATTRIBUTE_CACHE_HIT, SEMANTIC_ATTRIBUTE_CACHE_KEY } from '@sentry/core';
4+
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/nuxt';
5+
6+
test.describe('Storage Instrumentation - Aliases', () => {
7+
const prefixKey = (key: string) => `test-storage:${key}`;
8+
9+
test('instruments storage alias methods (get, set, has, del, remove) and creates spans', async ({ request }) => {
10+
const transactionPromise = waitForTransaction('nuxt-4', transactionEvent => {
11+
return transactionEvent.transaction?.includes('GET /api/storage-aliases-test') ?? false;
12+
});
13+
14+
const response = await request.get('/api/storage-aliases-test');
15+
expect(response.status()).toBe(200);
16+
17+
const transaction = await transactionPromise;
18+
19+
// Helper to find spans by operation
20+
const findSpansByOp = (op: string) => {
21+
return transaction.spans?.filter(span => span.data?.[SEMANTIC_ATTRIBUTE_SENTRY_OP] === op) || [];
22+
};
23+
24+
// Test set (alias for setItem)
25+
const setSpans = findSpansByOp('cache.set_item');
26+
expect(setSpans.length).toBeGreaterThanOrEqual(1);
27+
const setSpan = setSpans.find(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('alias:user'));
28+
expect(setSpan).toBeDefined();
29+
expect(setSpan?.data).toMatchObject({
30+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.set_item',
31+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt',
32+
[SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('alias:user'),
33+
'nuxt.storage.op': 'setItem',
34+
'nuxt.storage.mount': 'test-storage:',
35+
'nuxt.storage.driver': 'memory',
36+
});
37+
expect(setSpan?.description).toBe(prefixKey('alias:user'));
38+
39+
// Test get (alias for getItem)
40+
const getSpans = findSpansByOp('cache.get_item');
41+
expect(getSpans.length).toBeGreaterThanOrEqual(1);
42+
const getSpan = getSpans.find(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('alias:user'));
43+
expect(getSpan).toBeDefined();
44+
expect(getSpan?.data).toMatchObject({
45+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.get_item',
46+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt',
47+
[SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('alias:user'),
48+
[SEMANTIC_ATTRIBUTE_CACHE_HIT]: true,
49+
'nuxt.storage.op': 'getItem',
50+
'nuxt.storage.mount': 'test-storage:',
51+
'nuxt.storage.driver': 'memory',
52+
});
53+
expect(getSpan?.description).toBe(prefixKey('alias:user'));
54+
55+
// Test has (alias for hasItem)
56+
const hasSpans = findSpansByOp('cache.has_item');
57+
expect(hasSpans.length).toBeGreaterThanOrEqual(1);
58+
const hasSpan = hasSpans.find(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('alias:user'));
59+
expect(hasSpan).toBeDefined();
60+
expect(hasSpan?.data).toMatchObject({
61+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.has_item',
62+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt',
63+
[SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('alias:user'),
64+
[SEMANTIC_ATTRIBUTE_CACHE_HIT]: true,
65+
'nuxt.storage.op': 'hasItem',
66+
'nuxt.storage.mount': 'test-storage:',
67+
'nuxt.storage.driver': 'memory',
68+
});
69+
70+
// Test del and remove (both aliases for removeItem)
71+
const removeSpans = findSpansByOp('cache.remove_item');
72+
expect(removeSpans.length).toBeGreaterThanOrEqual(2); // Should have both del and remove calls
73+
74+
const delSpan = removeSpans.find(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('alias:temp1'));
75+
expect(delSpan).toBeDefined();
76+
expect(delSpan?.data).toMatchObject({
77+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.remove_item',
78+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt',
79+
[SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('alias:temp1'),
80+
'nuxt.storage.op': 'removeItem',
81+
'nuxt.storage.mount': 'test-storage:',
82+
'nuxt.storage.driver': 'memory',
83+
});
84+
expect(delSpan?.description).toBe(prefixKey('alias:temp1'));
85+
86+
const removeSpan = removeSpans.find(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('alias:temp2'));
87+
expect(removeSpan).toBeDefined();
88+
expect(removeSpan?.data).toMatchObject({
89+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.remove_item',
90+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt',
91+
[SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('alias:temp2'),
92+
'nuxt.storage.op': 'removeItem',
93+
'nuxt.storage.mount': 'test-storage:',
94+
'nuxt.storage.driver': 'memory',
95+
});
96+
expect(removeSpan?.description).toBe(prefixKey('alias:temp2'));
97+
98+
// Verify all spans have OK status
99+
const allStorageSpans = transaction.spans?.filter(
100+
span => span.data?.[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] === 'auto.cache.nuxt',
101+
);
102+
expect(allStorageSpans?.length).toBeGreaterThan(0);
103+
allStorageSpans?.forEach(span => {
104+
expect(span.status).toBe('ok');
105+
});
106+
});
107+
});

0 commit comments

Comments
 (0)