Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit cf09cfe

Browse files
committed
fix(core): revert deleted fixtures (#1199)
1 parent fa0d38d commit cf09cfe

22 files changed

+1962
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { actor } from "@rivetkit/core";
2+
3+
export interface State {
4+
initialInput?: unknown;
5+
onCreateInput?: unknown;
6+
}
7+
8+
// Test actor that can capture input during creation
9+
export const inputActor = actor({
10+
onAuth: () => {},
11+
createState: (c, input): State => {
12+
return {
13+
initialInput: input,
14+
onCreateInput: undefined,
15+
};
16+
},
17+
18+
onCreate: (c, input) => {
19+
c.state.onCreateInput = input;
20+
},
21+
22+
actions: {
23+
getInputs: (c) => {
24+
return {
25+
initialInput: c.state.initialInput,
26+
onCreateInput: c.state.onCreateInput,
27+
};
28+
},
29+
},
30+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { actor } from "@rivetkit/core";
2+
3+
// Short timeout actor
4+
export const shortTimeoutActor = actor({
5+
onAuth: () => {},
6+
state: { value: 0 },
7+
options: {
8+
action: {
9+
timeout: 50, // 50ms timeout
10+
},
11+
},
12+
actions: {
13+
quickAction: async (c) => {
14+
return "quick response";
15+
},
16+
slowAction: async (c) => {
17+
// This action should timeout
18+
await new Promise((resolve) => setTimeout(resolve, 100));
19+
return "slow response";
20+
},
21+
},
22+
});
23+
24+
// Long timeout actor
25+
export const longTimeoutActor = actor({
26+
onAuth: () => {},
27+
state: { value: 0 },
28+
options: {
29+
action: {
30+
timeout: 200, // 200ms timeout
31+
},
32+
},
33+
actions: {
34+
delayedAction: async (c) => {
35+
// This action should complete within timeout
36+
await new Promise((resolve) => setTimeout(resolve, 100));
37+
return "delayed response";
38+
},
39+
},
40+
});
41+
42+
// Default timeout actor
43+
export const defaultTimeoutActor = actor({
44+
onAuth: () => {},
45+
state: { value: 0 },
46+
actions: {
47+
normalAction: async (c) => {
48+
await new Promise((resolve) => setTimeout(resolve, 50));
49+
return "normal response";
50+
},
51+
},
52+
});
53+
54+
// Sync actor (timeout shouldn't apply)
55+
export const syncTimeoutActor = actor({
56+
onAuth: () => {},
57+
state: { value: 0 },
58+
options: {
59+
action: {
60+
timeout: 50, // 50ms timeout
61+
},
62+
},
63+
actions: {
64+
syncAction: (c) => {
65+
return "sync response";
66+
},
67+
},
68+
});
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { actor, UserError } from "@rivetkit/core";
2+
3+
// Actor with synchronous actions
4+
export const syncActionActor = actor({
5+
onAuth: () => {},
6+
state: { value: 0 },
7+
actions: {
8+
// Simple synchronous action that returns a value directly
9+
increment: (c, amount = 1) => {
10+
c.state.value += amount;
11+
return c.state.value;
12+
},
13+
// Synchronous action that returns an object
14+
getInfo: (c) => {
15+
return {
16+
currentValue: c.state.value,
17+
timestamp: Date.now(),
18+
};
19+
},
20+
// Synchronous action with no return value (void)
21+
reset: (c) => {
22+
c.state.value = 0;
23+
},
24+
},
25+
});
26+
27+
// Actor with asynchronous actions
28+
export const asyncActionActor = actor({
29+
onAuth: () => {},
30+
state: { value: 0, data: null as any },
31+
actions: {
32+
// Async action with a delay
33+
delayedIncrement: async (c, amount = 1) => {
34+
await Promise.resolve();
35+
c.state.value += amount;
36+
return c.state.value;
37+
},
38+
// Async action that simulates an API call
39+
fetchData: async (c, id: string) => {
40+
await Promise.resolve();
41+
42+
// Simulate response data
43+
const data = { id, timestamp: Date.now() };
44+
c.state.data = data;
45+
return data;
46+
},
47+
// Async action with error handling
48+
asyncWithError: async (c, shouldError: boolean) => {
49+
await Promise.resolve();
50+
51+
if (shouldError) {
52+
throw new UserError("Intentional error");
53+
}
54+
55+
return "Success";
56+
},
57+
},
58+
});
59+
60+
// Actor with promise actions
61+
export const promiseActor = actor({
62+
onAuth: () => {},
63+
state: { results: [] as string[] },
64+
actions: {
65+
// Action that returns a resolved promise
66+
resolvedPromise: (c) => {
67+
return Promise.resolve("resolved value");
68+
},
69+
// Action that returns a promise that resolves after a delay
70+
delayedPromise: (c): Promise<string> => {
71+
return new Promise<string>((resolve) => {
72+
c.state.results.push("delayed");
73+
resolve("delayed value");
74+
});
75+
},
76+
// Action that returns a rejected promise
77+
rejectedPromise: (c) => {
78+
return Promise.reject(new UserError("promised rejection"));
79+
},
80+
// Action to check the collected results
81+
getResults: (c) => {
82+
return c.state.results;
83+
},
84+
},
85+
});
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { actor, UserError } from "@rivetkit/core";
2+
3+
// Basic auth actor - requires API key
4+
export const authActor = actor({
5+
state: { requests: 0 },
6+
onAuth: (opts, params: { apiKey?: string } | undefined) => {
7+
const apiKey = params?.apiKey;
8+
if (!apiKey) {
9+
throw new UserError("API key required", { code: "missing_auth" });
10+
}
11+
12+
if (apiKey !== "valid-api-key") {
13+
throw new UserError("Invalid API key", { code: "invalid_auth" });
14+
}
15+
16+
return { userId: "user123", token: apiKey };
17+
},
18+
actions: {
19+
getRequests: (c) => {
20+
c.state.requests++;
21+
return c.state.requests;
22+
},
23+
getUserAuth: (c) => c.conn.auth,
24+
},
25+
});
26+
27+
// Intent-specific auth actor - checks different permissions for different intents
28+
export const intentAuthActor = actor({
29+
state: { value: 0 },
30+
onAuth: ({ request, intents }, params: { role: string }) => {
31+
console.log("intents", intents, params);
32+
const role = params.role;
33+
34+
if (intents.has("create") && role !== "admin") {
35+
throw new UserError("Admin role required for create operations", {
36+
code: "insufficient_permissions",
37+
});
38+
}
39+
40+
if (intents.has("action") && !["admin", "user"].includes(role || "")) {
41+
throw new UserError("User or admin role required for actions", {
42+
code: "insufficient_permissions",
43+
});
44+
}
45+
46+
return { role, timestamp: Date.now() };
47+
},
48+
actions: {
49+
getValue: (c) => c.state.value,
50+
setValue: (c, value: number) => {
51+
c.state.value = value;
52+
return value;
53+
},
54+
getAuth: (c) => c.conn.auth,
55+
},
56+
});
57+
58+
// Public actor - empty onAuth to allow public access
59+
export const publicActor = actor({
60+
state: { visitors: 0 },
61+
onAuth: () => {
62+
return null; // Allow public access
63+
},
64+
actions: {
65+
visit: (c) => {
66+
c.state.visitors++;
67+
return c.state.visitors;
68+
},
69+
},
70+
});
71+
72+
// No auth actor - should fail when accessed publicly (no onAuth defined)
73+
export const noAuthActor = actor({
74+
state: { value: 42 },
75+
actions: {
76+
getValue: (c) => c.state.value,
77+
},
78+
});
79+
80+
// Async auth actor - tests promise-based authentication
81+
export const asyncAuthActor = actor({
82+
state: { count: 0 },
83+
onAuth: async (opts, params: { token?: string } | undefined) => {
84+
const token = params?.token;
85+
if (!token) {
86+
throw new UserError("Token required", { code: "missing_token" });
87+
}
88+
89+
// Simulate token validation
90+
if (token === "invalid") {
91+
throw new UserError("Token is invalid", { code: "invalid_token" });
92+
}
93+
94+
return { userId: `user-${token}`, validated: true };
95+
},
96+
actions: {
97+
increment: (c) => {
98+
c.state.count++;
99+
return c.state.count;
100+
},
101+
getAuthData: (c) => c.conn.auth,
102+
},
103+
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { actor, CONNECTION_DRIVER_WEBSOCKET } from "@rivetkit/core";
2+
3+
export const connLivenessActor = actor({
4+
onAuth: () => {},
5+
state: {
6+
counter: 0,
7+
acceptingConnections: true,
8+
},
9+
options: {
10+
lifecycle: {
11+
connectionLivenessInterval: 5_000,
12+
connectionLivenessTimeout: 2_500,
13+
},
14+
},
15+
onConnect: (c, conn) => {
16+
if (!c.state.acceptingConnections) {
17+
conn.disconnect();
18+
throw new Error("Actor is not accepting connections");
19+
}
20+
},
21+
actions: {
22+
getWsConnectionsLiveness: (c) => {
23+
return Array.from(c.conns.values())
24+
.filter((conn) => conn.driver === CONNECTION_DRIVER_WEBSOCKET)
25+
.map((conn) => ({
26+
id: conn.id,
27+
status: conn.status,
28+
lastSeen: conn.lastSeen,
29+
}));
30+
},
31+
getConnectionId: (c) => {
32+
return c.conn.id;
33+
},
34+
kill: (c, connId: string) => {
35+
c.state.acceptingConnections = false;
36+
// Disconnect the connection with the given ID
37+
// This simulates a network failure or a manual disconnection
38+
// The connection will be cleaned up by the actor manager after the timeout
39+
const conn = c.conns.get(connId);
40+
if (conn) {
41+
conn.disconnect();
42+
}
43+
},
44+
getCounter: (c) => {
45+
return c.state.counter;
46+
},
47+
increment: (c, amount: number) => {
48+
c.state.counter += amount;
49+
return c.state.counter;
50+
},
51+
},
52+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { actor } from "@rivetkit/core";
2+
3+
export const counterWithParams = actor({
4+
onAuth: () => {},
5+
state: { count: 0, initializers: [] as string[] },
6+
createConnState: (c, opts, params: { name?: string }) => {
7+
return {
8+
name: params.name || "anonymous",
9+
};
10+
},
11+
onConnect: (c, conn) => {
12+
// Record connection name
13+
c.state.initializers.push(conn.state.name);
14+
},
15+
actions: {
16+
increment: (c, x: number) => {
17+
c.state.count += x;
18+
c.broadcast("newCount", {
19+
count: c.state.count,
20+
by: c.conn.state.name,
21+
});
22+
return c.state.count;
23+
},
24+
getInitializers: (c) => {
25+
return c.state.initializers;
26+
},
27+
},
28+
});

0 commit comments

Comments
 (0)